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 private:
82 const F compute_fn;
83
85 const R reverse_fn;
86
87 };
88
89
95 template <std::invocable F, typename R>
97 public changes_only_cell_state<static_mutable_compute_cell_state<F,R>> {
98
100
101 public:
102 using parent::parent;
103 };
104
109 template <std::invocable F, typename R, typename State = static_mutable_compute_cell_state<F,R>>
111
114
115 public:
119 typedef std::invoke_result_t<F> value_type;
120
138 template <typename T, typename U, typename... Args>
139 static_mutable_compute_cell(key_ref k, T&& compute, U&& reverse, Args&&... args) :
140 parent(k, std::forward<T>(compute), std::forward<U>(reverse), {std::forward<Args>(args)...}) {}
141
157 template <typename T, typename U, typename... Args>
158 static_mutable_compute_cell(T&& compute, U&& reverse, Args&&... args) :
159 parent(key_ref::create<unique_key>(), std::forward<T>(compute), std::forward<U>(reverse), std::unordered_set<cell>({cell(args)...})) {}
160
167 return this->state->value();
168 }
169
181 void value(value_type value) const {
182 this->state->value(value);
183 }
184
198 this->value(value);
199 return value;
200 }
201
220 this->value(value);
221 return value;
222 }
223
231 argument_tracker::global().track_argument(*this);
232 return value();
233 }
234 };
235
247 template <std::invocable C, typename R, Cell... As>
248 auto make_mutable_compute_cell(C compute, R reverse, As... args) {
249 return static_mutable_compute_cell<C,R>(compute, reverse, args...);
250 }
251
269 template <std::invocable C, typename R, Cell... As>
272
273 return static_mutable_compute_cell<C,R,Base>(compute, reverse, args...);
274 }
275
276} // live_cells
277
278#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: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
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
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:110
value_type operator=(const value_type &value)
Set the value of the cell to value.
Definition static_mutable_compute_cell.hpp:197
std::invoke_result_t< F > value_type
The type of value held by this cell.
Definition static_mutable_compute_cell.hpp:119
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:139
value_type value() const
Get the value of the cell.
Definition static_mutable_compute_cell.hpp:166
void value(value_type value) const
Set the value of the cell.
Definition static_mutable_compute_cell.hpp:181
value_type operator()() const
Get the value of the cell and track it as a dependency.
Definition static_mutable_compute_cell.hpp:230
value_type operator=(const value_type &value) const
Set the value of the cell.
Definition static_mutable_compute_cell.hpp:219
static_mutable_compute_cell(T &&compute, U &&reverse, Args &&... args)
Create a static mutable computed cell.
Definition static_mutable_compute_cell.hpp:158
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:97
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:248
Cell option specifying that the cell, to which it is applied, should only notify its observers when i...
Definition changes_only_state.hpp:94