You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
std::cout << "B triggered with " << str << " and " << v << std::endl;
37
+
});
38
+
39
+
// call emit to trigger all observers
40
+
eventA.emit();
41
+
eventB.emit("meaning of life", 42);
42
+
43
+
// `observe::Observer` can store any type of observer
44
+
observer.observe(eventA, [](){ std::cout << "I am now observing A" << std::endl; });
45
+
46
+
// to remove an observer without destroying the object, call reset
47
+
observer.reset();
48
+
}
23
49
```
24
50
51
+
Note that events and observers are thread and exception safe.
52
+
25
53
### Using observe::Value
26
54
55
+
The project also includes a header `observe/value.h` with an experimental observable value implementation.
56
+
The API is still subject to change, so use with caution.
57
+
27
58
```cpp
28
59
observe::Value a = 1;
29
60
observe::Value b = 2;
61
+
62
+
// contains the sum of `a` and `b`
30
63
observe::DependentObservableValue sum([](auto a, auto b){ return a+b; },a,b);
31
-
sum.onChange.connect([](auto &v){ std::cout << "The result changed to " << r << std::endl; });
64
+
65
+
// all observable values contain an `Event``onChange`
66
+
sum.onChange.connect([](auto &v){
67
+
std::cout << "The result changed to " << r << std::endl;
68
+
});
69
+
70
+
// access the value by dereferencing
32
71
std::cout << "The result is " << *sum << std::endl; // -> the result is 3
72
+
73
+
// changes will automatically propagate through dependent values
33
74
a.set(3); // -> The result changed to 5
34
75
```
35
76
36
77
## Installation and usage
37
78
38
-
With [CPM](https://github.com/TheLartians/CPM), observe::Event can be used in a CMake project simply by adding the following to the project's `CMakeLists.txt`.
79
+
With [CPM.cmake](https://github.com/TheLartians/CPM) you can easily add the headers to your project.
Alternatively, the repository can be cloned locally and included it via `add_subdirectory`. Installing observe::Event will make it findable in CMake's `find_package`.
0 commit comments