-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathBasicObservers.cpp
More file actions
53 lines (39 loc) · 1.33 KB
/
BasicObservers.cpp
File metadata and controls
53 lines (39 loc) · 1.33 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
// 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 - Creating subject-bound observers
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace example1
{
using namespace std;
using namespace react;
Group group;
StateVar<int> x = StateVar<int>::Create(group, 1);
void Run()
{
cout << "Example 1 - Creating observers" << endl;
{
auto st = State<int>::Create([] (int x) { return x; }, x);
auto obs = Observer::Create([] (int v) { cout << v << endl; }, st);
x.Set(2); // output: 2
}
x.Set(3); // no ouput
cout << endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Run examples
///////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
example1::Run();
return 0;
}