forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicReactors.cpp
More file actions
161 lines (120 loc) · 4.13 KB
/
BasicReactors.cpp
File metadata and controls
161 lines (120 loc) · 4.13 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
// Copyright Sebastian Jeckel 2014.
// 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 <iostream>
#include <utility>
#include <vector>
#include "react/Domain.h"
#include "react/Signal.h"
#include "react/Event.h"
#include "react/Reactor.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Example 1 - Creating reactive loops
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example1
{
using namespace std;
using namespace react;
REACTIVE_DOMAIN(D, sequential)
USING_REACTIVE_DOMAIN(D)
using PointT = pair<int,int>;
using PathT = vector<PointT>;
vector<PathT> paths;
EventSourceT<PointT> mouseDown = MakeEventSource<D,PointT>();
EventSourceT<PointT> mouseUp = MakeEventSource<D,PointT>();
EventSourceT<PointT> mouseMove = MakeEventSource<D,PointT>();
ReactorT loop
{
[&] (ReactorT::Context ctx)
{
PathT points;
points.emplace_back(ctx.Await(mouseDown));
ctx.RepeatUntil(mouseUp, [&] {
points.emplace_back(ctx.Await(mouseMove));
});
points.emplace_back(ctx.Await(mouseUp));
paths.push_back(points);
}
};
void Run()
{
cout << "Example 1 - Creating reactive loops" << endl;
mouseDown << PointT( 1,1 );
mouseMove << PointT( 2,2 ) << PointT( 3,3 ) << PointT( 4,4 );
mouseUp << PointT( 5,5 );
mouseMove << PointT( 999,999 );
mouseDown << PointT( 10,10 );
mouseMove << PointT( 20,20 );
mouseUp << PointT( 30,30 );
for (const auto& path : paths)
{
cout << "Path: ";
for (const auto& point : path)
cout << "(" << point.first << "," << point.second << ") ";
cout << endl;
}
cout << endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Example 2 - Creating reactive loops
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example2
{
using namespace std;
using namespace react;
REACTIVE_DOMAIN(D, sequential)
USING_REACTIVE_DOMAIN(D)
using PointT = pair<int,int>;
using PathT = vector<PointT>;
vector<PathT> paths;
EventSourceT<PointT> mouseDown = MakeEventSource<D,PointT>();
EventSourceT<PointT> mouseUp = MakeEventSource<D,PointT>();
EventSourceT<PointT> mouseMove = MakeEventSource<D,PointT>();
VarSignalT<int> counter = MakeVar<D>(103);
ReactorT loop
{
[&] (ReactorT::Context ctx)
{
PathT points;
points.emplace_back(ctx.Await(mouseDown));
auto count = ctx.Get(counter);
ctx.RepeatUntil(mouseUp, [&] {
points.emplace_back(ctx.Await(mouseMove));
});
points.emplace_back(ctx.Await(mouseUp));
paths.push_back(points);
}
};
void Run()
{
cout << "Example 2 - Creating reactive loops" << endl;
mouseDown << PointT( 1,1 );
mouseMove << PointT( 2,2 ) << PointT( 3,3 ) << PointT( 4,4 );
mouseUp << PointT( 5,5 );
counter <<= 42;
mouseMove << PointT( 999,999 );
counter <<= 80;
mouseDown << PointT( 10,10 );
mouseMove << PointT( 20,20 );
mouseUp << PointT( 30,30 );
for (const auto& path : paths)
{
cout << "Path: ";
for (const auto& point : path)
cout << "(" << point.first << "," << point.second << ") ";
cout << endl;
}
cout << endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Run examples
///////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
example1::Run();
example2::Run();
return 0;
}