forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
403 lines (313 loc) · 8.85 KB
/
Main.cpp
File metadata and controls
403 lines (313 loc) · 8.85 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright Sebastian Jeckel 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <functional>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
//#define REACT_ENABLE_LOGGING
#include "react/Signal.h"
#include "react/EventStream.h"
#include "react/Algorithm.h"
#include "react/ReactiveObject.h"
//#include "react/engine/SubtreeEngine.h"
//#include "react/engine/PulseCountEngine.h"
using namespace std;
using namespace react;
// Defines a domain.
// Each domain represents a separate dependency graph, managed by a dedicated propagation engine.
// Reactives of different domains can not be combined.
REACTIVE_DOMAIN(D);
//REACTIVE_DOMAIN(D, PulseCountEngine<parallel>);
void SignalExample1()
{
cout << "Signal Example 1" << endl;
auto width = D::MakeVar(60);
auto height = D::MakeVar(70);
auto depth = D::MakeVar(8);
auto area = width * height;
auto volume = area * depth;
cout << "t0" << endl;
cout << "\tArea: " << area() << endl;
cout << "\tVolume: " << volume() << endl;
width <<= 90;
depth <<= 80;
cout << "t1" << endl;
cout << "\tArea: " << area() << endl;
cout << "\tVolume: " << volume() << endl;
cout << endl;
}
void SignalExample2()
{
cout << "Signal Example 2" << endl;
auto width = D::MakeVar(60);
auto height = D::MakeVar(70);
auto depth = D::MakeVar(8);
auto volume = (width,height,depth) ->* [] (int w, int h, int d) {
return w * h * d;
};
// Observe returns an observer handle, which can be used to detach the observer explicitly.
// This observer handle holds a shared_ptr to the subject, so as long as it exists,
// the subject will not be destroyed.
// The lifetime of the observer itself is tied to the subject.
Observe(volume, [] (int v) {
cout << "Volume changed to: " << v << endl;
});
D::DoTransaction([&] {
width <<= 90;
depth <<= 80;
});
cout << endl;
}
void SignalExample3()
{
cout << "Signal Example 3" << endl;
auto src = D::MakeVar(0);
// Input values can be manipulated imperatively in observers.
// Inputs are implicitly thread-safe, buffered and executed in a continuation turn.
// This continuation turn is queued just like a regular turn.
// If other turns are already queued, they are executed before the continuation.
Observe(src, [&] (int v) {
cout << "V: " << v << endl;
if (v < 10)
src <<= v+1;
});
src <<= 1;
cout << endl;
}
void EventExample1()
{
cout << "Event Example 1" << endl;
auto numbers1 = D::MakeEventSource<int>();
auto numbers2 = D::MakeEventSource<int>();
auto anyNumber = numbers1 | numbers2;
Observe(anyNumber, [] (int v) {
cout << "Number: " << v << endl;
});
numbers1 << 10 << 20 << 30;
numbers2 << 40 << 50 << 60;
cout << endl;
}
void EventExample2()
{
cout << "Event Example 2" << endl;
// The event type can be omitted if not required, in which case the event
// stream just indicates that it has fired, i.e. it behaves like a token stream.
auto emitter = D::MakeEventSource();
auto counter = Iterate(0, emitter, Incrementer<int>());
// In this case, the observer func must not declare a parameter for token streams.
Observe(emitter, [] {
cout << "Emitter fired!" << endl;
});
// Using .Emit() to fire rather than "<< value"
for (int i=0; i<5; i++)
emitter.Emit();
cout << "Counted " << counter() << " events" << endl;
cout << endl;
}
class Person : public ReactiveObject<D>
{
public:
VarSignalT<int> Age = MakeVar(1);
SignalT<int> Health = 100 - Age;
SignalT<int> Wisdom = Age * Age / 100;
// Note ICC compiler bug:
// Initializing them directly uses the same lambda for both signals
ObserverT wisdomObs;
ObserverT weaknessObs;
Person()
{
wisdomObs = Observe(Wisdom > 50, [] (bool isWise)
{
if (isWise) cout << "I'll do it next week!" << endl;
else cout << "I'll do it next month!" << endl;
});
weaknessObs = Observe(Health < 25, [] (bool isWeak)
{
if (isWeak) cout << ":<" << endl;
else cout << ":D" << endl;
});
}
bool operator==(const Person& other) const
{
return this == &other;
}
};
void ObjectExample1()
{
cout << "Object Example 1" << endl;
Person somePerson;
somePerson.Age <<= 30;
somePerson.Age <<= 60;
somePerson.Age <<= 90;
cout << "Health: " << somePerson.Health() << endl;
cout << "Wisdom: " << somePerson.Wisdom() << endl;
cout << endl;
}
class Company : public ReactiveObject<D>
{
public:
VarSignalT<string> Name;
Company(const char* name) :
Name{ MakeVar(string(name)) }
{
}
inline bool operator==(const Company& other) const
{
return this == &other;
}
};
class Manager : public ReactiveObject<D>
{
ObserverT nameObs;
public:
VarRefSignalT<Company> CurrentCompany;
Manager(Company& c) :
CurrentCompany{ MakeVar(std::ref(c)) }
{
nameObs = REACTIVE_REF(CurrentCompany, Name).Observe([] (string name) {
cout << "Manager: Now managing " << name << endl;
});
}
};
void ObjectExample2()
{
cout << "Object Example 2" << endl;
Company company1{ "Cellnet" };
Company company2{ "Borland" };
Manager manager{ company1 };
company1.Name <<= string("BT Cellnet");
company2.Name <<= string("Inprise");
manager.CurrentCompany <<= std::ref(company2);
company1.Name <<= string("O2");
company2.Name <<= string("Borland");
cout << endl;
}
void FoldExample1()
{
cout << "Fold Example 1" << endl;
auto src = D::MakeEventSource<int>();
auto fold1 = Fold(0, src, [] (int v, int d) {
return v + d;
});
for (auto i=1; i<=100; i++)
src << i;
cout << fold1() << endl;
auto charSrc = D::MakeEventSource<char>();
auto strFold = Fold(std::string(""), charSrc, [] (std::string s, char c) {
return s + c;
});
charSrc << 'T' << 'e' << 's' << 't';
cout << "Str: " << strFold() << endl;
}
//
//#include "tbb/tick_count.h"
//
//void Debug()
//{
// cout << "A" << endl;
// {
// int x = 0;
//
// auto t0 = tbb::tick_count::now();
//
// for (int i=0; i<10000000; i++)
// {
// x += i;
// }
//
// auto t1 = tbb::tick_count::now();
//
// auto d = (t1 - t0).seconds();
//
// //cout << x << endl;
// cout << d << endl;
// }
//
// cout << "B" << endl;
// {
// auto a = D::MakeVar(0);
// auto b = D::MakeVar(1);
// auto c = D::MakeVar(2);
// auto d = D::MakeVar(4);
// auto e = D::MakeVar(5);
//
// auto x = a + b + c + d + e;
//
// auto t0 = tbb::tick_count::now();
//
// for (int i=0; i<10000000; i++)
// a <<= i;
//
// auto t1 = tbb::tick_count::now();
//
// auto td = (t1 - t0).seconds();
//
// cout << x() << endl;
// cout << td << endl;
// }
//}
#ifndef REACT_DISABLE_REACTORS
#include "react/Reactor.h"
void LoopTest()
{
cout << "ReactiveLoop Example 1" << endl;
using PointT = pair<int,int>;
using PathT = vector<PointT>;
vector<PathT> paths;
auto mouseDown = D::MakeEventSource<PointT>();
auto mouseUp = D::MakeEventSource<PointT>();
auto mouseMove = D::MakeEventSource<PointT>();
D::ReactiveLoopT loop
{
[&] (D::ReactiveLoopT::Context& ctx)
{
PathT points;
points.emplace_back(ctx.Await(mouseDown));
ctx.RepeatUntil(mouseUp, [&] {
points.emplace_back(ctx.Await(mouseMove));
});
points.emplace_back(ctx.Await(mouseUp));
paths.push_back(points);
}
};
mouseDown << PointT(1,1);
mouseMove << PointT(2,2) << PointT(3,3) << PointT(4,4);
mouseUp << PointT(5,5);
mouseMove << PointT(999,999);
mouseDown << PointT(10,10);
mouseMove << PointT(20,20);
mouseUp << PointT(30,30);
for (const auto& path : paths)
{
cout << "Path: ";
for (const auto& point : path)
cout << "(" << point.first << "," << point.second << ") ";
cout << endl;
}
}
#endif //REACT_DISABLE_REACTORS
int main()
{
SignalExample1();
SignalExample2();
SignalExample3();
EventExample1();
EventExample2();
ObjectExample1();
ObjectExample2();
FoldExample1();
#ifndef REACT_DISABLE_REACTORS
LoopTest();
#endif
//Debug();
#ifdef REACT_ENABLE_LOGGING
std::ofstream logfile;
logfile.open("log.txt");
D::Log().Write(logfile);
logfile.close();
#endif
return 0;
}