Reflection for RapidJSON 0.0.16
Reflection for serializing/deserializing with RapidJSON
Loading...
Searching...
No Matches
serializable.h
Go to the documentation of this file.
1#ifndef REFLECTIVE_RAPIDJSON_BINARY_SERIALIZABLE_H
2#define REFLECTIVE_RAPIDJSON_BINARY_SERIALIZABLE_H
3
10#include "./reflector.h"
11
12#include <iosfwd>
13#include <string>
14
15namespace ReflectiveRapidJSON {
16
18
22template <typename Type, BinaryVersion v> struct BinarySerializable {
24 void toBinary(std::ostream &outputStream, BinaryVersion version = 0) const;
25 BinaryVersion restoreFromBinary(std::istream &inputStream);
26 static Type fromBinary(std::istream &inputStream);
27
28 static constexpr const char *qualifiedName = "ReflectiveRapidJSON::BinarySerializable";
29 static constexpr auto version = v;
30
31#if __cplusplus > 201707L
32 bool operator==(const BinarySerializable<Type, v> &) const = default;
33#endif
34};
35
36template <typename Type, BinaryVersion v> inline void BinarySerializable<Type, v>::toBinary(std::ostream &outputStream, BinaryVersion version) const
37{
38 BinaryReflector::BinarySerializer(&outputStream).write(static_cast<const Type &>(*this), version);
39}
40
41template <typename Type, BinaryVersion v> inline BinaryVersion BinarySerializable<Type, v>::restoreFromBinary(std::istream &inputStream)
42{
43 return BinaryReflector::BinaryDeserializer(&inputStream).read(static_cast<Type &>(*this));
44}
45
46template <typename Type, BinaryVersion v> Type BinarySerializable<Type, v>::fromBinary(std::istream &inputStream)
47{
48 Type object;
49 static_cast<BinarySerializable<Type> &>(object).restoreFromBinary(inputStream);
50 return object;
51}
52
60#define REFLECTIVE_RAPIDJSON_MAKE_BINARY_SERIALIZABLE(T) \
61 template <> struct ReflectiveRapidJSON::AdaptedBinarySerializable<T> : Traits::Bool<true> {}
62
63} // namespace ReflectiveRapidJSON
64
65#endif // REFLECTIVE_RAPIDJSON_BINARY_SERIALIZABLE_H
Contains BinaryReader and BinaryWriter supporting binary (de)serialization of primitive and custom ty...
The BinaryDeserializer class can read various data types, including custom ones, from an std::istream...
Definition reflector.h:73
The BinarySerializer class can write various data types, including custom ones, to an std::ostream.
Definition reflector.h:100
std::uint64_t BinaryVersion
Definition reflector.h:38
VersionNotSupported< BinaryVersion > BinaryVersionNotSupported
The BinarySerializable class provides the CRTP-base for (de)serializable objects.
static Type fromBinary(std::istream &inputStream)
static constexpr const char * qualifiedName
void toBinary(std::ostream &outputStream, BinaryVersion version=0) const
BinaryVersion restoreFromBinary(std::istream &inputStream)