6
6
// NOLINTNEXTLINE
7
7
using namespace dynapse ;
8
8
9
+ class Foo {
10
+ public:
11
+ void Print () const { std::cout << " Hello, this is " << foo_ << " !" << ' \n ' ; }
12
+ [[nodiscard]] std::string Get () const { return foo_; }
13
+
14
+ // NOLINTNEXTLINE
15
+ int aaa = 114514 ;
16
+ // NOLINTNEXTLINE
17
+ float bbb = 123.5 ;
18
+ bool ccc = true ;
19
+
20
+ private:
21
+ std::string foo_ = " foo" ;
22
+ };
23
+
9
24
int main () {
10
25
// clang-format off
11
26
DYN_REFLECT_CLASS (String,
@@ -21,9 +36,48 @@ int main() {
21
36
}),
22
37
DYN_PROPERTY (V, " size" , make_any (GetReflect ().Construct (new int (0 ), " int" ))),
23
38
),
24
- )
39
+ );
40
+
41
+ DYN_REFLECT_CLASS (Foo,
42
+ DYN_CONSTRUCTOR ([](auto ) -> void * { return new Foo (); }),
43
+ DYN_DESTRUCTOR ([](void * ptr) { delete static_cast <Foo*>(ptr); }),
44
+ DYN_DECL_MEMBER_PROPS (
45
+ DYN_PROPERTY (RW, " aaa" , [](const Any& caller, const Args& args) -> Any {
46
+ void * aaa_ptr = new int (caller.As <Foo*>()->aaa );
47
+ return GetReflect ().Construct (aaa_ptr, " int" );
48
+ }, [](const Any& caller, const Args& args) -> Any {
49
+ std::cout << " Foo::aaa setter invoked!" << ' \n ' ;
50
+ caller.As <Foo*>()->aaa = args[0 ].To <int >();
51
+ return Any::Null ();
52
+ }),
53
+ DYN_PROPERTY (RW, " bbb" , [](const Any& caller, const Args& args) -> Any {
54
+ void * bbb_ptr = new float (caller.As <Foo*>()->bbb );
55
+ return GetReflect ().Construct (bbb_ptr, " float" );
56
+ }, [](const Any& caller, const Args& args) -> Any {
57
+ std::cout << " Foo::bbb setter invoked!" << ' \n ' ;
58
+ caller.As <Foo*>()->bbb = args[0 ].To <float >();
59
+ return Any::Null ();
60
+ }),
61
+ DYN_PROPERTY (RW, " ccc" , [](const Any& caller, const Args& args) -> Any {
62
+ void * ccc_ptr = new bool (caller.As <Foo*>()->ccc );
63
+ return GetReflect ().Construct (ccc_ptr, " bool" );
64
+ }, [](const Any& caller, const Args& args) -> Any {
65
+ std::cout << " Foo::ccc setter invoked!" << ' \n ' ;
66
+ caller.As <Foo*>()->ccc = args[0 ].To <bool >();
67
+ return Any::Null ();
68
+ }),
69
+ ),
70
+ DYN_DECL_MEMBER_FUNCS (
71
+ DYN_FUNCTION (" Print" , [](const Any& caller, const Args& args) -> Any {
72
+ caller.As <Foo*>()->Print ();
73
+ return Any::Null ();
74
+ }),
75
+ ),
76
+ );
25
77
// clang-format on
26
78
79
+ // Test `String`
80
+
27
81
auto reflect = GetReflect ();
28
82
auto string = reflect.Construct (" String" );
29
83
string.As <std::string*>()->append (" Hello, World!" );
@@ -38,5 +92,21 @@ int main() {
38
92
string[" size" ] = reflect.Construct (new int (10 ), " int" );
39
93
std::cout << " String.size: " << string[" size" ].To <int >() << ' \n ' ;
40
94
95
+ // Test `Foo`
96
+
97
+ auto foo = reflect.Construct (" Foo" );
98
+ foo[" Print" ]();
99
+ std::cout << " Foo.aaa: " << foo[" aaa" ].To <int >() << ' \n ' ;
100
+ std::cout << " typeof Foo.aaa: " << TypeOf (foo[" aaa" ]) << ' \n ' ;
101
+ // NOLINTNEXTLINE
102
+ foo[" aaa" ] = reflect.Construct (new int (1024 ), " int" );
103
+ std::cout << " Foo.aaa: " << foo[" aaa" ].To <int >() << ' \n ' ;
104
+
105
+ std::cout << " Foo.bbb: " << foo[" bbb" ].To <float >() << ' \n ' ;
106
+ std::cout << " typeof Foo.bbb: " << TypeOf (foo[" bbb" ]) << ' \n ' ;
107
+ // NOLINTNEXTLINE
108
+ foo[" bbb" ] = reflect.Construct (new float (1024.1024 ), " float" );
109
+ std::cout << " Foo.bbb: " << foo[" bbb" ].To <float >() << ' \n ' ;
110
+
41
111
return 0 ;
42
112
}
0 commit comments