- Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathserialize.cxx
54 lines (45 loc) · 1.1 KB
/
serialize.cxx
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
// Use introspect to iterate over each member of a class and
// stream its output.
#include<iostream>
#include<type_traits>
template<typenametype_t>
voidstream(std::ostream& os, consttype_t& obj) {
// @member_name won't work on non-class types, so check that here.
static_assert(std::is_class<type_t>::value, "stream requires class type");
// Stream the type name followed by the object name.
os<< @type_string(type_t)<< " {\n";
// Iterate over each member of type_t.
@meta for(int i = 0; i < @member_count(type_t); ++i) {
// Stream the member name and the member value.
os<< ""<<
@type_string(@member_type(type_t, i))<< ""<<
@member_name(type_t, i)<< ": "<<
<<'\"'<< @member_value(obj, i)<< "\"\n";
}
os<<"}\n";
}
structstruct1_t {
char c;
double d;
constchar* s;
};
structstruct2_t {
std::string string;
long l;
bool b;
};
intmain(int argc, char** argv) {
struct1_t one {
'X',
3.14159,
"A C string"
};
stream(std::cout, one);
struct2_t two {
"A C++ string",
42,
true
};
stream(std::cout, two);
return0;
}