Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
store_cell.hpp
1/*
2 * live_cells_cpp
3 * Copyright (C) 2024 Alexander Gutev <alex.gutev@gmail.com>
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you
6 * may not use this file except in compliance with the License. You
7 * may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 */
17
18#ifndef LIVE_CELLS_STORE_CELL_HPP
19#define LIVE_CELLS_STORE_CELL_HPP
20
21#include "keys.hpp"
22#include "compute_state.hpp"
23#include "stateful_cell.hpp"
24#include "changes_only_state.hpp"
25#include "tracker.hpp"
26
27namespace live_cells {
28
32 template <typename T>
34 using value_key<T>::value_key;
35 };
36
37 template <Cell C>
38 class store_cell_state;
39
43 template <Cell C>
48 const C arg;
49
50 friend class store_cell_state<C>;
51
52 public:
59 arg(arg) {}
60
68 C::value_type operator()(observer::ref) {
69 return arg.value();
70 }
71 };
72
76 template <Cell C>
77 class store_cell_state : public compute_cell_state<store_cell_compute_state<C>> {
80
81 public:
89 parent(k, arg) {}
90
91 protected:
93 typedef C::value_type value_type;
94
95 void init() override {
97
98 this->compute.arg.add_observer(this->observer_ptr());
99
100 try {
101 // Compute the initial value
102 this->value();
103 }
104 catch (...) {
105 // Prevent exception from being propagated to caller
106 }
107 }
108
109 void pause() override {
110 this->compute.arg.remove_observer(this->observer_ptr());
112 }
113 };
114
120 template <Cell C>
122 public changes_only_cell_state<store_cell_state<C>> {
124
125 public:
126 using parent::parent;
127
128 };
129
140 template <Cell C, typename State = store_cell_state<C>>
141 class store_cell : public stateful_cell<State> {
146
151
152 public:
156 typedef C::value_type value_type;
157
165 parent(key_ref::create<key_type>(cell.key()), cell) {}
166
173 return this->state->value();
174 }
175
182 value_type operator()() const {
183 argument_tracker::global().track_argument(*this);
184 return value();
185 }
186 };
187
205 template <Cell C>
206 auto store(const C &arg) {
207 return store_cell<C>(arg);
208 }
209
234 template <Cell C>
238
239 namespace ops {
240
251 constexpr auto store = [] (const Cell auto &cell) {
252 return live_cells::store(cell);
253 };
254
267 constexpr auto cache = [] (const Cell auto & cell) {
268 return live_cells::store(changes_only, cell);
269 };
270
271 } // ops
272
273} // live_cells
274
275#endif /* LIVE_CELLS_STORE_CELL_HPP */
static argument_tracker & global()
Definition tracker.hpp:99
Dynamically typed Cell container.
Definition observable.hpp:133
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
Cell state for a cell which computes a value as a function of one or more argument cells.
Definition compute_state.hpp:50
void pause() override
Definition compute_state.hpp:122
C compute
Compute value function.
Definition compute_state.hpp:95
value_type value()
Retrieve the latest cached value.
Definition compute_state.hpp:76
std::shared_ptr< observer > observer_ptr()
Get an observer::ref for this, that can be passed to add_observer and remove_observer of Cell.
Definition compute_state.hpp:101
void init() override
Definition compute_state.hpp:117
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
std::shared_ptr< observer > ref
Shared pointer to an observer.
Definition types.hpp:37
Base class for a cell with a state.
Definition stateful_cell.hpp:42
std::shared_ptr< S > state
Reference to the cell's state.
Definition stateful_cell.hpp:105
void remove_observer(observer::ref o) const
Remove an observer from the cell.
Definition stateful_cell.hpp:92
Defines the computation function of a store_cell.
Definition store_cell.hpp:44
store_cell_compute_state(C arg)
Definition store_cell.hpp:58
C::value_type operator()(observer::ref)
Compute the value of the argument cell.
Definition store_cell.hpp:68
Maintains the state of a store_cell.
Definition store_cell.hpp:77
void pause() override
Called after the last observer is removed.
Definition store_cell.hpp:109
C::value_type value_type
Definition store_cell.hpp:93
store_cell_state(key_ref k, C arg)
Create a store_cell state.
Definition store_cell.hpp:88
void init() override
Called before the first observer is added.
Definition store_cell.hpp:95
A Cell that caches the value of another Cell.
Definition store_cell.hpp:141
value_type value() const
Get the value of the cell.
Definition store_cell.hpp:172
store_cell(C cell)
Create a store cell that caches the value of cell.
Definition store_cell.hpp:164
value_type operator()() const
Get the value of the cell and track it as a dependency.
Definition store_cell.hpp:182
C::value_type value_type
The type of value held by this cell.
Definition store_cell.hpp:156
A store_cell_state that only notifies the observers of the cell if the new value of the cell is not e...
Definition store_cell.hpp:122
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 store
Operator for caching the value of another cell.
Definition store_cell.hpp:251
constexpr auto cache
Operator for creating a cell that caches the value of another cell and only notifies its observers wh...
Definition store_cell.hpp:267
Definition boolean.hpp:26
auto store(const C &arg)
Create a Cell that caches the value of another Cell.
Definition store_cell.hpp:206
Cell option specifying that the cell, to which it is applied, should only notify its observers when i...
Definition changes_only_state.hpp:94
Key identifying a store_cell.
Definition store_cell.hpp:33