Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
observer_cell_state.hpp
1#ifndef LIVE_CELLS_OBSERVER_CELL_STATE_HPP
2#define LIVE_CELLS_OBSERVER_CELL_STATE_HPP
3
4#include <cassert>
5#include <concepts>
6
7namespace live_cells {
8
22 protected:
26 bool stale = true;
27
32 bool updating = false;
33
41 stale = true;
42 }
43
51 stale = true;
52 }
53
66 void handle_will_update(const std::invocable auto &notify_will_update) {
67 handle_will_update([] {}, notify_will_update);
68 }
69
87 void handle_will_update(const std::invocable auto &pre_update,
88 const std::invocable auto &notify_will_update) {
89 if (!updating) {
90 assert(changed_dependencies == 0 && "Number of changed dependencies == 0 at start of update cycle.");
91
92 pre_update();
93
94 updating = true;
95 has_changed = false;
96 changed_dependencies = 0;
97
98 notify_will_update();
99 }
100
101 changed_dependencies++;
102 }
103
120 void handle_update(bool changed, const std::invocable<bool> auto &notify_update) {
121 handle_update(changed, [] { return true; }, notify_update, [] { });
122 }
123
153 const std::invocable auto &did_change,
154 const std::invocable<bool> auto &notify_update,
155 const std::invocable auto &post_update) {
156 if (updating) {
157 assert(changed_dependencies > 0 && "Calls to update() are not more than calls to will_update().");
158
159 has_changed = has_changed || changed;
160
161 if (--changed_dependencies == 0) {
162 stale = stale || has_changed;
163
164 notify_update(has_changed && did_change());
165 updating = false;
166
167 if (has_changed) {
168 post_update();
169 }
170 }
171 }
172 }
173
174 private:
175
179 int changed_dependencies = 0;
180
184 int has_changed = false;
185 };
186
187} // live_cells
188
189#endif /* LIVE_CELLS_OBSERVER_CELL_STATE_HPP */
A computed cell which determines its argument cells at runtime.
Definition dynamic_compute_cell.hpp:153
Provides functionality for observing a cell from a cell_state.
Definition observer_cell_state.hpp:21
bool updating
Are the argument cells in the process of updating their values?
Definition observer_cell_state.hpp:32
void init_observer_state()
Initialize the cell observation state.
Definition observer_cell_state.hpp:40
void handle_will_update(const std::invocable auto &notify_will_update)
Handle a observer::will_update call.
Definition observer_cell_state.hpp:66
void pause_observer_state()
Pause the cell observation state.
Definition observer_cell_state.hpp:50
bool stale
Does the value have to be recomputed?
Definition observer_cell_state.hpp:26
void handle_update(bool changed, const std::invocable auto &did_change, const std::invocable< bool > auto &notify_update, const std::invocable auto &post_update)
Handle an observer::update call.
Definition observer_cell_state.hpp:152
void handle_update(bool changed, const std::invocable< bool > auto &notify_update)
Handle an observer::update call.
Definition observer_cell_state.hpp:120
void handle_will_update(const std::invocable auto &pre_update, const std::invocable auto &notify_will_update)
Handle a observer::will_update call.
Definition observer_cell_state.hpp:87
Definition boolean.hpp:26