Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
static_mutable_compute_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_STATIC_MUTABLE_COMPUTE_CELL_HPP
19#define LIVE_CELLS_STATIC_MUTABLE_COMPUTE_CELL_HPP
20
21#include <functional>
22#include <memory>
23#include <concepts>
24#include <type_traits>
25
26#include "keys.hpp"
27#include "mutable_compute_cell_state.hpp"
28#include "changes_only_state.hpp"
29
30namespace live_cells {
31
36 template <std::invocable F, typename R>
37 class static_mutable_compute_cell_state : public mutable_compute_cell_state<std::invoke_result_t<F>> {
38 protected:
39 typedef std::invoke_result_t<F> value_type;
40
43
44 public:
45
54 template <typename T, typename U>
55 static_mutable_compute_cell_state(key_ref key, T&& compute, U&& reverse, const std::unordered_set<cell> &arguments) :
57 compute_fn(std::forward<T>(compute)),
58 reverse_fn(std::forward<U>(reverse)) {}
59
60 protected:
67 value_type compute() override {
68 return compute_fn();
69 }
70
76 void reverse_compute(value_type value) override {
77 reverse_fn(value);
78 }
79
80 void init() override {
82
83 for (auto arg : this->arguments) {
84 arg.add_observer(this->observer_ptr());
85 }
86 }
87
88 private:
90 const F compute_fn;
91
93 const R reverse_fn;
94
95 };
96
97
103 template <std::invocable F, typename R>
105 public changes_only_cell_state<static_mutable_compute_cell_state<F,R>> {
106
108
109 public:
110 using parent::parent;
111 };
112
117 template <std::invocable F, typename R, typename State = static_mutable_compute_cell_state<F,R>>
119
122
123 public:
127 typedef std::invoke_result_t<F> value_type;
128
146 template <typename T, typename U, typename... Args>
147 static_mutable_compute_cell(key_ref k, T&& compute, U&& reverse, Args&&... args) :
148 parent(k,
149 std::forward<T>(compute),
150 std::forward<U>(reverse),
151 std::unordered_set<cell>({cell(args)...})) {}
152
168 template <typename T, typename U, typename... Args>
169 static_mutable_compute_cell(T&& compute, U&& reverse, Args&&... args) :
170 parent(key_ref::create<unique_key>(), std::forward<T>(compute), std::forward<U>(reverse), std::unordered_set<cell>({cell(args)...})) {}
171
178 return this->state->value();
179 }
180
192 void value(value_type value) const {
193 this->state->value(value);
194 }
195
209 this->value(value);
210 return value;
211 }
212
231 this->value(value);
232 return value;
233 }
234
242 argument_tracker::global().track_argument(*this);
243 return value();
244 }
245 };
246
258 template <std::invocable C, typename R, Cell... As>
259 auto make_mutable_compute_cell(C compute, R reverse, As... args) {
260 return static_mutable_compute_cell<C,R>(compute, reverse, args...);
261 }
262
280 template <std::invocable C, typename R, Cell... As>
283
284 return static_mutable_compute_cell<C,R,Base>(compute, reverse, args...);
285 }
286
299 template <std::invocable C, typename R, Cell... As>
300 auto make_mutable_compute_cell(key_ref key, C compute, R reverse, As... args) {
301 return static_mutable_compute_cell<C,R>(key, compute, reverse, args...);
302 }
303
322 template <std::invocable C, typename R, Cell... As>
328
329} // live_cells
330
331#endif /* LIVE_CELLS_STATIC_MUTABLE_COMPUTE_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
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
Maintains the state of a mutable computed cell.
Definition mutable_compute_cell_state.hpp:39
std::unordered_set< cell > arguments
Set of argument cells referenced by the value computation function.
Definition mutable_compute_cell_state.hpp:162
std::shared_ptr< observer > observer_ptr()
Get an observer::ref for this, that can be passed to add_observer() and remove_observer() of the Cell...
Definition mutable_compute_cell_state.hpp:208
bool reverse
Is the value of the cell being set, and hence a reverse computation being performed?
Definition mutable_compute_cell_state.hpp:168
void init() override
Add this state as an observer to the compute function arguments.
Definition mutable_compute_cell_state.hpp:132
std::invoke_result_t< F > value()
Get the value of the cell.
Definition mutable_compute_cell_state.hpp:63
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
Maintains the state for a static_mutable_compute_cell.
Definition static_mutable_compute_cell.hpp:37
void reverse_compute(value_type value) override
Call the reverse computation function of the cell.
Definition static_mutable_compute_cell.hpp:76
mutable_compute_cell_state< value_type > parent
Definition static_mutable_compute_cell.hpp:42
static_mutable_compute_cell_state(key_ref key, T &&compute, U &&reverse, const std::unordered_set< cell > &arguments)
Create the state for a static_mutable_compute cell.
Definition static_mutable_compute_cell.hpp:55
void init() override
Add this state as an observer to the compute function arguments.
Definition static_mutable_compute_cell.hpp:80
value_type compute() override
Compute the value of the cell as a function of the arguments.
Definition static_mutable_compute_cell.hpp:67
A mutable computed cell with arguments determined at compile-time.
Definition static_mutable_compute_cell.hpp:118
value_type operator=(const value_type &value)
Set the value of the cell to value.
Definition static_mutable_compute_cell.hpp:208
std::invoke_result_t< F > value_type
The type of value held by this cell.
Definition static_mutable_compute_cell.hpp:127
static_mutable_compute_cell(key_ref k, T &&compute, U &&reverse, Args &&... args)
Create a static mutable computed cell.
Definition static_mutable_compute_cell.hpp:147
value_type value() const
Get the value of the cell.
Definition static_mutable_compute_cell.hpp:177
void value(value_type value) const
Set the value of the cell.
Definition static_mutable_compute_cell.hpp:192
value_type operator()() const
Get the value of the cell and track it as a dependency.
Definition static_mutable_compute_cell.hpp:241
value_type operator=(const value_type &value) const
Set the value of the cell.
Definition static_mutable_compute_cell.hpp:230
static_mutable_compute_cell(T &&compute, U &&reverse, Args &&... args)
Create a static mutable computed cell.
Definition static_mutable_compute_cell.hpp:169
A static_mutable_compute_cell_state that only notifies the observers of the cell when the cell's valu...
Definition static_mutable_compute_cell.hpp:105
A key of uniquely identified by a single instance.
Definition keys.hpp:128
Definition boolean.hpp:26
auto make_mutable_compute_cell(C compute, R reverse, As... args)
Create a static_mutable_compute_cell with compute function compute, reverse compute function reverse ...
Definition static_mutable_compute_cell.hpp:259
Cell option specifying that the cell, to which it is applied, should only notify its observers when i...
Definition changes_only_state.hpp:94