Repository navigation
Expand file tree
/
Copy pathBasicReactors.cpp
More file actions
87 lines (66 loc) · 2.26 KB
/
Copy pathBasicReactors.cpp
File metadata and controls
87 lines (66 loc) · 2.26 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
// 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/Event.h"
#include "react/Reactor.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Example 1 - Creating reactive loops
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example1
{
using namespace std;
using namespace react;
REACTIVE_DOMAIN(D)
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;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Run examples
///////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
example1::Run();
return 0;
}