Reflection for RapidJSON 0.0.16
Reflection for serializing/deserializing with RapidJSON
Loading...
Searching...
No Matches
versioning.h
Go to the documentation of this file.
1#ifndef REFLECTIVE_RAPIDJSON_VERSIONING
2#define REFLECTIVE_RAPIDJSON_VERSIONING
3
4#include <c++utilities/misc/traits.h>
5
6namespace ReflectiveRapidJSON {
7
8#ifdef REFLECTIVE_RAPIDJSON_GENERATOR
9#define REFLECTIVE_RAPIDJSON_CAT_1(a, b) a##b
10#define REFLECTIVE_RAPIDJSON_CAT_2(a, b) REFLECTIVE_RAPIDJSON_CAT_1(a, b)
11#define REFLECTIVE_RAPIDJSON_AS_OF_VERSION(version) \
12 static constexpr std::size_t REFLECTIVE_RAPIDJSON_CAT_2(rrjAsOfVersion, __COUNTER__) = version; \
13public
14#define REFLECTIVE_RAPIDJSON_UNTIL_VERSION(version) \
15 static constexpr std::size_t REFLECTIVE_RAPIDJSON_CAT_2(rrjUntilVersion, __COUNTER__) = version; \
16public
17#else
18#define REFLECTIVE_RAPIDJSON_AS_OF_VERSION(version) public
19#define REFLECTIVE_RAPIDJSON_UNTIL_VERSION(version) public
20#endif
21
22#ifdef REFLECTIVE_RAPIDJSON_SHORT_MACROS
23#define as_of_version(version) REFLECTIVE_RAPIDJSON_AS_OF_VERSION(version)
24#define until_version(version) REFLECTIVE_RAPIDJSON_UNTIL_VERSION(version)
25#endif
26
28
29template <typename VersionType> struct VersionNotSupported {
30 VersionType presentVersion = 0, maxVersion = 0;
31 const char *record = nullptr;
32};
33
34template <typename Type, bool Condition = IsVersioned<Type>::value> struct Versioning {
35 static constexpr auto enabled = false;
36};
37
38template <typename Type> struct Versioning<Type, true> {
39 static constexpr auto enabled = Type::version != 0;
40 static constexpr auto serializationDefault = Type::version;
41 static constexpr auto maxSupported = Type::version;
42 static constexpr auto &applyDefault(std::remove_const_t<decltype(serializationDefault)> &version)
43 {
44 if (!version) {
45 version = serializationDefault;
46 }
47 return version;
48 }
49 static constexpr auto applyDefaultValue(std::remove_const_t<decltype(serializationDefault)> version)
50 {
51 return applyDefault(version);
52 }
53 static constexpr auto isSupported(decltype(maxSupported) version)
54 {
55 return version <= maxSupported;
56 }
57 static constexpr auto assertVersion(decltype(maxSupported) version, const char *record = nullptr)
58 {
59 if (!isSupported(version)) {
60 throw typename Type::VersionNotSupported({ .presentVersion = version, .maxVersion = maxSupported, .record = record });
61 }
62 }
63};
64
65} // namespace ReflectiveRapidJSON
66
67#endif // REFLECTIVE_RAPIDJSON_TRAITS
CPP_UTILITIES_TRAITS_DEFINE_TYPE_CHECK(IsVersioned, T::version)
static constexpr auto isSupported(decltype(maxSupported) version)
Definition versioning.h:53
static constexpr auto & applyDefault(std::remove_const_t< decltype(serializationDefault)> &version)
Definition versioning.h:42
static constexpr auto applyDefaultValue(std::remove_const_t< decltype(serializationDefault)> version)
Definition versioning.h:49
static constexpr auto assertVersion(decltype(maxSupported) version, const char *record=nullptr)
Definition versioning.h:57
static constexpr auto enabled
Definition versioning.h:35