Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
mutable_computed.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_COMPUTED_HPP
19#define LIVE_CELLS_MUTABLE_COMPUTED_HPP
20
21#include <tuple>
22#include <utility>
23#include <concepts>
24
25#include "static_mutable_compute_cell.hpp"
26#include "dynamic_mutable_compute_cell.hpp"
27#include "util.hpp"
28
29namespace live_cells {
62 template <std::invocable F, typename R>
63 auto mutable_computed(F&& fn, R&& reverse) {
65 std::forward<F>(fn),
66 std::forward<R>(reverse)
67 );
68 }
69
108 template <std::invocable F, typename R>
111 std::forward<F>(fn),
112 std::forward<R>(reverse)
113 );
114 }
115
141 template <typename A1, typename A2, typename... As>
143 auto packed = internal::pack<2>(arg1, arg2, args...);
144
145 auto fn_args = std::get<0>(packed);
146 auto compute = std::get<1>(packed);
147 auto reverse = std::get<2>(packed);
148
149 return std::apply([&] (auto... args) {
150 auto fn = [=] {
151 return compute(args.value()...);
152 };
153
154 return make_mutable_compute_cell(fn, reverse, args...);
155 }, fn_args);
156 }
157
190 template <typename A1, typename A2, typename... As>
192 auto packed = internal::pack<2>(arg1, arg2, args...);
193
194 auto fn_args = std::get<0>(packed);
195 auto compute = std::get<1>(packed);
196 auto reverse = std::get<2>(packed);
197
198 return std::apply([&] (auto... args) {
199 auto fn = [=] {
200 return compute(args.value()...);
201 };
202
203 return make_mutable_compute_cell(changes_only, fn, reverse, args...);
204 }, fn_args);
205 }
206
207} // live_cells
208
209#endif /* LIVE_CELLS_MUTABLE_COMPUTED_HPP */
A computed cell which determines its argument cells at runtime.
Definition dynamic_compute_cell.hpp:153
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
auto mutable_computed(F &&fn, R &&reverse)
Create a mutable computed cell with dynamically determined argument cells.
Definition mutable_computed.hpp:63
Cell option specifying that the cell, to which it is applied, should only notify its observers when i...
Definition changes_only_state.hpp:94