Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
previous_value_cell.hpp
1#ifndef LIVE_CELLS_PREVIOUS_VALUE_CELL_HPP
2#define LIVE_CELLS_PREVIOUS_VALUE_CELL_HPP
3
4#include "keys.hpp"
5#include "stateful_cell.hpp"
6#include "cell_state.hpp"
7#include "observer_cell_state.hpp"
8#include "maybe.hpp"
9#include "exceptions.hpp"
10
11namespace live_cells {
12
16 template <typename T>
18 using value_key<T>::value_key;
19 };
20
24 template <Cell C>
29 typedef typename C::value_type value_type;
30
31 public:
40 cell(cell),
41
42 current_value(maybe<value_type>::wrap([cell] {
43 return cell.value();
44 })) {
45
46 stale = false;
47 }
48
54 value_type value() {
55 if (stale) {
56 update_current_value();
57 }
58
59 return prev_value.unwrap();
60 }
61
62 protected:
63 void init() override {
66
67 cell.add_observer(observer_ptr());
68
69 current_value = maybe<value_type>::wrap([this] {
70 return cell.value();
71 });
72 }
73
74 void pause() override {
75 cell.remove_observer(observer_ptr());
76
77 prev_value = maybe<value_type>();
78 current_value = maybe<value_type>();
79
82 }
83
84 void will_update(const key_ref &k) override {
85 handle_will_update([this] {
87 });
88 }
89
90 void update(const key_ref &k, bool changed) override {
92 changed,
93 [] { return true; },
94
95 [this] (bool changed) {
96 if (stale) {
97 update_current_value();
98 }
99
101 },
102
103 [] {}
104 );
105 }
106
107 private:
111 const C cell;
112
116 maybe<value_type> prev_value;
117
121 maybe<value_type> current_value;
122
127 std::shared_ptr<observer> observer_ptr() {
128 return std::dynamic_pointer_cast<observer>(this->shared_from_this());
129 }
130
137 void update_current_value() {
138 auto next_value = maybe<value_type>::wrap([this] {
139 return cell.value();
140 });
141
142 if (current_value == next_value) {
143 stale = false;
144 return;
145 }
146
147 prev_value = current_value;
148 current_value = next_value;
149
150 stale = false;
151 }
152 };
153
161 template <Cell C>
162 class previous_value_cell : public stateful_cell<previous_value_cell_state<C>> {
163
168
173
174 public:
178 typedef typename C::value_type value_type;
179
187 parent(key_ref::create<key_type>(cell.key()), cell),
188 cell(cell) {}
189
196 return this->state->value();
197 }
198
205 value_type operator()() const {
206 argument_tracker::global().track_argument(*this);
207 return value();
208 }
209
210 private:
211
215 const C cell;
216 };
217
231 template <Cell C>
232 auto previous(const C &cell) {
234 }
235
236 namespace ops {
247 constexpr auto previous = [] (const Cell auto &cell) {
249 };
250
251 } // ops
252
253} // live_cells
254
255#endif /* LIVE_CELLS_PREVIOUS_VALUE_CELL_HPP */
static argument_tracker & global()
Definition tracker.hpp:99
Maintains the state of a stateful cell.
Definition cell_state.hpp:43
virtual void notify_will_update()
Notify the observers that the cell's value will change.
Definition live_cells.cpp:76
virtual void notify_update(bool did_change=true)
Notify the observers that the cell's value has changed.
Definition live_cells.cpp:92
virtual void init()
Called before the first observer is added.
Definition cell_state.hpp:68
virtual void pause()
Called after the last observer is removed.
Definition cell_state.hpp:79
Dynamically typed Cell container.
Definition observable.hpp:133
T value() const
Get the value held by the underlying Cell.
Definition observable.hpp:197
void remove_observer(observer::ref obs)
Remove an observer from the underlying Cell.
Definition observable.hpp:172
void add_observer(observer::ref obs)
Add an observer to the underlying Cell.
Definition observable.hpp:159
A computed cell which determines its argument cells at runtime.
Definition dynamic_compute_cell.hpp:153
Dynamically type key container.
Definition keys.hpp:76
Defines the interface for a key which uniquely identifies a cell.
Definition keys.hpp:33
Container holding a cell value or an exception that occurred while computing a value.
Definition maybe.hpp:17
static maybe< T > wrap(const std::invocable auto &f)
Create a maybe holding the result of calling f.
Definition maybe.hpp:81
T unwrap() const
Get the value or throw the exception stored in this container.
Definition maybe.hpp:96
Provides functionality for observing a cell from a cell_state.
Definition observer_cell_state.hpp:21
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< bool > auto &notify_update)
Handle an observer::update call.
Definition observer_cell_state.hpp:120
Defines the interface for observing changes to the value of a Cell.
Definition types.hpp:32
Maintains the state of a previous_value_cell.
Definition previous_value_cell.hpp:25
previous_value_cell_state(key_ref k, C cell)
Create a previous_value_cell state.
Definition previous_value_cell.hpp:38
void pause() override
Called after the last observer is removed.
Definition previous_value_cell.hpp:74
void init() override
Called before the first observer is added.
Definition previous_value_cell.hpp:63
void will_update(const key_ref &k) override
Notifies this observer that the value of the Cell identified by k is going to change.
Definition previous_value_cell.hpp:84
value_type value()
Get the stored previous value.
Definition previous_value_cell.hpp:54
void update(const key_ref &k, bool changed) override
Notifies this observer that the value of the Cell identified by k has changed.
Definition previous_value_cell.hpp:90
A Cell that evaluates to the previous value of another Cell.
Definition previous_value_cell.hpp:162
value_type value() const
Get the value of the cell.
Definition previous_value_cell.hpp:195
previous_value_cell(const C &cell)
Create a cell that evaluates to the previous value of cell.
Definition previous_value_cell.hpp:186
value_type operator()() const
Get the value of the cell and track it as a dependency.
Definition previous_value_cell.hpp:205
C::value_type value_type
The cell's value type.
Definition previous_value_cell.hpp:178
Base class for a cell with a state.
Definition stateful_cell.hpp:42
std::shared_ptr< previous_value_cell_state< C > > state
Reference to the cell's state.
Definition stateful_cell.hpp:105
Base class for a key distinguished from other keys by one or more values.
Definition keys.hpp:147
Defines the cell protocol.
Definition types.hpp:128
constexpr auto previous
Operator for creating a cell that evaluates to the previous value of another cell.
Definition previous_value_cell.hpp:247
Definition boolean.hpp:26
auto previous(const C &cell)
Create a Cell that evaluates to the previous value of cell.
Definition previous_value_cell.hpp:232
Key identifying a previous_value_cell.
Definition previous_value_cell.hpp:17