Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
dynamic_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_DYNAMIC_MUTABLE_COMPUTE_CELL_HPP
19#define LIVE_CELLS_DYNAMIC_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, typename ValueType = std::invoke_result_t<F>>
38 protected:
39 typedef ValueType value_type;
40
43
44 public:
45
53 template <typename T, typename U>
55 parent(key, {}),
56 compute_fn(std::forward<T>(compute)),
57 reverse_fn(std::forward<U>(reverse)) {}
58
59 protected:
60 ValueType compute() override {
61 auto t = argument_tracker::global().with_tracker([this] (auto cell) {
62 if (!this->arguments.count(cell)) {
63 this->arguments.emplace(cell);
64
65 cell.add_observer(this->observer_ptr());
66 }
67 });
68
69 return compute_fn();
70 }
71
72 void reverse_compute(ValueType value) override {
73 reverse_fn(value);
74 }
75
76 private:
78 F compute_fn;
79
81 R reverse_fn;
82
83 };
84
90 template <std::invocable F, typename R>
92 public changes_only_cell_state<dynamic_mutable_compute_cell_state<F,R>> {
93
95
96 public:
97 using parent::parent;
98 };
99
104 template <std::invocable F, typename R, typename State = dynamic_mutable_compute_cell_state<F,R>>
106
109
110 public:
111
115 typedef std::invoke_result_t<F> value_type;
116
132 template <typename T, typename U>
133 dynamic_mutable_compute_cell(key_ref k, T&& compute, U&& reverse) :
134 parent(k, std::forward<T>(compute), std::forward<U>(reverse)) {}
135
149 template <typename T, typename U>
150 dynamic_mutable_compute_cell(T&& compute, U&& reverse) :
151 parent(key_ref::create<unique_key>(), std::forward<T>(compute), std::forward<U>(reverse)) {}
152
153 value_type value() const {
154 return this->state->value();
155 }
156
166 void value(value_type value) const {
167 this->state->value(value);
168 }
169
183 this->value(value);
184 return value;
185 }
186
204 value_type operator=(const value_type &value) const {
205 this->value(value);
206 return value;
207 }
208
209 value_type operator()() const {
210 argument_tracker::global().track_argument(*this);
211 return value();
212 }
213 };
214
215} // live_cells
216
217#endif /* LIVE_CELLS_DYNAMIC_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
Maintains the state of a dynamic_mutable_compute_cell.
Definition dynamic_mutable_compute_cell.hpp:37
dynamic_mutable_compute_cell_state(key_ref key, T &&compute, U &&reverse)
Create the state for a dynamic mutable computed cell.
Definition dynamic_mutable_compute_cell.hpp:54
mutable_compute_cell_state< ValueType > parent
Shorthand for parent class.
Definition dynamic_mutable_compute_cell.hpp:42
ValueType compute() override
Compute the value of the cell as a function of its argument cells.
Definition dynamic_mutable_compute_cell.hpp:60
A mutable computed cell with the argument cells determined dynamically.
Definition dynamic_mutable_compute_cell.hpp:105
value_type operator=(const value_type &value) const
Set the value of the cell.
Definition dynamic_mutable_compute_cell.hpp:204
dynamic_mutable_compute_cell(T &&compute, U &&reverse)
Create a dynamic mutable computed cell.
Definition dynamic_mutable_compute_cell.hpp:150
std::invoke_result_t< F > value_type
Shorthand for the type of value held by this cell.
Definition dynamic_mutable_compute_cell.hpp:115
value_type operator=(const value_type &value)
Set the value of the cell to value.
Definition dynamic_mutable_compute_cell.hpp:182
dynamic_mutable_compute_cell(key_ref k, T &&compute, U &&reverse)
Create a dynamic mutable computed cell.
Definition dynamic_mutable_compute_cell.hpp:133
void value(value_type value) const
Set the value of the cell.
Definition dynamic_mutable_compute_cell.hpp:166
A dynamic_mutable_compute_cell_state that only notifies the observers of the cell if its value has ac...
Definition dynamic_mutable_compute_cell.hpp:92
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:166
bool reverse
Is the value of the cell being set, and hence a reverse computation being performed?
Definition mutable_compute_cell_state.hpp:172
T 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
A key of uniquely identified by a single instance.
Definition keys.hpp:128
Definition boolean.hpp:26
constant_cell< T > value(const T &value)
Definition constant_cell.hpp:132