-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsource.cpp
More file actions
140 lines (111 loc) · 3.17 KB
/
source.cpp
File metadata and controls
140 lines (111 loc) · 3.17 KB
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
#include "Headers.h"
// Function declaration
json ToJson(rttr::instance obj, const char * name) ;
using namespace rttr;
enum class color
{
red,
green,
blue
};
struct point2d
{
point2d() {}
point2d(int x_, int y_) : x(x_), y(y_) {}
int x = 0;
int y = 0;
};
struct shape
{
shape(std::string n) : name(n) {}
shape() = default;
void set_visible(bool v) { visible = v; }
bool get_visible() const { return visible; }
color color_ = color::blue;
std::string name = "";
point2d position;
std::map<color, point2d> dictionary;
RTTR_ENABLE()
private:
bool visible = false;
};
struct circle : shape
{
circle(std::string n) : shape(n) {}
circle() = default;
double radius = 5.2;
std::vector<point2d> points;
int no_serialize = 100;
RTTR_ENABLE(shape)
};
struct Container
{
std::unordered_map<int, circle> map;
std::vector<int> array = {10,1};
RTTR_ENABLE()
};
RTTR_REGISTRATION
{
rttr::registration::class_<shape>("shape")
.property("visible", &shape::get_visible, &shape::set_visible)
.property("color", &shape::color_)
.property("name", &shape::name)
.property("position", &shape::position)
.property("dictionary", &shape::dictionary)
;
rttr::registration::class_<circle>("circle")
.property("radius", &circle::radius)
.property("points", &circle::points)
.property("no_serialize", &circle::no_serialize)
(
metadata("NO_SERIALIZE", true)
)
;
rttr::registration::class_<point2d>("point2d")
.constructor()(rttr::policy::ctor::as_object)
.property("x", &point2d::x)
.property("y", &point2d::y)
;
rttr::registration::enumeration<color>("color")
(
value("red", color::red),
value("blue", color::blue),
value("green", color::green)
);
rttr::registration::class_<Container>("circle")
.property("Hash Map", &Container::map)
.property("Array", &Container::array)
;
}
int main()
{
std::string json_string;
Container map;
{
circle c_1("Circle #1");
shape& my_shape = c_1;
c_1.set_visible(true);
c_1.points = std::vector<point2d>(2, point2d(1, 1));
c_1.points[1].x = 23;
c_1.points[1].y = 42;
c_1.position.x = 12;
c_1.position.y = 66;
c_1.radius = 5.123;
c_1.color_ = color::red;
// additional braces are needed for a VS 2013 bug
c_1.dictionary = { { {color::green, {1, 2} }, {color::blue, {3, 4} }, {color::red, {5, 6} } } };
c_1.no_serialize = 12345;
json_string = ToJson(my_shape, "Circle 1").dump(4); // serialize the circle to 'json_string'
map.map[0] = c_1;
}
std::cout << json_string << std::endl;
circle c_2("Circle #2"); // create a new empty circle
map.map[1] = c_2;
// from_json(json_string, c_2); // deserialize it with the content of 'c_1'
std::cout << "\n############################################\n" << std::endl;
std::cout << ToJson(c_2, "Circle 2").dump(4) << std::endl;
json_string = ToJson(map, "Map").dump(4);
std::cout << "Map:\n " << std::endl;
std::cout << json_string << std::endl;
return 0;
}