Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
make_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_MAKE_CELL_HPP
19#define LIVE_CELLS_MAKE_CELL_HPP
20
21#include <type_traits>
22
23#include "observable.hpp"
24#include "tracker.hpp"
25
26namespace live_cells {
27
31 template <typename C>
32 using cell_value_type = decltype(std::declval<C>().value());
33
59 template <typename C>
60 struct make_cell {
65
75 template <typename K, typename... Args>
76 requires (!Keyable<C> && std::constructible_from<C,Args...>)
77 make_cell(K&& key, Args&&... args) :
78 cell(std::forward<Args>(args)...),
79 key_(std::forward<K>(key)) {}
80
91 template <typename... Args>
92 requires (!Keyable<C> && std::constructible_from<C,Args...>)
94 cell(std::forward<Args>(args)...),
95 key_(key_ref::create<unique_key>()) {}
96
109 template <typename... Args>
110 requires (Keyable<C> && std::constructible_from<C,Args...>)
112 cell(std::forward<Args>(args)...) {}
113
126
137
146 return cell.value();
147 }
148
158 argument_tracker::global().track_argument(*this);
159 return value();
160 }
161
174 return cell();
175 }
176
185 return cell.key();
186 }
187
196 return key_;
197 }
198
199 protected:
204
211 const std::enable_if<!Keyable<C>, key_ref>::type key_;
212 };
213
219 template <typename C, typename Parent = make_cell<C>>
220 class make_mutable_cell : public Parent {
221 using Parent::make_cell;
222
223 public:
224 using Parent::value;
225
235 Parent::value_type operator=(const Parent::value_type &value) {
236 this->value(value);
237 return value;
238 }
239
254 Parent::value_type operator=(const Parent::value_type &value) const {
255 this->value(value);
256 return value;
257 }
258
266 void value(const Parent::value_type &value) const {
267 Parent::cell.value(value);
268 }
269 };
270
271} // live_cells
272
273#endif /* LIVE_CELLS_MAKE_CELL_HPP */
static argument_tracker & global()
Definition tracker.hpp:99
Dynamically typed Cell container.
Definition observable.hpp:133
T value() const
Get the value held by the underlying Cell.
Definition observable.hpp:197
void remove_observer(observer::ref obs)
Remove an observer from the underlying Cell.
Definition observable.hpp:172
void add_observer(observer::ref obs)
Add an observer to the underlying Cell.
Definition observable.hpp:159
key_ref key() const
Get the key that uniquely identifies the underlying Cell.
Definition observable.hpp:182
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
Parent::value_type operator=(const Parent::value_type &value) const
Set the value of the cell.
Definition make_cell.hpp:254
Parent::value_type operator=(const Parent::value_type &value)
Set the value of the cell to value.
Definition make_cell.hpp:235
void value(const Parent::value_type &value) const
Set the value of the cell to value.
Definition make_cell.hpp:266
std::shared_ptr< observer > ref
Shared pointer to an observer.
Definition types.hpp:37
Concept defining an object that can be identified by a key.
Definition types.hpp:72
Concept defining an observable object that can be tracked dynamically as a dependency.
Definition types.hpp:88
Definition boolean.hpp:26
decltype(std::declval< C >().value()) cell_value_type
The return type of C::value().
Definition make_cell.hpp:32
Defines a cell class using C such that the constraints defined by the concept Cell are satisifed.
Definition make_cell.hpp:60
make_cell(Args &&... args)
Constructor that forwards all its arguments args to the constructor of C.
Definition make_cell.hpp:93
C cell
Definition make_cell.hpp:203
void remove_observer(observer::ref obs) const
Remove an observer from the cell.
Definition make_cell.hpp:134
make_cell(K &&key, Args &&... args)
Constructor that allows a key to be provided, with args forwarded to the constructor of C.
Definition make_cell.hpp:77
void add_observer(observer::ref obs) const
Add an observer to the cell.
Definition make_cell.hpp:123
cell_value_type< C > value_type
Cell value type (return type of C::value()).
Definition make_cell.hpp:64
const std::enable_if<!Keyable< C >, key_ref >::type key_
Key identifying the cell.
Definition make_cell.hpp:211
key_ref key() const
Get the key identifying the cell.
Definition make_cell.hpp:184
value_type operator()() const
Get the value of the cell and track it as a dependency.
Definition make_cell.hpp:173
value_type value() const
Get the value of the cell.
Definition make_cell.hpp:145
value_type operator()() const
Get the value of the cell and track it as a dependency.
Definition make_cell.hpp:157
key_ref key() const
Get the key identifying the cell.
Definition make_cell.hpp:195
make_cell(Args &&... args)
Constructor that forwards all its arguments args to the constructor of C.
Definition make_cell.hpp:111