Repository navigation
Expand file tree
/
Copy pathBasicSignals.cpp
More file actions
227 lines (174 loc) · 5.91 KB
/
Copy pathBasicSignals.cpp
File metadata and controls
227 lines (174 loc) · 5.91 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
// 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 <cmath>
#include <functional>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include "react/Signal.h"
#include "react/Observer.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Example 1 - Hello world
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example1
{
using namespace std;
using namespace react;
// The concat function
string ConcatFunc(string first, string second)
{ return first + string(" ") + second; }
void PrintFunc(const string& s)
{ cout << s << endl; }
// Defines a group.
// Each group represents a separate dependency graph.
// Reactives from different groups can not be mixed.
Group group;
// The two words
VarSignal<string> firstWord( group, string("Change") );
VarSignal<string> secondWord( group, string("me!") );
// A signal that concatenates both words
Signal<string> bothWords(ConcatFunc, firstWord, secondWord);
void Run()
{
Observer obs{ PrintFunc, bothWords };
cout << "Example 1 - Hello world" << endl;
firstWord <<= string("Hello");
secondWord <<= string("World");
cout << endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Example 2 - Reacting to value changes
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example2
{
using namespace std;
using namespace react;
Group group;
VarSignal<int> x( group, 1 );
Signal<int> xAbs( [] (int v) { return abs(v); }, x);
void Run()
{
cout << "Example 2 - Reacting to value changes" << endl;
Observer obs(
[] (int newValue) { cout << "xAbs changed to " << newValue << endl; },
xAbs );
// initially x is 1
x <<= 2; // output: xAbs changed to 2
x <<= -3; // output: xAbs changed to 3
x <<= 3; // no output, xAbs is still 3
cout << endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Example 3 - Changing multiple inputs
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example3
{
using namespace std;
using namespace react;
int sumFunc(int a, int b)
{ return a + b; }
Group group;
VarSignal<int> a( group, 1 );
VarSignal<int> b( group, 1 );
Signal<int> x( sumFunc, a, b );
Signal<int> y( sumFunc, a, b );
Signal<int> z( sumFunc, x, y );
void Run()
{
cout << "Example 3 - Changing multiple inputs" << endl;
Observer obs(
[] (int newValue) { cout << "z changed to " << newValue << endl; },
z );
a <<= 2; // output: z changed to 6
b <<= 2; // output: z changed to 8
group.DoTransaction([&]
{
a <<= 4;
b <<= 4;
}); // output: z changed to 16
cout << endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Example 4 - Modifying signal values in place
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example4
{
using namespace std;
using namespace react;
Group group;
VarSignal<vector<string>> data( group );
void Run()
{
cout << "Example 4 - Modifying signal values in place" << endl;
data.Modify([] (vector<string>& data)
{ data.push_back("Hello"); });
data.Modify([] (vector<string>& data)
{ data.push_back("World"); });
// for (const auto& s : data.Value())
// cout << s << " ";
cout << endl;
// output: Hello World
cout << endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Example 5 - Complex signals
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example5
{
using namespace std;
using namespace react;
Group group;
// Helpers
using ExprPairType = pair<string, int>;
using ExprVectType = vector<ExprPairType>;
string MakeExprStr(int a, int b, const char* op)
{
return to_string(a) + string(op) + to_string(b);
}
void PrintExpressions(const ExprVectType& expressions)
{
cout << "Expressions: " << endl;
for (const auto& p : expressions)
cout << "\t" << p.first << " is " << p.second << endl;
}
// Input operands
VarSignal<int> a( group, 1 );
VarSignal<int> b( group, 2 );
// The expression vector
Signal<ExprVectType> expressions(
[] (int a, int b)
{
ExprVectType result;
result.push_back(make_pair(MakeExprStr(a, b, "+"), a + b));
result.push_back(make_pair(MakeExprStr(a, b, "-"), a - b));
result.push_back(make_pair(MakeExprStr(a, b, "*"), a * b));
return result;
}, a, b );
void Run()
{
cout << "Example 5 - Complex signals (v3)" << endl;
Observer obs(PrintExpressions, expressions);
a <<= 50;
b <<= 60;
cout << endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Run examples
///////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
example1::Run();
example2::Run();
example3::Run();
example4::Run();
example5::Run();
return 0;
}