-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTest.h
142 lines (118 loc) · 2.07 KB
/
Test.h
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//NOTE: intended to be included multiple times
struct Foo
{
Event<void(float)> evt;
};
struct Bar
{
void onEvt(float delta)
{
total += delta;
}
Listener<&Foo::evt, &Bar::onEvt> listener;
Bar(Foo& foo)
: listener(&foo, this)
{
}
volatile float total = 0.f;
};
Foo g_foo;
std::function<void(float)> g_func;
namespace ReentranceTest
{
struct Foo
{
Event<void(int count)> evt;
};
struct Bar
{
void onEvt(int count);
Listener<&Foo::evt, &Bar::onEvt> listener;
Foo* m_foo;
int x = 0;
bool flag;
Bar(Foo& foo, bool flag = true)
: listener(&foo, this)
, flag(flag)
{
m_foo = &foo;
}
};
std::vector<std::unique_ptr<Bar>> dyn_listeners;
void test()
{
dyn_listeners.clear();
Foo foo;
for (int i = 0, n = rand() % 10; i < n; i++)
dyn_listeners.emplace_back(new Bar(foo, false));
Bar bar(foo);
foo.evt(3);
assert(bar.x == 3);
}
inline void Bar::onEvt(int count)
{
std::cout << "Calling " << this << std::endl;
if (flag) {
if (count > 0) {
this->x++;
//purposely add or remove listeners
if (rand() & 1)
dyn_listeners.emplace_back(new Bar(*m_foo, false));
else if (!dyn_listeners.empty())
dyn_listeners.erase(dyn_listeners.begin() + rand() % dyn_listeners.size());
m_foo->evt(count - 1);
}
}
}
}
namespace MoveTest
{
void test()
{
Foo foo;
Bar bar(foo);
Bar bar2 = std::move(bar);
foo.evt(1);
assert(bar2.total == 1);
}
}
void test()
{
//basic usage
{
Foo foo;
Bar bar(foo);
foo.evt(3.14f);
assert(bar.total == 3.14f);
foo.evt(3.14f);
assert(bar.total == 6.28f);
}
//default construct
{
Listener<&Foo::evt, &Bar::onEvt> listener;
(void)listener;
}
//scope safety (listener)
{
Foo foo;
{
Bar bar1(foo), * bar3;
{
Bar bar2(foo);
bar3 = new Bar(foo);
foo.evt(1.f);
assert(bar1.total == 1);
assert(bar2.total == 1);
assert(bar3->total == 1);
}
foo.evt(1.f);
assert(bar1.total == 2);
assert(bar3->total == 2);
}
foo.evt(1.f);
}
//reentrance and modification safety
ReentranceTest::test();
//move
MoveTest::test();
}