forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicSignals.cpp
More file actions
228 lines (174 loc) · 6.36 KB
/
BasicSignals.cpp
File metadata and controls
228 lines (174 loc) · 6.36 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
// Copyright Sebastian Jeckel 2017.
// 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/state.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
StateVar<string> firstWord = StateVar<string>::Create(group, string("Change"));
StateVar<string> secondWord = StateVar<string>::Create(group, string("me!"));
// A signal that concatenates both words
State<string> bothWords = State<string>::Create(ConcatFunc, firstWord, secondWord);
void Run()
{
auto obs = Observer::Create(PrintFunc, bothWords);
cout << "Example 1 - Hello world" << endl;
firstWord.Set(string("Hello"));
secondWord.Set(string("World"));
cout << endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Example 2 - Reacting to value changes
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example2
{
using namespace std;
using namespace react;
Group group;
StateVar<int> x = StateVar<int>::Create(group, 1);
State<int> xAbs = State<int>::Create([] (int v) { return abs(v); }, x);
void Run()
{
cout << "Example 2 - Reacting to value changes" << endl;
auto obs = Observer::Create([] (int newValue)
{ cout << "xAbs changed to " << newValue << endl; }, xAbs);
// initially x is 1
x.Set(2); // output: xAbs changed to 2
x.Set(-3); // output: xAbs changed to 3
x.Set(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;
StateVar<int> a = StateVar<int>::Create(group, 1);
StateVar<int> b = StateVar<int>::Create(group, 1);
State<int> x = State<int>::Create(sumFunc, a, b);
State<int> y = State<int>::Create(sumFunc, a, b);
State<int> z = State<int>::Create(sumFunc, x, y);
void Run()
{
cout << "Example 3 - Changing multiple inputs" << endl;
auto obs = Observer::Create([] (int newValue)
{ cout << "z changed to " << newValue << endl; }, z);
a.Set(2); // output: z changed to 6
b.Set(2); // output: z changed to 8
group.DoTransaction([&]
{
a.Set(4);
b.Set(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;
StateVar<vector<string>> data = StateVar<vector<string>>::Create(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"); });
auto obs = Observer::Create([] (const vector<string>& data)
{
for (const auto& s : data)
cout << s << " ";
}, data);
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
StateVar<int> a = StateVar<int>::Create(group, 1);
StateVar<int> b = StateVar<int>::Create(group, 2);
// The expression vector
State<ExprVectType> expressions = State<ExprVectType>::Create([] (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;
auto obs = Observer::Create(PrintExpressions, expressions);
a.Set(50);
b.Set(60);
cout << endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Run examples
///////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
example1::Run();
example2::Run();
example3::Run();
example4::Run();
example5::Run();
return 0;
}