-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtest.cpp
109 lines (88 loc) · 2.72 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <string>
#include <map>
#include "object/object.hpp"
#include "object/objectptr.hpp"
#include "object/reflect.hpp"
#include "type/type.hpp"
#include "object/composite_type.hpp"
#include "object/struct_type.hpp"
#include "base/array_type.hpp"
#include "serialization/json_archive.hpp"
#include "object/universe.hpp"
#include "base/maybe.hpp"
#include "base/maybe_type.hpp"
#include "type/type_registry.hpp"
#include "object/child_list.hpp"
struct Scene : Object {
REFLECT;
ChildList children;
};
BEGIN_TYPE_INFO(Scene)
property(&Scene::children, "Children", "The objects of the scene.");
END_TYPE_INFO()
struct Foo : Object {
REFLECT;
Maybe<int> foo;
void a_signal_receiver(int n) { std::cout << "Foo::a_signal_receiver: " << n << "\n"; an_empty_signal(); }
Signal<int32> after_signal_received;
Signal<> an_empty_signal;
Foo() : foo(123) {}
};
BEGIN_TYPE_INFO(Foo)
description("Foo is a class.");
property(&Foo::foo, "foo", "A number.");
slot(&Foo::a_signal_receiver, "Signal receiver", "It's a slot.");
signal(&Foo::after_signal_received, "After signal received", "It's a signal.");
signal(&Foo::an_empty_signal, "Empty signal", "blah blah");
END_TYPE_INFO()
struct Bar : Object {
REFLECT;
int bar;
Array<int> list;
ObjectPtr<Foo> foo;
Bar() : bar(456), foo(nullptr) {}
~Bar() {
std::cout << "~Bar " << this << '\n';
}
Signal<int32> when_something_happens;
};
BEGIN_TYPE_INFO(Bar)
description("Bar is a class.");
property(&Bar::bar, "bar", "Another number");
property(&Bar::list, "list", "A list of numbers");
property(&Bar::foo, "foo", "A reference to a foo");
signal(&Bar::when_something_happens, "when_something_happens", "La la la");
END_TYPE_INFO()
int main (int argc, char const *argv[])
{
TypeRegistry::add<Scene>();
TypeRegistry::add<Object>();
TypeRegistry::add<Foo>();
TypeRegistry::add<Bar>();
TestUniverse universe;
auto t = new CompositeType("FooBar", get_type<Scene>());
t->add_aspect(get_type<Foo>());
t->add_aspect(get_type<Bar>());
t->freeze();
ObjectPtr<> p = universe.create_object(t, "Composite FooBar");
ObjectPtr<Bar> b = universe.create<Bar>("Bar");
ObjectPtr<Scene> scene = p.cast<Scene>();
scene->children.push_back(b);
ObjectPtr<Bar> bar = aspect_cast<Bar>(p);
ObjectPtr<Foo> foo = aspect_cast<Foo>(p);
bar->foo = foo;
bar->when_something_happens.connect(foo, &Foo::a_signal_receiver);
bar->when_something_happens(bar->bar);
JSONArchive json;
json.serialize(p, universe);
json.write(std::cout);
TestUniverse universe2;
ObjectPtr<> root = json.deserialize(universe2);
ObjectPtr<Bar> bar2 = aspect_cast<Bar>(root);
bar2->when_something_happens(bar2->bar);
JSONArchive json2;
json2.serialize(root, universe2);
json2.write(std::cout);
return 0;
}