-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
207 lines (176 loc) · 3.51 KB
/
example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <tuple>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <list>
#include <forward_list>
#include <memory>
#include <iostream>
#include "boost_fusion_sequence_jsonify.h"
using nlohmann::json;
enum Meh
{
EH,
FEH,
GEH,
};
// map Meh values to JSON as strings
NLOHMANN_JSON_SERIALIZE_ENUM( Meh, {
{EH, "EH"},
{FEH, "FEH"},
{GEH, "GEH"},
})
struct C
{
int bleh;
std::string breh;
Meh meh;
bool operator==(const C &rhs) const
{
return this->bleh == rhs.bleh && this->breh == rhs.breh && this->meh == rhs.meh;
}
bool operator!=(const C &rhs) const
{
return !(*this == rhs);
}
bool operator<(const C &rhs) const
{
return this->bleh < rhs.bleh;
}
bool operator>=(const C &rhs) const
{
return !(*this < rhs);
}
};
namespace std
{
template <>
struct hash<C>
{
size_t operator()(const C& c) const
{
// Compute individual hash values for two data members and combine them using XOR and bit shifting
return ((hash<int>()(c.bleh) ^ (hash<std::string>()(c.breh )) ^ (hash<Meh>()(c.meh) >> 1)));
}
};
}
BOOST_FUSION_ADAPT_STRUCT(
C,
bleh,
breh,
meh,
)
struct B
{
int a;
double b;
std::list<C> c;
};
BOOST_FUSION_ADAPT_STRUCT(
B,
a,
b,
c
)
struct A {
int x;
float y;
std::map<int, B> z_int_map;
std::tuple<std::string, B, C> z_tuple;
};
BOOST_FUSION_ADAPT_STRUCT(
A,
x,
y,
z_int_map,
z_tuple,
)
struct D {
std::set<C> c_set;
std::unordered_set<C> c_unordered_set;
std::unordered_map<int, C> c_unordered_map;
std::array<B, 2> b_arr;
std::forward_list<A> a_flist;
};
BOOST_FUSION_ADAPT_STRUCT(
D,
c_set,
c_unordered_set,
c_unordered_map,
b_arr,
a_flist,
)
struct E {
C *c_raw_ptr;
std::shared_ptr<C> c_shared_ptr;
std::unique_ptr<C> c_unique_ptr;
};
BOOST_FUSION_ADAPT_STRUCT(
E,
c_raw_ptr,
c_shared_ptr,
c_unique_ptr
)
BOOST_FUSION_SEQUENCE_JSONIFY()
int main()
{
// test smallest inner structure with an enumeration
C c { 1, "brehem", Meh::GEH };
json CToJson = c;
C CfromJson = CToJson.get<C>();
json CToJsonAgain = CfromJson;
// test nested structure with above structure embedded
B b {
666,
666.0,
{ c, c }
};
json BToJson = b;
B BfromJson = BToJson.get<B>();
json jB = b;
json BToJsonAgain = BfromJson;
A a {
1, // x
2, //y
{ // z_int_map
{
1, // int key
b // B which contains C
}
},
// z_tuple
std::make_tuple<std::string, B, C>(
"key1", // string key
B(b), // B which contains C
C(c) // c again cause why not
),
};
json AToJson = a;
A AfromJson = AToJson.get<A>();
json AToJsonAgain = AfromJson;
D d {
{c, c},
{c, c},
{ {1, c } },
{b, b},
{a, a, a}
};
json DToJson = d;
D DfromJson = DToJson.get<D>();
json DToJsonDagain = DfromJson;
std::cout << DToJson.dump(4) << std::endl;
E e {
new C(c),
std::make_shared<C>(c),
std::make_unique<C>(c)
};
json EToJson = e;
E EfromJson = EToJson.get<E>();
json EToJsonEagain = EfromJson;
std::cout << EToJson.dump(4) << std::endl;
delete e.c_raw_ptr;
delete EfromJson.c_raw_ptr;
return 0;
}