-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.cpp
47 lines (38 loc) · 1.04 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
/*
* >= C++11 Reflection
*
* - Daniel Bloemendal
*/
#include <tuple>
#include <type_traits>
#include <string>
#include <iostream>
#include "reflection.hpp"
template<typename Klass>
class example_serializer {
Klass& _instance;
public:
example_serializer(Klass& instance) : _instance(instance) {}
template<typename T>
void operator()(const field<Klass, T>& field) {
std::cout << field.name << " (" << field.caption << "): " << (&_instance)->*field.pointer << std::endl;
}
void serialize() {
for_each(meta<Klass>::fields(), *this);
}
};
class example_class {
public:
int integer_field;
float float_field;
std::string string_field;
};
REFLECT_REGISTER(example_class,
REFLECT_FIELD(integer_field, "an integer field"),
REFLECT_FIELD(float_field, "a floating point field"),
REFLECT_FIELD(string_field, "a string field"))
int main() {
example_class ex{1, 1.5, "hello world"};
example_serializer<example_class> s(ex);
s.serialize();
}