Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
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_COMPUTED_HPP
19#define LIVE_CELLS_COMPUTED_HPP
20
21#include <tuple>
22#include <utility>
23
24#include "compute_cell.hpp"
25#include "dynamic_compute_cell.hpp"
26#include "exceptions.hpp"
27#include "util.hpp"
28
29namespace live_cells {
30
44 template <std::invocable F>
45 auto computed(F&& compute) {
46 return dynamic_compute_cell<F>(std::forward<F>(compute));
47 }
48
69 template <std::invocable F>
70 auto computed(changes_only_option option, F&& compute) {
72 std::forward<F>(compute)
73 );
74 }
75
90 template <std::invocable F>
91 auto computed(key_ref key, F&& compute) {
92 return dynamic_compute_cell<F>(key, std::forward<F>(compute));
93 }
94
116 template <std::invocable F>
119 key,
120 std::forward<F>(compute)
121 );
122 }
123
138 template <typename A, typename... As>
139 auto computed(A arg1, As... args) {
140 auto packed = internal::pack<1>(arg1, args...);
141
142 auto fn_args = std::get<0>(packed);
143 auto fn = std::get<1>(packed);
144
145 return std::apply([&] (auto... args) {
146 auto f = [=] {
147 return fn(args.value()...);
148 };
149
150 return make_compute_cell(f, args...);
151 }, fn_args);
152 }
153
170 template <typename A, typename... As>
172 auto packed = internal::pack<1>(arg1, args...);
173
174 auto fn_args = std::get<0>(packed);
175 auto fn = std::get<1>(packed);
176
177 return std::apply([&] (auto... args) {
178 auto f = [=] {
179 return fn(args.value()...);
180 };
181
182 return make_compute_cell(key, f, args...);
183 }, fn_args);
184 }
185
194 inline void none() {
196 }
197
198} // live_cells
199
200#endif /* LIVE_CELLS_COMPUTED_HPP */
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
Definition boolean.hpp:26
auto computed(F &&compute)
Create a cell with a value that is a function of one or more argument cells.
Definition computed.hpp:45
void none()
Stop the computation of the current computed cell's value.
Definition computed.hpp:194
auto make_compute_cell(std::invocable auto f, auto... args)
Definition compute_cell.hpp:100
Cell option specifying that the cell, to which it is applied, should only notify its observers when i...
Definition changes_only_state.hpp:94
Exception indicating that the value of a cell should not be computed.
Definition exceptions.hpp:33