2018-06-23 14:35:43 +02:00
|
|
|
#ifndef REFLECTIVE_RAPIDJSON_BINARY_SERIALIZABLE_H
|
|
|
|
#define REFLECTIVE_RAPIDJSON_BINARY_SERIALIZABLE_H
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \file serializable.h
|
2021-07-03 19:50:25 +02:00
|
|
|
* \brief Contains only the definition of the BinarySerializable template class which makes the reflection
|
2018-06-23 14:35:43 +02:00
|
|
|
* accessible. The actual implementation is found in binaryreflector.h and generated files.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "./reflector.h"
|
|
|
|
|
|
|
|
#include <iosfwd>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace ReflectiveRapidJSON {
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief The BinarySerializable class provides the CRTP-base for (de)serializable objects.
|
|
|
|
*/
|
2021-06-06 15:06:55 +02:00
|
|
|
template <typename Type, BinaryVersion v> struct BinarySerializable {
|
|
|
|
void toBinary(std::ostream &outputStream, BinaryVersion version = 0) const;
|
|
|
|
BinaryVersion restoreFromBinary(std::istream &inputStream);
|
2018-06-23 17:24:28 +02:00
|
|
|
static Type fromBinary(std::istream &inputStream);
|
2018-06-23 14:35:43 +02:00
|
|
|
|
|
|
|
static constexpr const char *qualifiedName = "ReflectiveRapidJSON::BinarySerializable";
|
2021-06-06 15:06:55 +02:00
|
|
|
static constexpr auto version = v;
|
2018-06-23 14:35:43 +02:00
|
|
|
};
|
|
|
|
|
2021-06-06 15:06:55 +02:00
|
|
|
template <typename Type, BinaryVersion v> inline void BinarySerializable<Type, v>::toBinary(std::ostream &outputStream, BinaryVersion version) const
|
2018-06-23 14:35:43 +02:00
|
|
|
{
|
2021-06-06 15:06:55 +02:00
|
|
|
BinaryReflector::BinarySerializer(&outputStream).write(static_cast<const Type &>(*this), version);
|
2018-06-23 14:35:43 +02:00
|
|
|
}
|
|
|
|
|
2021-06-06 15:06:55 +02:00
|
|
|
template <typename Type, BinaryVersion v> inline BinaryVersion BinarySerializable<Type, v>::restoreFromBinary(std::istream &inputStream)
|
2018-06-23 14:35:43 +02:00
|
|
|
{
|
2021-06-06 15:06:55 +02:00
|
|
|
return BinaryReflector::BinaryDeserializer(&inputStream).read(static_cast<Type &>(*this));
|
2018-06-23 14:35:43 +02:00
|
|
|
}
|
|
|
|
|
2021-06-06 15:06:55 +02:00
|
|
|
template <typename Type, BinaryVersion v> Type BinarySerializable<Type, v>::fromBinary(std::istream &inputStream)
|
2018-06-23 17:24:28 +02:00
|
|
|
{
|
|
|
|
Type object;
|
|
|
|
static_cast<BinarySerializable<Type> &>(object).restoreFromBinary(inputStream);
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2018-06-23 14:35:43 +02:00
|
|
|
/*!
|
|
|
|
* \def The REFLECTIVE_RAPIDJSON_MAKE_BINARY_SERIALIZABLE macro allows to adapt (de)serialization for types defined in 3rd party header files.
|
|
|
|
* \remarks The struct will not have the toBinary() and fromBinary() methods available. Use the corresponding functions in the namespace
|
|
|
|
* ReflectiveRapidJSON::BinaryReflector instead.
|
|
|
|
* \todo GCC complains when putting :: before "ReflectiveRapidJSON" namespace: "global qualification of class name is invalid before ':' token"
|
|
|
|
* Find out whether this is a compiler bug or a correct error message.
|
|
|
|
*/
|
|
|
|
#define REFLECTIVE_RAPIDJSON_MAKE_BINARY_SERIALIZABLE(T) \
|
|
|
|
template <> struct ReflectiveRapidJSON::AdaptedBinarySerializable<T> : Traits::Bool<true> { \
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ReflectiveRapidJSON
|
|
|
|
|
|
|
|
#endif // REFLECTIVE_RAPIDJSON_BINARY_SERIALIZABLE_H
|