-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.cpp
81 lines (70 loc) · 1.96 KB
/
test.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
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
#include <cppdynamic/dynamic.h>
#include <assert.h>
int main(){
{
// dynamic object can be a fundamental type
dynamic::DynamicObject uut = 33;
assert(33 == (int)uut);
}
{
// dynamic object can be a complex type
dynamic::DynamicObject uut = std::string("hello");
std::string res = uut;
assert("hello" == res);
}
{
// dynamic object can be reassigned
dynamic::DynamicObject uut = 33;
assert(33 == (int)uut);
uut = std::string("hello");
std::string res = uut;
assert("hello" == res);
}
{
//dynamic object can be a functor
dynamic::DynamicObject uut = [](int i, int j){return i + j; };
int result = uut(3, 4);
assert(result == 7);
}
{
// dynamic object can be an expando object
dynamic::DynamicObject uut;
uut["prop1"] = 33;
uut["prop2"] = std::string("hello");
uut["prop3"]["prop31"] = 5;
uut["prop4"] = [](int i, int j){return i + j; };
int prop1 = uut["prop1"];
std::string prop2 = uut["prop2"];
int prop31 = uut["prop3"]["prop31"];
int result = uut["prop4"](5, 6);
assert(prop1 == 33);
assert(prop2 == "hello");
assert(prop31 == 5);
assert(result == 11);
}
{
// you can create a custom dynamic object implementation:
class MyImp : public dynamic::DynamicObjectImplementationBase{
protected:
virtual bool tryMemberGet(const get_member_context & context, result_type & result)override
{
if (context.name == "prop1"){
result = 44;
return true;
}
if (context.name == "prop2"){
result = std::string("asd");
return true;
}
return false;
}
};
// initialization is still a bit complex
dynamic::DynamicObject uut =
std::dynamic_pointer_cast<dynamic::IDynamicObjectImplementation>(std::make_shared<MyImp>());
int a = uut["prop1"];
std::string b = uut["prop2"];
assert(a == 44);
assert(b == "asd");
}
}