Skip to content

Commit a7ae030

Browse files
committed
Progress...
1 parent 5a98839 commit a7ae030

10 files changed

Lines changed: 888 additions & 189 deletions

File tree

examples/src/Main.cpp

Lines changed: 228 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,248 @@
44
// (See accompanying file LICENSE_1_0.txt or copy at
55
// http://www.boost.org/LICENSE_1_0.txt)
66

7-
//#define REACT_ENABLE_LOGGING
7+
#include <cstdio>
8+
#include <vector>
9+
#include <chrono>
10+
#include <thread>
811

9-
#include "react/Domain.h"
1012
#include "react/Signal.h"
1113
#include "react/Event.h"
1214
#include "react/Algorithm.h"
15+
#include "react/Observer.h"
1316

14-
#include "tbb/tick_count.h"
17+
using namespace react;
1518

16-
#include <chrono>
19+
template <typename T>
20+
T Multiply(T a, T b)
21+
{
22+
return a * b;
23+
}
1724

18-
using namespace std;
19-
using namespace react;
25+
template <typename T> void PrintValue(T v)
26+
{
27+
printf("Value: %d\n", v);
28+
}
29+
30+
template <typename T> void PrintArea(T v)
31+
{
32+
printf("Area: %d\n", v);
33+
}
34+
35+
template <typename T> void PrintVolume(T v)
36+
{
37+
printf("Volume: %d\n", v);
38+
}
2039

21-
void testme()
40+
template <typename T> void PrintEvents(EventRange<T> evts)
2241
{
23-
// Note: This project exists as a sandbox where I occasionally stage new examples.
24-
// Currently it's empty.
42+
printf("Processing events...\n");
43+
44+
for (const auto& e : evts)
45+
printf(" Event: %d\n", e);
46+
}
47+
48+
template <typename T> bool FilterFunc(T v)
49+
{
50+
return v > 10;
51+
}
52+
53+
int main2()
54+
{
55+
ReactiveGroup<> group;
56+
57+
{
58+
// Signals
59+
VarSignal<int> x{ group, 0 };
60+
VarSignal<int> y{ group, 0 };
61+
VarSignal<int> z{ group, 0 };
62+
63+
Signal<int> area{ Multiply<int>, x, y };
64+
Signal<int> volume{ Multiply<int>, area, z };
65+
66+
Observer<> areaObs{ PrintArea<int>, area };
67+
Observer<> volumeObs{ PrintVolume<int>, volume };
68+
69+
x.Set(2); // a: 0, v: 0
70+
y.Set(2); // a: 4, v: 0
71+
z.Set(2); // a: 4, v: 8
72+
73+
group.DoTransaction([&]
74+
{
75+
x <<= 100;
76+
y <<= 3;
77+
y <<= 4;
78+
});
79+
80+
// a: 400, v: 800
81+
}
82+
83+
{
84+
// Events
85+
EventSource<int> button1{ group };
86+
EventSource<int> button2{ group };
87+
88+
Event<int> anyButton = Merge(button1, button2);
89+
Event<int> filtered = Filter(FilterFunc<int>, anyButton);
90+
91+
Observer<> eventObs{ PrintEvents<int>, anyButton };
92+
93+
button1.Emit(1);
94+
button2.Emit(2);
95+
96+
group.DoTransaction([&]
97+
{
98+
for (int i=0; i<10; ++i)
99+
button1.Emit(42);
100+
});
101+
}
102+
103+
{
104+
// Dynamic signals
105+
VarSignal<int> s1{ group, 10 };
106+
VarSignal<int> s2{ group, 22 };
107+
108+
SignalSlot<int> slot{ s1 };
109+
110+
Observer<> areaObs{ PrintValue<int>, slot };
111+
112+
s1.Set(42);
113+
114+
slot.Set(s2);
115+
116+
s2.Set(667);
117+
}
118+
119+
{
120+
ReactiveGroup<> group1;
121+
ReactiveGroup<> group2;
122+
123+
VarSignal<int> s1{ group1, 10 };
124+
VarSignal<int> s2{ group2, 11 };
125+
126+
Signal<int> v{ Multiply<int>, s1, s2 };
127+
128+
Observer<> obs{ PrintValue<int>, v };
129+
130+
s1.Set(555);
131+
132+
std::this_thread::sleep_for(std::chrono::seconds(5));
133+
}
134+
135+
{
136+
ReactiveGroup<> group1;
137+
ReactiveGroup<> group2;
138+
139+
EventSource<int> e1{ group1 };
140+
EventSource<int> e2{ group2 };
141+
142+
auto merged = Merge(group2, e1, e2);
143+
144+
auto joined = Join(e1, e2);
145+
auto joined2 = Join(group1, e1, e2);
146+
147+
Observer<> eventObs{ PrintEvents<int>, merged };
148+
149+
e1.Emit(222);
150+
151+
std::this_thread::sleep_for(std::chrono::seconds(5));
152+
}
153+
154+
return 0;
25155
}
26156

27157
int main()
28158
{
29-
testme();
159+
ReactiveGroup<> group;
160+
161+
VarSignal<int> a{ };
162+
VarSignal<int> b{ };
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+
/*int main2()
195+
{
196+
ReactiveGroup<> group1;
197+
ReactiveGroup<> group2;
198+
ReactiveGroup<> group3;
199+
200+
VarSignal<int> x{ 0, group1 };
201+
VarSignal<int> y{ 0, group2 };
202+
VarSignal<int> z{ 0, group3 };
203+
204+
Signal<int> area{ Multiply<int>, x, y };
205+
Signal<int> volume{ Multiply<int>, area, z };
206+
207+
Observer<> obs{ PrintAreaAndVolume, area, volume };
208+
209+
Signal<vector<int>> volumeHistory = Iterate<vector<int>>( vector<int>{ }, PushToVector, Monitor(volume));
210+
211+
x <<= 2;
212+
y <<= 2;
213+
z <<= 2;
214+
215+
group.DoTransaction([&]
216+
{
217+
x <<= 100;
218+
y <<= 200;
219+
z <<= 300;
220+
});
221+
222+
obs.Cancel();
223+
224+
x <<= 42;
225+
226+
printf("History:\n");
227+
for (auto t : volumeHistory.Value())
228+
printf("%d ", t);
229+
printf("\n");
230+
231+
return 0;
232+
}
233+
234+
235+
int main3()
236+
{
237+
using namespace std;
238+
using namespace react;
239+
240+
ReactiveGroup<> group1;
241+
ReactiveGroup<> group2;
242+
243+
auto sig1 = VarSignal<int>( 10, group1 );
30244
31-
#ifdef REACT_ENABLE_LOGGING
32-
std::ofstream logfile;
33-
logfile.open("log.txt");
245+
auto link1 = SignalLink<int>( sig1, group2 );
246+
auto link2 = SignalLink<int>( sig1, group2 );
34247
35-
D::Log().Write(logfile);
36-
logfile.close();
37-
#endif
248+
sig1.Set(10);
38249
39250
return 0;
40-
}
251+
}*/

include/react/API.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ enum ReferencePolicy
2929
weak
3030
};
3131

32-
enum ThreadingPolicy
33-
{
34-
sequential,
35-
concurrent
36-
};
37-
3832
enum class WeightHint
3933
{
4034
automatic,
@@ -68,6 +62,9 @@ class VarSignalBase;
6862
template <typename S>
6963
class SignalSlotBase;
7064

65+
template <typename S>
66+
class SignalLinkBase;
67+
7168
template <typename S, OwnershipPolicy = unique>
7269
class Signal;
7370

@@ -77,6 +74,9 @@ class VarSignal;
7774
template <typename S, OwnershipPolicy = unique>
7875
class SignalSlot;
7976

77+
template <typename S, OwnershipPolicy = unique>
78+
class SignalLink;
79+
8080
// Events
8181
enum class Token;
8282

0 commit comments

Comments
 (0)