Repository navigation
Expand file tree
/
Copy pathBasicEvents.cpp
More file actions
248 lines (195 loc) · 6.25 KB
/
Copy pathBasicEvents.cpp
File metadata and controls
248 lines (195 loc) · 6.25 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
// Copyright Sebastian Jeckel 2016.
// 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 <iostream>
#include <string>
#include <utility>
#include "react/Event.h"
#include "react/Observer.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Example 1 - Hello world
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example1
{
using namespace std;
using namespace react;
// Defines a group.
// Each group represents a separate dependency graph.
// Reactives from different groups can not be mixed.
ReactiveGroup group;
// An event source that emits values of type string
namespace v1
{
EventSource<string> mySource( group );
void Run()
{
cout << "Example 1 - Hello world (string source)" << endl;
Observer obs(
[] (EventRange<string> in)
{
for (const auto& s : in)
cout << s << std::endl;
},
mySource);
mySource << string("Hello world #1");
// Or without the operator:
mySource.Emit(string("Hello world #2"));
cout << endl;
}
}
// An event source without an explicit value type
namespace v2
{
EventSource<> helloWorldTrigger( group );
void Run()
{
cout << "Example 1 - Hello world (token source)" << endl;
int count = 0;
Observer obs(
[&] (EventRange<> in)
{
for (auto t : in)
cout << "Hello world #" << ++count << endl;
},
helloWorldTrigger);
helloWorldTrigger.Emit();
// Or without the stream operator:
helloWorldTrigger << Token::value;
cout << endl;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Example 2 - Merging event streams
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example2
{
using namespace std;
using namespace react;
ReactiveGroup group;
// An event stream that merges both sources
EventSource<> leftClick( group );
EventSource<> rightClick( group );
Event<> anyClick = Merge(leftClick, rightClick);
void Run()
{
cout << "Example 2 - Merging event streams (Merge)" << endl;
int count = 0;
Observer obs(
[&] (EventRange<> in)
{
for (auto t : in)
cout << "clicked #" << ++count << endl;
},
anyClick);
leftClick.Emit(); // output: clicked #1
rightClick.Emit(); // output: clicked #2
cout << endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Example 3 - Filtering events
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example3
{
using namespace std;
using namespace react;
ReactiveGroup group;
EventSource<int> numbers( group );
Event<int> greater10 = Filter([] (int n) { return n > 10; }, numbers);
void Run()
{
cout << "Example 3 - Filtering events" << endl;
Observer obs(
[&] (EventRange<int> in)
{
for (auto n : in)
cout << n << endl;
},
greater10);
numbers << 5 << 11 << 7 << 100; // output: 11, 100
cout << endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Example 4 - Filtering events
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example4
{
using namespace std;
using namespace react;
ReactiveGroup group;
// Data types
enum class Tag { normal, critical };
using TaggedNum = pair<Tag, int>;
EventSource<int> numbers( group );
Event<TaggedNum> tagged = Transform<TaggedNum>(
[] (int n)
{
if (n > 10)
return TaggedNum( Tag::critical, n );
else
return TaggedNum( Tag::normal, n );
},
numbers);
void Run()
{
cout << "Example 4 - Transforming events" << endl;
Observer obs(
[] (EventRange<TaggedNum> in)
{
for (TaggedNum e : in)
{
if (e.first == Tag::critical)
cout << "(critical) " << e.second << endl;
else
cout << "(normal) " << e.second << endl;
}
},
tagged);
numbers << 5; // output: (normal) 5
numbers << 20; // output: (critical) 20
cout << endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Example 5 - Queuing multiple inputs
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example5
{
using namespace std;
using namespace react;
ReactiveGroup group;
EventSource<int> src( group );
void Run()
{
cout << "Example 5 - Queuing multiple inputs" << endl;
Observer obs(
[] (EventRange<int> in)
{
for (int e : in)
cout << e << endl;
}, src);
// output: 1, 2, 3, 4
group.DoTransaction([]
{
src << 1 << 2 << 3;
src << 4;
});
cout << endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Run examples
///////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
example1::v1::Run();
example1::v2::Run();
example2::Run();
example3::Run();
example4::Run();
example5::Run();
return 0;
}