Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
mutable_cell_view.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_MUTABLE_CELL_VIEW_HPP
19#define LIVE_CELLS_MUTABLE_CELL_VIEW_HPP
20
21#include <functional>
22#include <tuple>
23#include <utility>
24
25#include "make_cell.hpp"
26#include "compute_cell.hpp"
27#include "mutable_cell.hpp"
28
29namespace live_cells {
43 template <std::invocable F, typename R, typename... Os>
44 class mutable_cell_view_base : public compute_cell_base<F, Os...> {
45
47 typedef compute_cell_base<F, Os...> parent;
48
50 typedef std::invoke_result_t<F> value_type;
51
52 public:
53 using parent::value;
54
70 mutable_cell_view_base(F compute, R reverse, Os... args) :
71 parent(compute, args...),
72 reverse(reverse) {}
73
79 void value(value_type value) const {
80 batch([&] {
81 reverse(value);
82 });
83 }
84
85 private:
89 const R reverse;
90 };
91
102 template <std::invocable F, typename R, Cell... Ts>
104
117 auto make_mutable_cell_view(std::invocable auto compute, auto reverse, auto... args) {
118 return mutable_cell_view<
119 decltype(compute),
120 decltype(reverse),
121 decltype(args)...>(
122 compute,
123 reverse,
124 args...
125 );
126 }
127
142 auto make_mutable_cell_view(key_ref key, std::invocable auto compute, auto reverse, auto... args) {
143 return mutable_cell_view<
144 decltype(compute),
145 decltype(reverse),
146 decltype(args)...>(
147 key,
148 compute,
149 reverse,
150 args...
151 );
152 }
153
179 template <typename A1, typename A2, typename... As>
181 auto packed = internal::pack<2>(arg1, arg2, args...);
182
183 auto fn_args = std::get<0>(packed);
184 auto compute = std::get<1>(packed);
185 auto reverse = std::get<2>(packed);
186
187 return std::apply([&] (auto... args) {
188 auto f = [=] {
189 return compute(args.value()...);
190 };
191
192 return make_mutable_cell_view(key, f, reverse, args...);
193 }, fn_args);
194 }
195
196 template <typename A1, typename A2, typename... As>
197 auto cell_view(A1 arg1, A2 arg2, As... args) {
198 auto packed = internal::pack<2>(arg1, arg2, args...);
199
200 auto fn_args = std::get<0>(packed);
201 auto compute = std::get<1>(packed);
202 auto reverse = std::get<2>(packed);
203
204 return std::apply([&] (auto... args) {
205 auto f = [=] {
206 return compute(args.value()...);
207 };
208
209 return make_mutable_cell_view(f, reverse, args...);
210 }, fn_args);
211 }
212
213} // live_cells
214
215
216#endif /* LIVE_CELLS_MUTABLE_CELL_VIEW_HPP */
A cell with a value that is a function of one or more argument cells.
Definition compute_cell.hpp:44
auto value() const
Get the value of the cell.
Definition compute_cell.hpp:67
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
Same as make_cell but also provides a value setter value(Parent::value_type) so that the definition s...
Definition make_cell.hpp:220
A stateless mutable computed cell that does not cache its own value.
Definition mutable_cell_view.hpp:44
void value(value_type value) const
Set the value of the cell.
Definition mutable_cell_view.hpp:79
mutable_cell_view_base(F compute, R reverse, Os... args)
Create a stateless mutable computed cell.
Definition mutable_cell_view.hpp:70
Definition boolean.hpp:26
auto make_mutable_cell_view(std::invocable auto compute, auto reverse, auto... args)
Definition mutable_cell_view.hpp:117
auto cell_view(key_ref key, A1 arg1, A2 arg2, As... args)
Definition mutable_cell_view.hpp:180
void batch(F fn)
Batch changes to the values of mutable cells.
Definition mutable_cell.hpp:307