forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBasicComposition.cpp
More file actions
130 lines (100 loc) · 3.46 KB
/
BasicComposition.cpp
File metadata and controls
130 lines (100 loc) · 3.46 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
// Copyright Sebastian Jeckel 2017.
// 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 <string>
#include <utility>
#include "react/state.h"
#include "react/event.h"
#include "react/observer.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Example 1 - Reactive class members
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example1
{
using namespace std;
using namespace react;
Group g;
class Shape
{
public:
StateVar<int> width = StateVar<int>::Create(g, 0);
StateVar<int> height = StateVar<int>::Create(g, 0);
State<int> size = State<int>::Create(g, CalcSize, width, height);
auto GetReactiveMembers() const -> decltype(auto)
{ return std::tie(width, height, size); }
private:
static int CalcSize(int w, int h)
{ return w * h; }
};
void Run()
{
cout << "Example 1 - Reactive class members" << endl;
auto myShape = ObjectState<Shape>::Create(g, Shape());
auto obs = Observer::Create([] (const auto& ctx)
{
const Shape& shape = ctx.GetObject();
cout << "Size is " << ctx.Get(shape.size) << endl;
}, myShape);
g.DoTransaction([&]
{
myShape->width.Set(4);
myShape->height.Set(4);
}); // output: Size changed to 16
cout << endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Example 2 - Slots
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example2
{
using namespace std;
using namespace react;
Group g;
class Company
{
public:
StateVar<string> name;
Company(const char* name) :
name( StateVar<string>::Create(g, name) )
{ }
bool operator==(const Company& other) const
{ return name == other.name; }
};
class Employee
{
public:
StateSlot<string> myCompanyName;
Employee(const Company& company) :
myCompanyName( StateSlot<string>::Create(company.name) )
{ }
void SetCompany(const Company& company)
{ myCompanyName.Set(company.name); }
};
void Run()
{
cout << "Example 2 - Slots" << endl;
Company company1( "MetroTec" );
Company company2( "ACME" );
Employee alice( company1 );
auto obs = Observer::Create([] (const string& name)
{
cout << "Alice now works for " << name << endl;
}, alice.myCompanyName);
company1.name.Set(string( "ModernTec" )); // output: Alice now works for ModernTec
alice.SetCompany(company2); // output: Alice now works for ACME
company2.name.Set(string( "A.C.M.E." )); // output: Alice now works for A.C.M.E.
cout << endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Run examples
///////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
example1::Run();
example2::Run();
return 0;
}