FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp.react/examples/src/BasicEvents.cpp at master · zengyuxing007/cpp.react · GitHub
zengyuxing007
/
cpp.react
Public
forked from
snakster/cpp.react
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp.react
/
examples
/
src
/
BasicEvents.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
268 lines (201 loc) · 6.96 KB
Breadcrumbs
cpp.react
/
examples
/
src
/
BasicEvents.cpp
Copy path
File metadata and controls
268 lines (201 loc) · 6.96 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
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
<
iostream
>
#
include
<
string
>
#
include
<
utility
>
#
include
"
react/Domain.h
"
#
include
"
react/Event.h
"
#
include
"
react/Observer.h
"
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ Example 1 - Hello world
//
/////////////////////////////////////////////////////////////////////////////////////////////////
namespace
example1
{
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, sequential)
//
Define type aliases for the given domain in this namespace.
//
Now we can use EventSourceT instead of D::EventSourceT.
USING_REACTIVE_DOMAIN
(D)
//
An event source that emits values of type string
namespace
v1
{
EventSourceT<string> mySource = MakeEventSource<D,string>();
void
Run
()
{
cout <<
"
Example 1 - Hello world (string source)
"
<< endl;
Observe
(mySource, [] (
const
string& s) {
std::cout << s << std::endl;
});
mySource <<
string
(
"
Hello world #1
"
);
//
Or without the operator:
mySource.
Emit
(
string
(
"
Hello world #2
"
));
//
Or as a function call:
mySource
(
string
(
"
Hello world #3
"
));
cout << endl;
}
}
//
An event source without an explicit value type
namespace
v2
{
EventSourceT<> helloWorldTrigger = MakeEventSource<D>();
void
Run
()
{
cout <<
"
Example 1 - Hello world (token source)
"
<< endl;
int
count =
0
;
Observe
(helloWorldTrigger, [&] (Token) {
cout <<
"
Hello world #
"
<< ++count << endl;
});
helloWorldTrigger.
Emit
();
//
Or without the stream operator:
helloWorldTrigger << Token::value;
//
Or as a function call:
helloWorldTrigger
();
cout << endl;
}
}
}
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ Example 2 - Merging event streams
//
/////////////////////////////////////////////////////////////////////////////////////////////////
namespace
example2
{
using
namespace
std
;
using
namespace
react
;
REACTIVE_DOMAIN
(D, sequential)
USING_REACTIVE_DOMAIN
(D)
//
An event stream that merges both sources
namespace
v1
{
EventSourceT<> leftClick = MakeEventSource<D>();
EventSourceT<> rightClick = MakeEventSource<D>();
EventsT<> anyClick = Merge(leftClick, rightClick);
void
Run
()
{
cout <<
"
Example 2 - Merging event streams (Merge)
"
<< endl;
int
count =
0
;
Observe
(anyClick, [&] (Token) {
cout <<
"
clicked #
"
<< ++count << endl;
});
leftClick.
Emit
();
//
output: clicked #1
rightClick.
Emit
();
//
output: clicked #2
cout << endl;
}
}
//
Using overloaded operator | instead of explicit Merge
namespace
v2
{
EventSourceT<> leftClick = MakeEventSource<D>();
EventSourceT<> rightClick = MakeEventSource<D>();
EventsT<> anyClick = leftClick | rightClick;
void
Run
()
{
cout <<
"
Example 2 - Merging event streams (operator)
"
<< endl;
int
count =
0
;
Observe
(anyClick, [&] (Token) {
cout <<
"
clicked #
"
<< ++count << endl;
});
leftClick.
Emit
();
//
output: clicked #1
rightClick.
Emit
();
//
output: clicked #2
cout << endl;
}
}
}
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ Example 3 - Filtering events
//
/////////////////////////////////////////////////////////////////////////////////////////////////
namespace
example3
{
using
namespace
std
;
using
namespace
react
;
REACTIVE_DOMAIN
(D, sequential)
USING_REACTIVE_DOMAIN
(D)
EventSourceT<
int
> numbers = MakeEventSource<D,
int
>();
EventsT<
int
> greater10 = Filter(numbers, [] (
int
n) {
return
n >
10
;
});
void
Run
()
{
cout <<
"
Example 3 - Filtering events
"
<< endl;
Observe
(greater10, [] (
int
n) {
cout << n << endl;
});
numbers <<
5
<<
11
<<
7
<<
100
;
//
output: 11, 100
cout << endl;
}
}
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ Example 4 - Filtering events
//
/////////////////////////////////////////////////////////////////////////////////////////////////
namespace
example4
{
using
namespace
std
;
using
namespace
react
;
REACTIVE_DOMAIN
(D, sequential)
USING_REACTIVE_DOMAIN
(D)
//
Data types
enum
class
ETag
{ normal, critical };
using
TaggedNum = pair<ETag,
int
>;
EventSourceT<
int
> numbers = MakeEventSource<D,
int
>();
EventsT<TaggedNum> tagged = Transform(numbers, [] (
int
n) {
if
(n >
10
)
return
TaggedNum
( ETag::critical, n );
else
return
TaggedNum
( ETag::normal, n );
});
void
Run
()
{
cout <<
"
Example 4 - Transforming events
"
<< endl;
Observe
(tagged, [] (
const
TaggedNum& t) {
if
(t.
first
== ETag::critical)
cout <<
"
(critical)
"
<< t.
second
<< endl;
else
cout <<
"
(normal)
"
<< t.
second
<< endl;
});
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
;
REACTIVE_DOMAIN
(D, sequential)
USING_REACTIVE_DOMAIN
(D)
EventSourceT<
int
> src = MakeEventSource<D,
int
>();
void
Run
()
{
cout <<
"
Example 5 - Queuing multiple inputs
"
<< endl;
Observe
(src, [] (
int
v) {
cout << v << endl;
});
//
output: 1, 2, 3, 4
DoTransaction<D>([] {
src <<
1
<<
2
<<
3
;
src <<
4
;
});
cout << endl;
}
}
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ Run examples
//
/////////////////////////////////////////////////////////////////////////////////////////////////
int
main
()
{
example1::v1::Run
();
example1::v2::Run
();
example2::v1::Run
();
example2::v2::Run
();
example3::Run
();
example4::Run
();
example5::Run
();
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL