Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
types.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_TYPES_HPP
19#define LIVE_CELLS_TYPES_HPP
20
21#include <concepts>
22#include <utility>
23
24#include "keys.hpp"
25
26namespace live_cells {
27
32 class observer {
33 public:
37 typedef std::shared_ptr<observer> ref;
38
39 virtual ~observer() noexcept = default;
40
48
58 virtual void update(const key_ref &k, bool did_change) = 0;
59 };
60
72 concept Keyable = requires(const T &o) {
73 { o.key() } -> std::same_as<key_ref>;
74 };
75
87 template <typename T>
88 concept Trackable = requires(const T &o) {
89 { o() } -> std::same_as<decltype(o.value())>;
90 };
91
127 template <typename T>
128 concept Cell = requires(const T &o) {
129 { o.add_observer(observer::ref()) };
130 { o.remove_observer(observer::ref()) };
131 { o.value() } -> std::same_as<typename T::value_type>;
132 { o() } -> std::same_as<typename T::value_type>;
133 { o.key() } -> std::same_as<key_ref>;
134 };
135
140 template <typename C, typename T>
141 concept TypedCell = Cell<C> && std::convertible_to<typename C::value_type, T>;
142
154 template <typename C>
155 concept MutableCell = Cell<C> && requires(C c) {
156 { c.value(std::declval<typename C::value_type>()) };
157 };
158
163 template <typename C, typename T>
165
166 namespace ops {
167
177 template <typename Op, typename C>
178 concept Operator = requires(const Op &op) {
179 { op(std::declval<C>()) } -> Cell;
180 };
181
193 template <Cell C, Operator<C> O>
194 inline auto operator|(const C &arg, const O &op) {
195 return op(arg);
196 }
197
198 } // ops
199
200} // live_cells
201
202#endif /* LIVE_CELLS_TYPES_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 observing changes to the value of a Cell.
Definition types.hpp:32
virtual void will_update(const key_ref &k)=0
Notifies this observer that the value of the Cell identified by k is going to change.
std::shared_ptr< observer > ref
Shared pointer to an observer.
Definition types.hpp:37
virtual void update(const key_ref &k, bool did_change)=0
Notifies this observer that the value of the Cell identified by k has changed.
Defines the cell protocol.
Definition types.hpp:128
Concept defining an object that can be identified by a key.
Definition types.hpp:72
Concept defining a Cell that can have its value set.
Definition types.hpp:155
Concept defining an observable object that can be tracked dynamically as a dependency.
Definition types.hpp:88
Concept that constrains Cell to cells holding a specific value type T.
Definition types.hpp:141
Concept that constrains MutableCell to cells holding a specific value type T.
Definition types.hpp:164
Concept defining an operator on a cell.
Definition types.hpp:178
auto operator|(const C &arg, const O &op)
Apply an Operator op on a Cell arg.
Definition types.hpp:194
Definition boolean.hpp:26