reflective-rapidjson/lib/binary/reflector.h

235 lines
9.0 KiB
C++

#ifndef REFLECTIVE_RAPIDJSON_BINARY_REFLECTOR_H
#define REFLECTIVE_RAPIDJSON_BINARY_REFLECTOR_H
/*!
* \file reflector.h
* \brief Contains BinaryReader and BinaryWriter supporting binary (de)serialization
* of primitive and custom types.
*/
#include "../traits.h"
#include <c++utilities/conversion/conversionexception.h>
#include <c++utilities/conversion/types.h>
#include <c++utilities/io/binaryreader.h>
#include <c++utilities/io/binarywriter.h>
#include <any>
#include <limits>
#include <memory>
#include <string>
#include <tuple>
namespace ReflectiveRapidJSON {
/*!
* \brief The AdaptedBinarySerializable class allows considering 3rd party classes as serializable.
*/
template <typename T> struct AdaptedBinarySerializable : public Traits::Bool<false> {
static constexpr const char *name = "AdaptedBinarySerializable";
static constexpr const char *qualifiedName = "ReflectiveRapidJSON::AdaptedBinarySerializable";
};
template <typename Type> struct BinarySerializable;
/*!
* \brief The BinaryReflector namespace contains BinaryReader and BinaryWriter for automatic binary (de)serialization.
*/
namespace BinaryReflector {
// define traits to distinguish between "built-in" types like int, std::string, std::vector, ... and custom structs/classes
template <typename Type>
using IsBuiltInType = Traits::Any<Traits::IsAnyOf<Type, char, byte, bool, std::string, int16, uint16, int32, uint32, int64, uint64, float32, float64>,
Traits::IsIteratable<Type>, Traits::IsSpecializingAnyOf<Type, std::pair, std::unique_ptr, std::shared_ptr>, std::is_enum<Type>>;
template <typename Type> using IsCustomType = Traits::Not<IsBuiltInType<Type>>;
class BinaryDeserializer;
class BinarySerializer;
template <typename Type, Traits::EnableIf<IsCustomType<Type>> * = nullptr> void readCustomType(BinaryDeserializer &deserializer, Type &customType);
template <typename Type, Traits::EnableIf<IsCustomType<Type>> * = nullptr> void writeCustomType(BinarySerializer &serializer, const Type &customType);
class BinaryDeserializer : public IoUtilities::BinaryReader {
public:
BinaryDeserializer(std::istream *stream);
using IoUtilities::BinaryReader::read;
template <typename Type, Traits::EnableIf<Traits::IsSpecializationOf<Type, std::pair>> * = nullptr> void read(Type &pair);
template <typename Type, Traits::EnableIf<Traits::IsSpecializationOf<Type, std::unique_ptr>> * = nullptr> void read(Type &pair);
template <typename Type, Traits::EnableIf<Traits::IsSpecializationOf<Type, std::shared_ptr>> * = nullptr> void read(Type &pair);
template <typename Type, Traits::EnableIf<IsArray<Type>, Traits::IsResizable<Type>> * = nullptr> void read(Type &iteratable);
template <typename Type, Traits::EnableIfAny<IsMapOrHash<Type>, IsMultiMapOrHash<Type>> * = nullptr> void read(Type &iteratable);
template <typename Type,
Traits::EnableIf<IsIteratableExceptString<Type>,
Traits::None<IsMapOrHash<Type>, IsMultiMapOrHash<Type>, Traits::All<IsArray<Type>, Traits::IsResizable<Type>>>> * = nullptr>
void read(Type &iteratable);
template <typename Type, Traits::EnableIf<std::is_enum<Type>> * = nullptr> void read(Type &customType);
template <typename Type, Traits::EnableIf<IsCustomType<Type>> * = nullptr> void read(Type &customType);
std::unordered_map<uint64, std::any> m_pointer;
};
class BinarySerializer : public IoUtilities::BinaryWriter {
public:
BinarySerializer(std::ostream *stream);
using IoUtilities::BinaryWriter::write;
template <typename Type, Traits::EnableIf<Traits::IsSpecializationOf<Type, std::pair>> * = nullptr> void write(const Type &pair);
template <typename Type, Traits::EnableIf<Traits::IsSpecializingAnyOf<Type, std::unique_ptr>> * = nullptr> void write(const Type &pointer);
template <typename Type, Traits::EnableIf<Traits::IsSpecializingAnyOf<Type, std::shared_ptr>> * = nullptr> void write(const Type &pointer);
template <typename Type, Traits::EnableIf<IsIteratableExceptString<Type>, Traits::HasSize<Type>> * = nullptr> void write(const Type &iteratable);
template <typename Type, Traits::EnableIf<std::is_enum<Type>> * = nullptr> void write(const Type &customType);
template <typename Type, Traits::EnableIf<IsCustomType<Type>> * = nullptr> void write(const Type &customType);
std::unordered_map<uint64, bool> m_pointer;
};
inline BinaryDeserializer::BinaryDeserializer(std::istream *stream)
: IoUtilities::BinaryReader(stream)
{
}
template <typename Type, Traits::EnableIf<Traits::IsSpecializationOf<Type, std::pair>> *> void BinaryDeserializer::read(Type &pair)
{
read(pair.first);
read(pair.second);
}
template <typename Type, Traits::EnableIf<Traits::IsSpecializationOf<Type, std::unique_ptr>> *> void BinaryDeserializer::read(Type &pointer)
{
if (!readBool()) {
pointer.reset();
return;
}
pointer = std::make_unique<typename Type::element_type>();
read(*pointer);
}
template <typename Type, Traits::EnableIf<Traits::IsSpecializationOf<Type, std::shared_ptr>> *> void BinaryDeserializer::read(Type &pointer)
{
const auto occurence = readByte();
if (!occurence) {
// pointer not set
pointer.reset();
return;
}
const auto id = readVariableLengthUIntBE();
if (occurence == 1) {
// first occurence: make a new pointer
m_pointer[id] = pointer = std::make_shared<typename Type::element_type>();
read(*pointer);
return;
}
// further occurences: copy previous pointer
try {
pointer = std::any_cast<Type>(m_pointer[id]);
} catch (const std::bad_any_cast) {
throw ConversionUtilities::ConversionException("Referenced pointer type does not match");
}
}
template <typename Type, Traits::EnableIf<IsArray<Type>, Traits::IsResizable<Type>> *> void BinaryDeserializer::read(Type &iteratable)
{
const auto size = readVariableLengthUIntBE();
iteratable.resize(size);
for (auto &element : iteratable) {
read(element);
}
}
template <typename Type, Traits::EnableIfAny<IsMapOrHash<Type>, IsMultiMapOrHash<Type>> *> void BinaryDeserializer::read(Type &iteratable)
{
const auto size = readVariableLengthUIntBE();
for (size_t i = 0; i != size; ++i) {
std::pair<typename std::remove_const<typename Type::value_type::first_type>::type, typename Type::value_type::second_type> value;
read(value);
iteratable.emplace(std::move(value));
}
}
template <typename Type,
Traits::EnableIf<IsIteratableExceptString<Type>,
Traits::None<IsMapOrHash<Type>, IsMultiMapOrHash<Type>, Traits::All<IsArray<Type>, Traits::IsResizable<Type>>>> *>
void BinaryDeserializer::read(Type &iteratable)
{
const auto size = readVariableLengthUIntBE();
for (size_t i = 0; i != size; ++i) {
typename Type::value_type value;
read(value);
iteratable.emplace(std::move(value));
}
}
template <typename Type, Traits::EnableIf<std::is_enum<Type>> *> void BinaryDeserializer::read(Type &enumValue)
{
typename std::underlying_type<Type>::type value;
read(value);
enumValue = static_cast<Type>(value);
}
template <typename Type, Traits::EnableIf<IsCustomType<Type>> *> void BinaryDeserializer::read(Type &customType)
{
readCustomType(*this, customType);
}
inline BinarySerializer::BinarySerializer(std::ostream *stream)
: IoUtilities::BinaryWriter(stream)
{
}
template <typename Type, Traits::EnableIf<Traits::IsSpecializationOf<Type, std::pair>> *> void BinarySerializer::write(const Type &pair)
{
write(pair.first);
write(pair.second);
}
template <typename Type, Traits::EnableIf<Traits::IsSpecializingAnyOf<Type, std::unique_ptr>> *> void BinarySerializer::write(const Type &pointer)
{
const bool hasValue = pointer != nullptr;
writeBool(hasValue);
if (hasValue) {
write(*pointer);
}
}
template <typename Type, Traits::EnableIf<Traits::IsSpecializingAnyOf<Type, std::shared_ptr>> *> void BinarySerializer::write(const Type &pointer)
{
if (pointer == nullptr) {
writeByte(0);
return;
}
const auto id = reinterpret_cast<uint64>(pointer.get());
auto &alreadyWritten = m_pointer[id];
writeByte(alreadyWritten ? 2 : 1);
writeVariableLengthUIntBE(id);
if (!alreadyWritten) {
alreadyWritten = true;
write(*pointer);
}
}
template <typename Type, Traits::EnableIf<IsIteratableExceptString<Type>, Traits::HasSize<Type>> *>
void BinarySerializer::write(const Type &iteratable)
{
writeVariableLengthUIntBE(iteratable.size());
for (const auto &element : iteratable) {
write(element);
}
}
template <typename Type, Traits::EnableIf<std::is_enum<Type>> *> void BinarySerializer::write(const Type &enumValue)
{
write(static_cast<typename std::underlying_type<Type>::type>(enumValue));
}
template <typename Type, Traits::EnableIf<IsCustomType<Type>> *> void BinarySerializer::write(const Type &customType)
{
writeCustomType(*this, customType);
}
} // namespace BinaryReflector
} // namespace ReflectiveRapidJSON
#endif // REFLECTIVE_RAPIDJSON_BINARY_REFLECTOR_H