-
Notifications
You must be signed in to change notification settings - Fork 37
/
main.cpp
231 lines (162 loc) · 3.87 KB
/
main.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <chrono>
#include <boost/signals2.hpp>
#include "lsignal.h"
using lsignal::connection;
using lsignal::signal;
using lsignal::slot;
void print_sum(int x, int y)
{
std::cout << "sum(" << x << ", " << y << ") = " << (x + y) << "\n";
}
void print_mul(int x, int y)
{
std::cout << "mul(" << x << ", " << y << ") = " << (x * y) << "\n";
}
int pow2(int x)
{
return x * x;
}
int pow3(int x)
{
return x * x * x;
}
int sum_agg(const std::vector<int>& v)
{
return std::accumulate(v.cbegin(), v.cend(), 0);
}
void bar()
{
std::cout << "function: bar\n";
}
struct baz
{
void operator() ()
{
std::cout << "functor: baz\n";
}
};
struct qux
{
void print()
{
std::cout << "class member: qux\n";
}
};
struct demo
: public lsignal::slot
{
int value;
demo(int v) : value(v)
{
}
};
std::chrono::duration<double> get_performance(const std::function<void()>& fn)
{
std::chrono::time_point<std::chrono::steady_clock> start, end;
start = std::chrono::steady_clock::now();
fn();
end = std::chrono::steady_clock::now();
return end - start;
}
int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
// example 1
std::cout << "example #1:\n";
signal<void(int, int)> data;
data.connect(print_sum);
data.connect(print_mul);
data(3, 4);
// example 2
std::cout << "\nexample #2:\n";
signal<int(int)> worker;
worker.connect(pow2);
worker.connect(pow3);
std::cout << "last slot = " << worker(2) << "\n";
std::cout << "agg value = " << worker(2, sum_agg) << "\n";
// example 3
std::cout << "\nexample #3:\n";
signal<void()> news;
connection conn_one = news.connect([]() { std::cout << "news #1\n"; });
connection conn_two = news.connect([]() { std::cout << "news #2\n"; });
news.connect([]() { std::cout << "news #3\n"; });
std::cout << "(all connections)\n";
news();
std::cout << "(lock connection one)\n";
conn_one.set_lock(true);
news();
std::cout << "(disconnect connection two)\n";
conn_two.disconnect();
news();
// example 4
std::cout << "\nexample #4:\n";
signal<void()> dummy;
auto foo = []() { std::cout << "lambda: foo\n"; };
dummy.connect(foo);
dummy.connect(bar);
baz b;
qux q;
dummy.connect(b);
dummy.connect(&q, &qux::print);
dummy();
// example 5
std::cout << "\nexample #5:\n";
signal<void()> printer;
{
demo dm(42);
auto print_value = [&dm]() { std::cout << "value = " << dm.value << "\n"; };
printer.connect(std::move(print_value), &dm);
printer();
}
printer();
// example 6
std::cout << "\nexample #6:\n";
signal<void(int)> first;
signal<void(int)> second;
first.connect(&second);
second.connect([](int x)
{
std::cout << "x = " << x << "\n";
});
first(10);
// example 7
std::cout << "\nexample #7: slot\n";
signal<void()> sig7;
{
slot s;
// sig7.connect(bar, &s); // compile error
sig7.connect([](){ std::cout << "sig7\n"; }, &s);
sig7();
}
sig7();
// example 8
std::cout << "\nexample #8: disconnect_all\n";
signal<void()> sig8;
sig8.connect(bar);
sig8.connect(bar);
sig8.connect(bar);
sig8.disconnect_all();
sig8();
// check performance
lsignal::signal<void()> ls;
boost::signals2::signal_type<void(), boost::signals2::keywords::mutex_type<boost::signals2::dummy_mutex>>::type bs;
ls.connect([](){});
bs.connect([](){});
auto lsignal_func = [&ls]() { ls(); };
auto bsignal_func = [&bs]() { bs(); };
std::chrono::nanoseconds elapsed;
// lsignal performance
std::cout << "\nlsignal performance:\n";
elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(get_performance(lsignal_func));
std::cout << elapsed.count() << " ns\n";
// boost signal2 perfromance (with dummy mutex)
std::cout << "\nboost::signals2 performance:\n";
elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(get_performance(bsignal_func));
std::cout << elapsed.count() << " ns\n";
return 0;
}