| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5a2bb61 commit e929f02
12 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -304,7 +304,7 @@ struct Benchmark_Random : public BenchmarkBase<D> | |||
| 304 | 304 | auto t0 = tbb::tick_count::now(); | |
| 305 | 305 | for (int i=0; i<params.K; i++) | |
| 306 | 306 | { | |
| 307 | - D::DoTransaction([&] { | ||
| 307 | + DoTransaction<D>([&] { | ||
| 308 | 308 | for (int j=0; j<counts[i]; j++) | |
| 309 | 309 | { | |
| 310 | 310 | generator.InputSignals[cursor++] <<= 10+i; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -46,7 +46,7 @@ namespace example1 | |||
| 46 | 46 | ||
| 47 | 47 | mySensor.Samples << 20 << 21 << 21 << 22; // output: 20, 21, 22 | |
| 48 | 48 | ||
| 49 | - D::DoTransaction([&] { | ||
| 49 | + DoTransaction<D>([&] { | ||
| 50 | 50 | mySensor.Samples << 30 << 31 << 31 << 32; | |
| 51 | 51 | }); // output: 32 | |
| 52 | 52 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -46,7 +46,7 @@ namespace example1 | |||
| 46 | 46 | cout << "Size changed to " << newValue << endl; | |
| 47 | 47 | }); | |
| 48 | 48 | ||
| 49 | - D::DoTransaction([&] { | ||
| 49 | + DoTransaction<D>([&] { | ||
| 50 | 50 | myShape.Width <<= 4; | |
| 51 | 51 | myShape.Height <<= 4; | |
| 52 | 52 | }); // output: Size changed to 16 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -238,7 +238,7 @@ namespace example5 | |||
| 238 | 238 | cout << v << endl; | |
| 239 | 239 | }); // output: 1, 2, 3, 4 | |
| 240 | 240 | ||
| 241 | - D::DoTransaction([] { | ||
| 241 | + DoTransaction<D>([] { | ||
| 242 | 242 | src << 1 << 2 << 3; | |
| 243 | 243 | src << 4; | |
| 244 | 244 | }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ | |||
| 9 | 9 | #include <vector> | |
| 10 | 10 | ||
| 11 | 11 | #include "react/Domain.h" | |
| 12 | + #include "react/Signal.h" | ||
| 12 | 13 | #include "react/Event.h" | |
| 13 | 14 | #include "react/Reactor.h" | |
| 14 | 15 | ||
@@ -76,12 +77,85 @@ namespace example1 | |||
| 76 | 77 | } | |
| 77 | 78 | } | |
| 78 | 79 | ||
| 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 | + | ||
| 79 | 152 | /////////////////////////////////////////////////////////////////////////////////////////////////// | |
| 80 | 153 | /// Run examples | |
| 81 | 154 | /////////////////////////////////////////////////////////////////////////////////////////////////// | |
| 82 | 155 | int main() | |
| 83 | 156 | { | |
| 84 | 157 | example1::Run(); | |
| 158 | + example2::Run(); | ||
| 85 | 159 | ||
| 86 | 160 | return 0; | |
| 87 | 161 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -154,7 +154,7 @@ namespace example3 | |||
| 154 | 154 | a <<= 2; // output: z changed to 6 | |
| 155 | 155 | b <<= 2; // output: z changed to 8 | |
| 156 | 156 | ||
| 157 | - D::DoTransaction([] { | ||
| 157 | + DoTransaction<D>([] { | ||
| 158 | 158 | a <<= 4; | |
| 159 | 159 | b <<= 4; | |
| 160 | 160 | }); // output: z changed to 16 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,10 +21,12 @@ using namespace react; | |||
| 21 | 21 | // Defines a domain. | |
| 22 | 22 | // Each domain represents a separate dependency graph, managed by a dedicated propagation engine. | |
| 23 | 23 | // Reactives of different domains can not be combined. | |
| 24 | - REACTIVE_DOMAIN(D, sequential) | ||
| 24 | + | ||
| 25 | 25 | ||
| 26 | 26 | void SignalExample3() | |
| 27 | 27 | { | |
| 28 | + REACTIVE_DOMAIN(D, sequential_concurrent) | ||
| 29 | + | ||
| 28 | 30 | cout << "Signal Example 3" << endl; | |
| 29 | 31 | ||
| 30 | 32 | auto src = MakeVar<D>(0); | |
@@ -109,6 +111,8 @@ void SignalExample5() | |||
| 109 | 111 | ||
| 110 | 112 | void testme() | |
| 111 | 113 | { | |
| 114 | + REACTIVE_DOMAIN(D, sequential_concurrent) | ||
| 115 | + | ||
| 112 | 116 | std::vector<int> results; | |
| 113 | 117 | ||
| 114 | 118 | auto f_0 = [] (int a) -> int | |
@@ -159,21 +163,21 @@ void testme() | |||
| 159 | 163 | ||
| 160 | 164 | for (int i=0; i<10000; i++) | |
| 161 | 165 | { | |
| 162 | - D::AsyncTransaction(st, [&,i] { | ||
| 166 | + AsyncTransaction<D>(st, [&,i] { | ||
| 163 | 167 | n1 <<= 1+i; | |
| 164 | 168 | }); | |
| 165 | 169 | } | |
| 166 | 170 | ||
| 167 | 171 | for (int i=0; i<10000; i++) | |
| 168 | 172 | { | |
| 169 | - D::AsyncTransaction(st, [&,i] { | ||
| 173 | + AsyncTransaction<D>(st, [&,i] { | ||
| 170 | 174 | n1 <<= 20000+i; | |
| 171 | 175 | }); | |
| 172 | 176 | } | |
| 173 | 177 | ||
| 174 | 178 | for (int i=0; i<10000; i++) | |
| 175 | 179 | { | |
| 176 | - D::AsyncTransaction(st, [&,i] { | ||
| 180 | + AsyncTransaction<D>(st, [&,i] { | ||
| 177 | 181 | n1 <<= 100000+i; | |
| 178 | 182 | }); | |
| 179 | 183 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -127,8 +127,11 @@ class TransactionStatus | |||
| 127 | 127 | private: | |
| 128 | 128 | std::shared_ptr<StateT> state_; | |
| 129 | 129 | ||
| 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); | ||
| 132 | 135 | }; | |
| 133 | 136 | ||
| 134 | 137 | /////////////////////////////////////////////////////////////////////////////////////////////////// | |
@@ -178,59 +181,6 @@ class DomainBase | |||
| 178 | 181 | ||
| 179 | 182 | using ReactorT = Reactor<D>; | |
| 180 | 183 | ||
| 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 | - | ||
| 234 | 184 | #ifdef REACT_ENABLE_LOGGING | |
| 235 | 185 | /////////////////////////////////////////////////////////////////////////////////////////////// | |
| 236 | 186 | /// Log | |
@@ -294,6 +244,8 @@ template | |||
| 294 | 244 | auto MakeContinuation(const Signal<D,S>& trigger, FIn&& func) | |
| 295 | 245 | -> Continuation<D,DOut> | |
| 296 | 246 | { | |
| 247 | + static_assert(DOut::is_concurrent, "MakeContinuation requires concurrent target domain."); | ||
| 248 | + | ||
| 297 | 249 | using REACT_IMPL::SignalContinuationNode; | |
| 298 | 250 | using F = typename std::decay<FIn>::type; | |
| 299 | 251 | ||
@@ -315,6 +267,8 @@ template | |||
| 315 | 267 | auto MakeContinuation(const Events<D,E>& trigger, FIn&& func) | |
| 316 | 268 | -> Continuation<D,DOut> | |
| 317 | 269 | { | |
| 270 | + static_assert(DOut::is_concurrent, "MakeContinuation requires concurrent target domain."); | ||
| 271 | + | ||
| 318 | 272 | using REACT_IMPL::EventContinuationNode; | |
| 319 | 273 | using F = typename std::decay<FIn>::type; | |
| 320 | 274 | ||
@@ -338,6 +292,8 @@ auto MakeContinuation(const Events<D,E>& trigger, | |||
| 338 | 292 | const SignalPack<D,TDepValues...>& depPack, FIn&& func) | |
| 339 | 293 | -> Continuation<D,DOut> | |
| 340 | 294 | { | |
| 295 | + static_assert(DOut::is_concurrent, "MakeContinuation requires concurrent target domain."); | ||
| 296 | + | ||
| 341 | 297 | using REACT_IMPL::SyncedContinuationNode; | |
| 342 | 298 | using F = typename std::decay<FIn>::type; | |
| 343 | 299 | ||
@@ -366,6 +322,67 @@ auto MakeContinuation(const Events<D,E>& trigger, | |||
| 366 | 322 | depPack.Data); | |
| 367 | 323 | } | |
| 368 | 324 | ||
| 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 | + | ||
| 369 | 386 | /******************************************/ REACT_END /******************************************/ | |
| 370 | 387 | ||
| 371 | 388 | /***************************************/ REACT_IMPL_BEGIN /**************************************/ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -111,7 +111,7 @@ TYPED_TEST_P(EventStreamTest, EventMerge1) | |||
| 111 | 111 | results.push_back(v); | |
| 112 | 112 | }); | |
| 113 | 113 | ||
| 114 | - D::DoTransaction([&] { | ||
| 114 | + DoTransaction<D>([&] { | ||
| 115 | 115 | a1 << 10; | |
| 116 | 116 | a2 << 20; | |
| 117 | 117 | a3 << 30; | |
@@ -147,7 +147,7 @@ TYPED_TEST_P(EventStreamTest, EventMerge2) | |||
| 147 | 147 | std::string s2("two"); | |
| 148 | 148 | std::string s3("three"); | |
| 149 | 149 | ||
| 150 | - D::DoTransaction([&] { | ||
| 150 | + DoTransaction<D>([&] { | ||
| 151 | 151 | a1 << s1; | |
| 152 | 152 | a2 << s2; | |
| 153 | 153 | a3 << s3; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -93,7 +93,7 @@ TYPED_TEST_P(OperationsTest, Iterate2) | |||
| 93 | 93 | ASSERT_EQ(v, 5050); | |
| 94 | 94 | }); | |
| 95 | 95 | ||
| 96 | - D::DoTransaction([&] { | ||
| 96 | + DoTransaction<D>([&] { | ||
| 97 | 97 | for (auto i=1; i<=100; i++) | |
| 98 | 98 | numSrc << i; | |
| 99 | 99 | }); | |
@@ -660,7 +660,7 @@ TYPED_TEST_P(OperationsTest, SyncedEventTransform1) | |||
| 660 | 660 | ||
| 661 | 661 | in1 << string("Hello Worlt") << string("Hello World"); | |
| 662 | 662 | ||
| 663 | - D::DoTransaction([&] { | ||
| 663 | + DoTransaction<D>([&] { | ||
| 664 | 664 | in2 << string("Hello Vorld"); | |
| 665 | 665 | first.Set(string("Alice")); | |
| 666 | 666 | last.Set(string("Anderson")); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments