Skip to content

Commit 5114927

Browse files
committed
Redesign WIP. Removed reactor stuff. Fixed basic examples. Observers, algorithms. Event buffer clearing.
1 parent bd4c67a commit 5114927

22 files changed

Lines changed: 421 additions & 836 deletions

‎examples/CMakeLists.txt‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,6 @@ target_link_libraries(Example_BasicEvents CppReact)
1414
add_executable(Example_BasicObservers src/BasicObservers.cpp)
1515
target_link_libraries(Example_BasicObservers CppReact)
1616

17-
### Example_BasicReactors
18-
find_package(Boost 1.55 COMPONENTS coroutine context system)
19-
if(Boost_FOUND)
20-
include_directories(${Boost_INCLUDE_DIRS})
21-
add_executable(Example_BasicReactors src/BasicReactors.cpp)
22-
target_link_libraries(Example_BasicReactors CppReact ${Boost_LIBRARIES})
23-
else()
24-
message("boost::coroutine not found. Skipping build of Example_BasicReactors.")
25-
endif()
26-
#add_exyecutable(Example_BasicReactors src/BasicReactors.cpp)
27-
#target_link_libraries(Example_BasicReactors CppReact)
28-
2917
### Example_BasicSignals
3018
add_executable(Example_BasicSignals src/BasicSignals.cpp)
3119
target_link_libraries(Example_BasicSignals CppReact)

‎examples/src/BasicAlgorithms.cpp‎

Lines changed: 118 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <utility>
1010
#include <vector>
1111

12-
#include "react/Domain.h"
1312
#include "react/Signal.h"
1413
#include "react/Event.h"
1514
#include "react/Observer.h"
@@ -23,15 +22,12 @@ namespace example1
2322
using namespace std;
2423
using namespace react;
2524

26-
REACTIVE_DOMAIN(D, sequential)
25+
ReactiveGroup<> group;
2726

28-
class Sensor
27+
struct Sensor
2928
{
30-
public:
31-
USING_REACTIVE_DOMAIN(D)
32-
33-
EventSourceT<int> Samples = MakeEventSource<D,int>();
34-
SignalT<int> LastSample = Hold(Samples, 0);
29+
EventSource<int> samples { group };
30+
Signal<int> lastSample = Hold(0, samples);
3531
};
3632

3733
void Run()
@@ -40,14 +36,18 @@ namespace example1
4036

4137
Sensor mySensor;
4238

43-
Observe(mySensor.LastSample, [] (int v) {
44-
std::cout << v << std::endl;
45-
});
39+
Observer<> obs(
40+
[] (int v)
41+
{
42+
cout << v << endl;
43+
},
44+
mySensor.lastSample);
4645

47-
mySensor.Samples << 20 << 21 << 21 << 22; // output: 20, 21, 22
46+
mySensor.samples << 20 << 21 << 21 << 22; // output: 20, 21, 22
4847

49-
DoTransaction<D>([&] {
50-
mySensor.Samples << 30 << 31 << 31 << 32;
48+
group.DoTransaction([&]
49+
{
50+
mySensor.samples << 30 << 31 << 31 << 32;
5151
}); // output: 32
5252

5353
cout << endl;
@@ -62,17 +62,12 @@ namespace example2
6262
using namespace std;
6363
using namespace react;
6464

65-
REACTIVE_DOMAIN(D, sequential)
65+
ReactiveGroup<> group;
6666

67-
class Employee
67+
struct Employee
6868
{
69-
public:
70-
USING_REACTIVE_DOMAIN(D)
71-
72-
VarSignalT<string> Name = MakeVar<D>(string( "Bob" ));
73-
VarSignalT<int> Salary = MakeVar<D>(3000);
74-
75-
EventsT<int> SalaryChanged = Monitor(Salary);
69+
VarSignal<string> name { string( "Bob" ), group };
70+
VarSignal<int> salary { 3000, group };
7671
};
7772

7873
void Run()
@@ -81,12 +76,13 @@ namespace example2
8176

8277
Employee bob;
8378

84-
Observe(
85-
bob.SalaryChanged,
86-
With(bob.Name),
87-
[] (int newSalary, const string& name) {
88-
cout << name << " now earns " << newSalary << endl;
89-
});
79+
Observer<> obs(
80+
[] (EventRange<int> in, const string& name)
81+
{
82+
for (int newSalary : in)
83+
cout << name << " now earns " << newSalary << endl;
84+
},
85+
Monitor(bob.salary), bob.name);
9086

9187
cout << endl;
9288
}
@@ -100,21 +96,21 @@ namespace example3
10096
using namespace std;
10197
using namespace react;
10298

103-
REACTIVE_DOMAIN(D, sequential)
99+
ReactiveGroup<> group;
104100

105-
class Counter
101+
struct Counter
106102
{
107-
public:
108-
USING_REACTIVE_DOMAIN(D)
103+
EventSource<> increment { group };
109104

110-
EventSourceT<> Increment = MakeEventSource<D>();
111-
112-
SignalT<int> Count = Iterate(
113-
Increment,
105+
Signal<int> count = Iterate<int>(
114106
0,
115-
[] (Token, int oldCount) {
116-
return oldCount + 1;
117-
});
107+
[] (EventRange<> in, int count)
108+
{
109+
for (auto _ : in)
110+
++count;
111+
return count;
112+
},
113+
increment);
118114
};
119115

120116
void Run()
@@ -123,12 +119,11 @@ namespace example3
123119

124120
Counter myCounter;
125121

126-
// Note: Using function-style operator() instead of .Emit() and .Value()
127-
myCounter.Increment();
128-
myCounter.Increment();
129-
myCounter.Increment();
122+
myCounter.increment.Emit();
123+
myCounter.increment.Emit();
124+
myCounter.increment.Emit();
130125

131-
cout << myCounter.Count() << endl; // output: 3
126+
cout << myCounter.count.Value() << endl; // output: 3
132127

133128
cout << endl;
134129
}
@@ -142,34 +137,33 @@ namespace example4
142137
using namespace std;
143138
using namespace react;
144139

145-
REACTIVE_DOMAIN(D, sequential)
140+
ReactiveGroup<> group;
146141

147-
class Sensor
142+
struct Sensor
148143
{
149-
public:
150-
USING_REACTIVE_DOMAIN(D)
151-
152-
EventSourceT<float> Input = MakeEventSource<D,float>();
144+
EventSource<float> input{ group };
153145

154-
SignalT<int> Count = Iterate(
155-
Tokenize(Input),
146+
Signal<int> count = Iterate<int>(
156147
0,
157-
[] (Token, int oldCount) {
158-
return oldCount + 1;
159-
});
160-
161-
SignalT<float> Sum = Iterate(
162-
Input,
148+
[] (EventRange<float> in, int count)
149+
{
150+
for (auto _ : in)
151+
count++;
152+
return count;
153+
},
154+
input);
155+
156+
Signal<float> sum = Iterate<float>(
163157
0.0f,
164-
[] (float v, float sum) {
165-
return v + sum;
166-
});
167-
168-
SignalT<float> Average = MakeSignal(
169-
With(Sum,Count),
170-
[] (float sum, int count) {
171-
return count != 0 ? sum / count : 0.0f;
172-
});
158+
[] (EventRange<float> in, float sum)
159+
{
160+
for (auto v : in)
161+
sum += v;
162+
return sum;
163+
},
164+
input);
165+
166+
VarSignal<float> average{ group };
173167
};
174168

175169
void Run()
@@ -178,9 +172,9 @@ namespace example4
178172

179173
Sensor mySensor;
180174

181-
mySensor.Input << 10.0f << 5.0f << 10.0f << 8.0f;
175+
mySensor.input << 10.0f << 5.0f << 10.0f << 8.0f;
182176

183-
cout << "Average: " << mySensor.Average() << endl; // output: 8.25
177+
cout << "Average: " << mySensor.average.Value() << endl; // output: 8.25
184178

185179
cout << endl;
186180
}
@@ -194,31 +188,35 @@ namespace example5
194188
using namespace std;
195189
using namespace react;
196190

197-
REACTIVE_DOMAIN(D, sequential)
191+
ReactiveGroup<> group;
198192

199193
enum ECmd { increment, decrement, reset };
200194

201195
class Counter
202196
{
203-
public:
204-
USING_REACTIVE_DOMAIN(D)
205-
206-
EventSourceT<int> Update = MakeEventSource<D,int>();
207-
VarSignalT<int> Delta = MakeVar<D>(1);
208-
VarSignalT<int> Start = MakeVar<D>(0);
209-
210-
SignalT<int> Count = Iterate(
211-
Update,
212-
Start.Value(),
213-
With(Delta, Start),
214-
[] (int cmd, int oldCount, int delta, int start) {
197+
private:
198+
static int DoCounterLoop(EventRange<int> in, int count, int delta, int start)
199+
{
200+
for (int cmd : in)
201+
{
215202
if (cmd == increment)
216-
return oldCount + delta;
203+
count += delta;
217204
else if (cmd == decrement)
218-
return oldCount - delta;
205+
count -= delta;
219206
else
220-
return start;
221-
});
207+
count = start;
208+
}
209+
210+
return count;
211+
}
212+
213+
public:
214+
EventSource<int> update{ group };
215+
216+
VarSignal<int> delta{ 1, group };
217+
VarSignal<int> start{ 0, group };
218+
219+
Signal<int> count{ Iterate<int>(start.Value(), DoCounterLoop, update, delta, start) };
222220
};
223221

224222
void Run()
@@ -227,23 +225,23 @@ namespace example5
227225

228226
Counter myCounter;
229227

230-
cout << "Start: " << myCounter.Count() << endl; // output: 0
228+
cout << "Start: " << myCounter.count.Value() << endl; // output: 0
231229

232-
myCounter.Update(increment);
233-
myCounter.Update(increment);
234-
myCounter.Update(increment);
230+
myCounter.update.Emit(increment);
231+
myCounter.update.Emit(increment);
232+
myCounter.update.Emit(increment);
235233

236-
cout << "3x increment by 1: " << myCounter.Count() << endl; // output: 3
234+
cout << "3x increment by 1: " << myCounter.count.Value() << endl; // output: 3
237235

238-
myCounter.Delta <<= 5;
239-
myCounter.Update(decrement);
236+
myCounter.delta <<= 5;
237+
myCounter.update.Emit(decrement);
240238

241-
cout << "1x decrement by 5: " << myCounter.Count() << endl; // output: -2
239+
cout << "1x decrement by 5: " << myCounter.count.Value() << endl; // output: -2
242240

243-
myCounter.Start <<= 100;
244-
myCounter.Update(reset);
241+
myCounter.start <<= 100;
242+
myCounter.update.Emit(reset);
245243

246-
cout << "reset to 100: " << myCounter.Count() << endl; // output: 100
244+
cout << "reset to 100: " << myCounter.count.Value() << endl; // output: 100
247245

248246
cout << endl;
249247
}
@@ -257,32 +255,32 @@ namespace example6
257255
using namespace std;
258256
using namespace react;
259257

260-
REACTIVE_DOMAIN(D, sequential)
258+
ReactiveGroup<> group;
261259

262260
class Sensor
263261
{
264-
public:
265-
USING_REACTIVE_DOMAIN(D)
266-
267-
EventSourceT<int> Input = MakeEventSource<D,int>();
268-
269-
VarSignalT<int> Threshold = MakeVar<D>(42);
270-
271-
SignalT<vector<int>> AllSamples = Iterate(
272-
Input,
273-
vector<int>{},
274-
[] (int input, vector<int>& all) {
262+
private:
263+
static void DoIterateAllSamples(EventRange<int> in, vector<int>& all)
264+
{
265+
for (int input : in)
275266
all.push_back(input);
276-
});
267+
}
277268

278-
SignalT<vector<int>> CriticalSamples = Iterate(
279-
Input,
280-
vector<int>{},
281-
With(Threshold),
282-
[] (int input, vector<int>& critical, int threshold) {
269+
static void DoIterateCritSamples(EventRange<int> in, vector<int>& critical, int threshold)
270+
{
271+
for (int input : in)
283272
if (input > threshold)
284273
critical.push_back(input);
285-
});
274+
}
275+
276+
public:
277+
EventSource<int> input{ group };
278+
279+
VarSignal<int> threshold{ 42, group };
280+
281+
Signal<vector<int>> allSamples{ Iterate<vector<int>>(vector<int>{ }, DoIterateAllSamples, input) };
282+
283+
Signal<vector<int>> criticalSamples{ Iterate<vector<int>>(vector<int>{ }, DoIterateCritSamples, input, threshold) };
286284
};
287285

288286
void Run()
@@ -291,15 +289,15 @@ namespace example6
291289

292290
Sensor mySensor;
293291

294-
mySensor.Input << 40 << 29 << 43 << 50;
292+
mySensor.input << 40 << 29 << 43 << 50;
295293

296294
cout << "All samples: ";
297-
for (auto const& v : mySensor.AllSamples())
295+
for (auto const& v : mySensor.allSamples.Value())
298296
cout << v << " ";
299297
cout << endl;
300298

301299
cout << "Critical samples: ";
302-
for (auto const& v : mySensor.CriticalSamples())
300+
for (auto const& v : mySensor.criticalSamples.Value())
303301
cout << v << " ";
304302
cout << endl;
305303

0 commit comments

Comments
 (0)