-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest.cpp
More file actions
103 lines (87 loc) · 2.87 KB
/
test.cpp
File metadata and controls
103 lines (87 loc) · 2.87 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
#include "reflect_struct.h"
#include <iostream>
#include <string>
struct test_type0{
DEF_FIELD_BEGIN(test_type0)
DEF_FIELD(int, x)
DEF_FIELD(std::string, y)
DEF_FIELD_END
};
struct test_type1{
DEF_FIELD_BEGIN(test_type1)
DEF_FIELD(test_type0, z)
DEF_FIELD(std::string, w)
DEF_FIELD_END
};
struct iterate_output{
template<typename T>
void operator()(const T& arg, typename reflect_struct::not_reflect_struct<T>::type *p = 0) const{(void)p;
std::cout << arg << std::endl;
}
template<typename T>
void operator()(const T&, typename T::this_is_reflect_struct* p = 0 ) const{(void)p;
std::cout << std::endl;
}
};
struct iterate_output_with_name{
template<typename T>
void operator()(const T& arg, const char* name, typename reflect_struct::not_reflect_struct<T>::type *p = 0) const{(void)p;
std::cout << name <<" : " << arg << std::endl;
}
template<typename T>
void operator()(const T&, const char* name, typename T::this_is_reflect_struct* p = 0 ) const{(void)p;
std::cout << "reflect_struct : " << name << std::endl;
}
};
struct iterate_output_with_name_depth{
int current_depth;
iterate_output_with_name_depth():current_depth(-1){
}
void prefix_init(int depth){
if(this->current_depth > depth){
std::cout << "}, " ;
this->current_depth = depth;
}
else if(this->current_depth == depth){
std::cout << ", " ;
}
else{
this->current_depth = depth;
}
}
template<typename T>
void operator()(const T& arg, const char* name, int depth, typename reflect_struct::not_reflect_struct<T>::type *p = 0) {(void)p;
prefix_init(depth);
std::cout <<'"'<< name <<"\" : " << arg ;
}
void operator()(const std::string& arg, const char* name, int depth) {
prefix_init(depth);
std::cout <<'"'<< name <<"\" : \"" << arg <<'"' ;
}
template<typename T>
void operator()(const T&, const char* name, int depth, typename T::this_is_reflect_struct* p = 0) {(void)p;
prefix_init(depth);
std::cout <<'"'<< name <<"\" : {" ;
}
};
int main(){
iterate_output i1;
iterate_output_with_name i2;
iterate_output_with_name_depth i3;
test_type1 instance;
instance.z.x = 1;
instance.z.y = "2";
instance.w = "3";
std::cout << "show all the elements" << std::endl;
instance.iterate_all(i1);
std::cout << std::endl;
std::cout << "show all the elements with name" << std::endl;
instance.iterate_all_with_name(i2);
std::cout << std::endl;
std::cout << "show in json type" << std::endl;
std::cout << '{';
instance.iterate_all_with_name_depth(i3);
std::cout << '}';
std::cout << std::endl;
return 0;
}