Reflection for RapidJSON 0.0.16
Reflection for serializing/deserializing with RapidJSON
Loading...
Searching...
No Matches
traits.cpp
Go to the documentation of this file.
1#include "../traits.h"
2#include "../versioning.h"
3
5
6#include <list>
7#include <vector>
8
9// define structs for testing REFLECTIVE_RAPIDJSON_TREAT_AS_…
10struct Foo {};
11struct Bar {};
12
13// define structs for testing versioning
14struct VersionlessBase : public ReflectiveRapidJSON::BinarySerializable<VersionlessBase> {};
15struct VersionedDerived : public VersionlessBase, public ReflectiveRapidJSON::BinarySerializable<VersionedDerived, 1> {};
16struct VersionedBase : public ReflectiveRapidJSON::BinarySerializable<VersionlessBase, 1> {};
17struct VersionlessDerived : public VersionedBase, public ReflectiveRapidJSON::BinarySerializable<VersionlessDerived> {};
18
19namespace ReflectiveRapidJSON {
24} // namespace ReflectiveRapidJSON
25
26using namespace std;
27using namespace ReflectiveRapidJSON;
28
29// test traits
30static_assert(IsArray<vector<int>>::value, "vector mapped to array");
31static_assert(IsArray<list<int>>::value, "list mapped to array");
32static_assert(!IsArray<set<int>>::value, "set not considered an array");
33static_assert(!IsArray<multiset<int>>::value, "multiset not considered an array");
34static_assert(IsArrayOrSet<set<int>>::value, "set is array or set");
35static_assert(IsArrayOrSet<multiset<int>>::value, "multiset is array or set");
36static_assert(IsArrayOrSet<Foo>::value, "Foo is array or set via TreatAsMultiSet");
37static_assert(IsArrayOrSet<Bar>::value, "Foo is array or set via TreatAsSet");
38static_assert(!IsArrayOrSet<string>::value, "string not mapped to array or set though it is iteratable");
39static_assert(IsSet<set<int>>::value, "set mapped to set");
40static_assert(IsSet<unordered_set<int>>::value, "unordered_set mapped to set");
41static_assert(IsSet<Bar>::value, "Bar mapped to set via TreatAsSet");
42static_assert(!IsSet<string>::value, "string not mapped to set");
43static_assert(IsMultiSet<unordered_multiset<int>>::value, "multiset");
44static_assert(IsMultiSet<Foo>::value, "Foo mapped to multiset via TreatAsMultiSet");
45static_assert(!IsMultiSet<string>::value, "string not mapped to multiset");
46static_assert(!IsArray<string>::value, "string not mapped to array though it is iteratable");
47static_assert(IsMapOrHash<map<string, int>>::value, "map mapped to object");
48static_assert(IsMapOrHash<unordered_map<string, int>>::value, "hash mapped to object");
49static_assert(!IsMapOrHash<vector<int>>::value, "vector not mapped to object");
50static_assert(IsMapOrHash<Foo>::value, "Foo mapped to object via TreatAsMapOrHash");
51static_assert(IsMultiMapOrHash<multimap<string, int>>::value, "multimap mapped to object");
52static_assert(IsMultiMapOrHash<unordered_multimap<string, int>>::value, "unordered multimap mapped to object");
53static_assert(!IsMultiMapOrHash<vector<int>>::value, "vector not mapped to object");
54static_assert(IsMultiMapOrHash<Foo>::value, "Foo mapped to object via TreatAsMultiMapOrHash");
55static_assert(IsIteratableExceptString<std::vector<int>>::value, "vector is iteratable");
56static_assert(!IsIteratableExceptString<std::string>::value, "string not iteratable");
57static_assert(!IsIteratableExceptString<std::wstring>::value, "wstring not iteratable");
58static_assert(!IsIteratableExceptString<const std::string>::value, "string not iteratable");
59
60// test versioning traits
61static_assert(!Versioning<int>::enabled, "versioning for built-in types not enabled");
62static_assert(!Versioning<std::string>::enabled, "versioning for standard types not enabled");
63static_assert(!Versioning<VersionlessBase>::enabled, "versioning not enabled by default");
64static_assert(Versioning<BinarySerializable<VersionedDerived, 1>>::enabled, "versioning enabled if non-zero version parameter specified (derived)");
65static_assert(Versioning<VersionedBase>::enabled, "versioning enabled if non-zero version parameter specified (base)");
66static_assert(!Versioning<BinarySerializable<VersionlessDerived>>::enabled, "versioning disabled for derived, even if base is versioned");
67static_assert(!Versioning<BinarySerializable<Foo, 0>>::enabled, "versioning disabled if zero-version specified");
68static_assert(Versioning<BinarySerializable<Foo, 3>>::applyDefaultValue(0) == 3, "default version returned");
69static_assert(Versioning<BinarySerializable<Foo, 3>>::applyDefaultValue(2) == 2, "default version overridden");
Contains only the definition of the BinarySerializable template class which makes the reflection acce...
Traits::Any< Traits::IsSpecializationOf< Type, std::set >, Traits::IsSpecializationOf< Type, std::unordered_set >, TreatAsSet< Type > > IsSet
Definition traits.h:43
Traits::Any< Traits::IsSpecializationOf< Type, std::multiset >, Traits::IsSpecializationOf< Type, std::unordered_multiset >, TreatAsMultiSet< Type > > IsMultiSet
Definition traits.h:45
Traits::Any< Traits::All< Traits::IsIteratable< Type >, Traits::Not< Traits::IsSpecializationOf< Type, std::basic_string > >, Traits::Not< Traits::IsSpecializationOf< Type, std::basic_string_view > >, Traits::Not< IsMapOrHash< Type > >, Traits::Not< IsMultiMapOrHash< Type > > >, TreatAsSet< Type >, TreatAsMultiSet< Type > > IsArrayOrSet
Definition traits.h:47
Traits::Any< Traits::IsSpecializationOf< Type, std::multimap >, Traits::IsSpecializationOf< Type, std::unordered_multimap >, TreatAsMultiMapOrHash< Type > > IsMultiMapOrHash
Definition traits.h:40
Traits::All< Traits::IsIteratable< Type >, Traits::Not< Traits::IsSpecializationOf< Type, std::basic_string > >, Traits::Not< Traits::IsSpecializationOf< Type, std::basic_string > > > IsIteratableExceptString
Definition traits.h:55
Traits::All< Traits::IsIteratable< Type >, Traits::Not< Traits::IsSpecializationOf< Type, std::basic_string > >, Traits::Not< IsMapOrHash< Type > >, Traits::Not< IsMultiMapOrHash< Type > >, Traits::Not< IsSet< Type > >, Traits::Not< IsMultiSet< Type > > > IsArray
Definition traits.h:52
Traits::Any< Traits::IsSpecializationOf< Type, std::map >, Traits::IsSpecializationOf< Type, std::unordered_map >, TreatAsMapOrHash< Type > > IsMapOrHash
Definition traits.h:38
Definition traits.cpp:11
Definition traits.cpp:10
The BinarySerializable class provides the CRTP-base for (de)serializable objects.
#define REFLECTIVE_RAPIDJSON_TREAT_AS_MULTI_MAP_OR_HASH(T)
Definition traits.h:29
#define REFLECTIVE_RAPIDJSON_TREAT_AS_MULTI_SET(T)
Definition traits.h:33
#define REFLECTIVE_RAPIDJSON_TREAT_AS_SET(T)
Definition traits.h:31
#define REFLECTIVE_RAPIDJSON_TREAT_AS_MAP_OR_HASH(T)
Definition traits.h:27