Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
changes_only_state.hpp
1#ifndef LIVE_CELLS_CHANGES_ONLY_STATE_HPP
2#define LIVE_CELLS_CHANGES_ONLY_STATE_HPP
3
4#include <optional>
5
6#include "compute_state.hpp"
7
8namespace live_cells {
9
23 template <typename Base>
24 class changes_only_cell_state : public Base {
25 public:
26 using Base::Base;
27
28 void will_update(const key_ref &k) override {
29 Base::handle_will_update([this] { pre_update(); },
30 [this] { Base::notify_will_update(); });
31 }
32
33 void update(const key_ref &k, bool changed) override {
34 Base::handle_update(changed,
35 [this] { return did_change(); },
36 [this] (bool changed) { Base::notify_update(changed); },
37 [this] { post_update(); });
38 }
39
40 private:
44 std::optional<typename Base::value_type> old;
45
53 bool did_change() {
54 try {
55 return !old.has_value() || Base::value() != old.value();
56 }
57 catch (...) {
58 old.reset();
59 }
60
61 return true;
62 }
63
69 void pre_update() {
70 try {
71 old = Base::value();
72 }
73 catch (...) {
74 old.reset();
75 }
76 }
77
84 void post_update() {
85 old.reset();
86 }
87 };
88
95
101 static constexpr changes_only_option changes_only{};
102
103} // live_cells
104
105#endif /* LIVE_CELLS_CHANGES_ONLY_STATE_HPP */
Mixin that modifies a compute_cell_state subclass to only notify its observers if the new value of th...
Definition changes_only_state.hpp:24
Dynamically type key container.
Definition keys.hpp:76
Definition boolean.hpp:26
Cell option specifying that the cell, to which it is applied, should only notify its observers when i...
Definition changes_only_state.hpp:94