Skip to content

Commit e929f02

Browse files
committed
DoTransaction is now a free function.
1 parent 5a2bb61 commit e929f02

12 files changed

Lines changed: 235 additions & 140 deletions

File tree

benchmarks/src/BenchmarkRandom.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ struct Benchmark_Random : public BenchmarkBase<D>
304304
auto t0 = tbb::tick_count::now();
305305
for (int i=0; i<params.K; i++)
306306
{
307-
D::DoTransaction([&] {
307+
DoTransaction<D>([&] {
308308
for (int j=0; j<counts[i]; j++)
309309
{
310310
generator.InputSignals[cursor++] <<= 10+i;

examples/src/BasicAlgorithms.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace example1
4646

4747
mySensor.Samples << 20 << 21 << 21 << 22; // output: 20, 21, 22
4848

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

examples/src/BasicComposition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace example1
4646
cout << "Size changed to " << newValue << endl;
4747
});
4848

49-
D::DoTransaction([&] {
49+
DoTransaction<D>([&] {
5050
myShape.Width <<= 4;
5151
myShape.Height <<= 4;
5252
}); // output: Size changed to 16

examples/src/BasicEvents.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ namespace example5
238238
cout << v << endl;
239239
}); // output: 1, 2, 3, 4
240240

241-
D::DoTransaction([] {
241+
DoTransaction<D>([] {
242242
src << 1 << 2 << 3;
243243
src << 4;
244244
});

examples/src/BasicReactors.cpp

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

1111
#include "react/Domain.h"
12+
#include "react/Signal.h"
1213
#include "react/Event.h"
1314
#include "react/Reactor.h"
1415

@@ -76,12 +77,85 @@ namespace example1
7677
}
7778
}
7879

80+
///////////////////////////////////////////////////////////////////////////////////////////////////
81+
/// Example 2 - Creating reactive loops
82+
///////////////////////////////////////////////////////////////////////////////////////////////////
83+
namespace example2
84+
{
85+
using namespace std;
86+
using namespace react;
87+
88+
REACTIVE_DOMAIN(D, sequential)
89+
USING_REACTIVE_DOMAIN(D)
90+
91+
using PointT = pair<int,int>;
92+
using PathT = vector<PointT>;
93+
94+
vector<PathT> paths;
95+
96+
EventSourceT<PointT> mouseDown = MakeEventSource<D,PointT>();
97+
EventSourceT<PointT> mouseUp = MakeEventSource<D,PointT>();
98+
EventSourceT<PointT> mouseMove = MakeEventSource<D,PointT>();
99+
100+
VarSignalT<int> counter = MakeVar<D>(103);
101+
102+
ReactorT loop
103+
{
104+
[&] (ReactorT::Context ctx)
105+
{
106+
PathT points;
107+
108+
points.emplace_back(ctx.Await(mouseDown));
109+
110+
auto count = ctx.Get(counter);
111+
112+
ctx.RepeatUntil(mouseUp, [&] {
113+
points.emplace_back(ctx.Await(mouseMove));
114+
});
115+
116+
points.emplace_back(ctx.Await(mouseUp));
117+
118+
paths.push_back(points);
119+
}
120+
};
121+
122+
void Run()
123+
{
124+
cout << "Example 2 - Creating reactive loops" << endl;
125+
126+
mouseDown << PointT( 1,1 );
127+
mouseMove << PointT( 2,2 ) << PointT( 3,3 ) << PointT( 4,4 );
128+
mouseUp << PointT( 5,5 );
129+
130+
counter <<= 42;
131+
132+
mouseMove << PointT( 999,999 );
133+
134+
counter <<= 80;
135+
136+
mouseDown << PointT( 10,10 );
137+
mouseMove << PointT( 20,20 );
138+
mouseUp << PointT( 30,30 );
139+
140+
for (const auto& path : paths)
141+
{
142+
cout << "Path: ";
143+
for (const auto& point : path)
144+
cout << "(" << point.first << "," << point.second << ") ";
145+
cout << endl;
146+
}
147+
148+
cout << endl;
149+
}
150+
}
151+
79152
///////////////////////////////////////////////////////////////////////////////////////////////////
80153
/// Run examples
81154
///////////////////////////////////////////////////////////////////////////////////////////////////
82155
int main()
83156
{
84157
example1::Run();
158+
example2::Run();
85159

86160
return 0;
87161
}

examples/src/BasicSignals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ namespace example3
154154
a <<= 2; // output: z changed to 6
155155
b <<= 2; // output: z changed to 8
156156

157-
D::DoTransaction([] {
157+
DoTransaction<D>([] {
158158
a <<= 4;
159159
b <<= 4;
160160
}); // output: z changed to 16

examples/src/Main.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ using namespace react;
2121
// Defines a domain.
2222
// Each domain represents a separate dependency graph, managed by a dedicated propagation engine.
2323
// Reactives of different domains can not be combined.
24-
REACTIVE_DOMAIN(D, sequential)
24+
2525

2626
void SignalExample3()
2727
{
28+
REACTIVE_DOMAIN(D, sequential_concurrent)
29+
2830
cout << "Signal Example 3" << endl;
2931

3032
auto src = MakeVar<D>(0);
@@ -109,6 +111,8 @@ void SignalExample5()
109111

110112
void testme()
111113
{
114+
REACTIVE_DOMAIN(D, sequential_concurrent)
115+
112116
std::vector<int> results;
113117

114118
auto f_0 = [] (int a) -> int
@@ -159,21 +163,21 @@ void testme()
159163

160164
for (int i=0; i<10000; i++)
161165
{
162-
D::AsyncTransaction(st, [&,i] {
166+
AsyncTransaction<D>(st, [&,i] {
163167
n1 <<= 1+i;
164168
});
165169
}
166170

167171
for (int i=0; i<10000; i++)
168172
{
169-
D::AsyncTransaction(st, [&,i] {
173+
AsyncTransaction<D>(st, [&,i] {
170174
n1 <<= 20000+i;
171175
});
172176
}
173177

174178
for (int i=0; i<10000; i++)
175179
{
176-
D::AsyncTransaction(st, [&,i] {
180+
AsyncTransaction<D>(st, [&,i] {
177181
n1 <<= 100000+i;
178182
});
179183
}

include/react/Domain.h

Lines changed: 72 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,11 @@ class TransactionStatus
127127
private:
128128
std::shared_ptr<StateT> state_;
129129

130-
template <typename D, typename TPolicy>
131-
friend class DomainBase;
130+
template <typename D, typename F>
131+
friend void AsyncTransaction(TransactionStatus& status, F&& func);
132+
133+
template <typename D, typename F>
134+
friend void AsyncTransaction(TurnFlagsT flags, TransactionStatus& status, F&& func);
132135
};
133136

134137
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -178,59 +181,6 @@ class DomainBase
178181

179182
using ReactorT = Reactor<D>;
180183

181-
///////////////////////////////////////////////////////////////////////////////////////////////
182-
/// DoTransaction
183-
///////////////////////////////////////////////////////////////////////////////////////////////
184-
template <typename F>
185-
static void DoTransaction(F&& func)
186-
{
187-
using REACT_IMPL::DomainSpecificInputManager;
188-
DomainSpecificInputManager<D>::Instance().DoTransaction(0, std::forward<F>(func));
189-
}
190-
191-
template <typename F>
192-
static void DoTransaction(TurnFlagsT flags, F&& func)
193-
{
194-
using REACT_IMPL::DomainSpecificInputManager;
195-
DomainSpecificInputManager<D>::Instance().DoTransaction(flags, std::forward<F>(func));
196-
}
197-
198-
///////////////////////////////////////////////////////////////////////////////////////////////
199-
/// AsyncTransaction
200-
///////////////////////////////////////////////////////////////////////////////////////////////
201-
template <typename F>
202-
static void AsyncTransaction(F&& func)
203-
{
204-
using REACT_IMPL::DomainSpecificInputManager;
205-
DomainSpecificInputManager<D>::Instance()
206-
.AsyncTransaction(0, nullptr, std::forward<F>(func));
207-
}
208-
209-
template <typename F>
210-
static void AsyncTransaction(TurnFlagsT flags, F&& func)
211-
{
212-
using REACT_IMPL::DomainSpecificInputManager;
213-
DomainSpecificInputManager<D>::Instance()
214-
.AsyncTransaction(flags, nullptr, std::forward<F>(func));
215-
}
216-
217-
template <typename F>
218-
static void AsyncTransaction(TransactionStatus& status, F&& func)
219-
{
220-
using REACT_IMPL::DomainSpecificInputManager;
221-
222-
DomainSpecificInputManager<D>::Instance()
223-
.AsyncTransaction(0, status.state_, std::forward<F>(func));
224-
}
225-
226-
template <typename F>
227-
static void AsyncTransaction(TurnFlagsT flags, TransactionStatus& status, F&& func)
228-
{
229-
using REACT_IMPL::DomainSpecificInputManager;
230-
DomainSpecificInputManager<D>::Instance()
231-
.AsyncTransaction(flags, status.state_, std::forward<F>(func));
232-
}
233-
234184
#ifdef REACT_ENABLE_LOGGING
235185
///////////////////////////////////////////////////////////////////////////////////////////////
236186
/// Log
@@ -294,6 +244,8 @@ template
294244
auto MakeContinuation(const Signal<D,S>& trigger, FIn&& func)
295245
-> Continuation<D,DOut>
296246
{
247+
static_assert(DOut::is_concurrent, "MakeContinuation requires concurrent target domain.");
248+
297249
using REACT_IMPL::SignalContinuationNode;
298250
using F = typename std::decay<FIn>::type;
299251

@@ -315,6 +267,8 @@ template
315267
auto MakeContinuation(const Events<D,E>& trigger, FIn&& func)
316268
-> Continuation<D,DOut>
317269
{
270+
static_assert(DOut::is_concurrent, "MakeContinuation requires concurrent target domain.");
271+
318272
using REACT_IMPL::EventContinuationNode;
319273
using F = typename std::decay<FIn>::type;
320274

@@ -338,6 +292,8 @@ auto MakeContinuation(const Events<D,E>& trigger,
338292
const SignalPack<D,TDepValues...>& depPack, FIn&& func)
339293
-> Continuation<D,DOut>
340294
{
295+
static_assert(DOut::is_concurrent, "MakeContinuation requires concurrent target domain.");
296+
341297
using REACT_IMPL::SyncedContinuationNode;
342298
using F = typename std::decay<FIn>::type;
343299

@@ -366,6 +322,67 @@ auto MakeContinuation(const Events<D,E>& trigger,
366322
depPack.Data);
367323
}
368324

325+
///////////////////////////////////////////////////////////////////////////////////////////////
326+
/// DoTransaction
327+
///////////////////////////////////////////////////////////////////////////////////////////////
328+
template <typename D, typename F>
329+
void DoTransaction(F&& func)
330+
{
331+
using REACT_IMPL::DomainSpecificInputManager;
332+
DomainSpecificInputManager<D>::Instance().DoTransaction(0, std::forward<F>(func));
333+
}
334+
335+
template <typename D, typename F>
336+
void DoTransaction(TurnFlagsT flags, F&& func)
337+
{
338+
using REACT_IMPL::DomainSpecificInputManager;
339+
DomainSpecificInputManager<D>::Instance().DoTransaction(flags, std::forward<F>(func));
340+
}
341+
342+
///////////////////////////////////////////////////////////////////////////////////////////////
343+
/// AsyncTransaction
344+
///////////////////////////////////////////////////////////////////////////////////////////////
345+
template <typename D, typename F>
346+
void AsyncTransaction(F&& func)
347+
{
348+
static_assert(D::is_concurrent, "AsyncTransaction requires concurrent domain.");
349+
350+
using REACT_IMPL::DomainSpecificInputManager;
351+
DomainSpecificInputManager<D>::Instance()
352+
.AsyncTransaction(0, nullptr, std::forward<F>(func));
353+
}
354+
355+
template <typename D, typename F>
356+
void AsyncTransaction(TurnFlagsT flags, F&& func)
357+
{
358+
static_assert(D::is_concurrent, "AsyncTransaction requires concurrent domain.");
359+
360+
using REACT_IMPL::DomainSpecificInputManager;
361+
DomainSpecificInputManager<D>::Instance()
362+
.AsyncTransaction(flags, nullptr, std::forward<F>(func));
363+
}
364+
365+
template <typename D, typename F>
366+
void AsyncTransaction(TransactionStatus& status, F&& func)
367+
{
368+
static_assert(D::is_concurrent, "AsyncTransaction requires concurrent domain.");
369+
370+
using REACT_IMPL::DomainSpecificInputManager;
371+
372+
DomainSpecificInputManager<D>::Instance()
373+
.AsyncTransaction(0, status.state_, std::forward<F>(func));
374+
}
375+
376+
template <typename D, typename F>
377+
void AsyncTransaction(TurnFlagsT flags, TransactionStatus& status, F&& func)
378+
{
379+
static_assert(D::is_concurrent, "AsyncTransaction requires concurrent domain.");
380+
381+
using REACT_IMPL::DomainSpecificInputManager;
382+
DomainSpecificInputManager<D>::Instance()
383+
.AsyncTransaction(flags, status.state_, std::forward<F>(func));
384+
}
385+
369386
/******************************************/ REACT_END /******************************************/
370387

371388
/***************************************/ REACT_IMPL_BEGIN /**************************************/

tests/src/EventStreamTest.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ TYPED_TEST_P(EventStreamTest, EventMerge1)
111111
results.push_back(v);
112112
});
113113

114-
D::DoTransaction([&] {
114+
DoTransaction<D>([&] {
115115
a1 << 10;
116116
a2 << 20;
117117
a3 << 30;
@@ -147,7 +147,7 @@ TYPED_TEST_P(EventStreamTest, EventMerge2)
147147
std::string s2("two");
148148
std::string s3("three");
149149

150-
D::DoTransaction([&] {
150+
DoTransaction<D>([&] {
151151
a1 << s1;
152152
a2 << s2;
153153
a3 << s3;

tests/src/OperationsTest.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ TYPED_TEST_P(OperationsTest, Iterate2)
9393
ASSERT_EQ(v, 5050);
9494
});
9595

96-
D::DoTransaction([&] {
96+
DoTransaction<D>([&] {
9797
for (auto i=1; i<=100; i++)
9898
numSrc << i;
9999
});
@@ -660,7 +660,7 @@ TYPED_TEST_P(OperationsTest, SyncedEventTransform1)
660660

661661
in1 << string("Hello Worlt") << string("Hello World");
662662

663-
D::DoTransaction([&] {
663+
DoTransaction<D>([&] {
664664
in2 << string("Hello Vorld");
665665
first.Set(string("Alice"));
666666
last.Set(string("Anderson"));

0 commit comments

Comments
 (0)