Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
constant_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_CONSTANT_CELL_HPP
19#define LIVE_CELLS_CONSTANT_CELL_HPP
20
21#include <functional>
22#include <memory>
23
24#include "observable.hpp"
25
26namespace live_cells {
27
31 template <typename T>
32 struct constant_key : key {
34 const T value;
35
43
44 bool eq(const key &k) const noexcept override {
45 auto *ptr = dynamic_cast<const constant_key*>(&k);
46
47 return ptr != nullptr && value == ptr->value;
48 }
49
50 std::size_t hash() const noexcept override {
51 return std::hash<T>{}(value);
52 }
53 };
54
61 template <typename T>
63 public:
65 typedef T value_type;
66
75 constant_cell(const T &value) : m_value(value) {}
76
85 key_ref key() const {
86 return key_ref::create<constant_key<T>>(m_value);
87 }
88
94 T value() const {
95 return m_value;
96 }
97
98 void add_observer(observer::ref) const {
99 // Do nothing
100 }
101
102 void remove_observer(observer::ref) const {
103 // Do nothing
104 }
105
114 T operator()() const {
115 // No need to track a constant cell
116 return m_value;
117 }
118
119 private:
120 const T m_value;
121
122 };
123
131 template <typename T>
133 return constant_cell<T>(value);
134 }
135
140 template <typename T>
141 concept CellOrValue = Cell<T> || std::constructible_from<constant_cell<T>, T>;
142
155 auto ensure_cell(const Cell auto &thing) {
156 return thing;
157 }
158
172 template <typename T>
173 auto ensure_cell(const T& thing) {
174 return constant_cell<T>(thing);
175 }
176
177} // live_cells
178
179#endif /* LIVE_CELLS_CONSTANT_CELL_HPP */
A cell which holds a constant value.
Definition constant_cell.hpp:62
key_ref key() const
Get the key that identifies this cell.
Definition constant_cell.hpp:85
T value() const
Get the constant value held by this cell.
Definition constant_cell.hpp:94
T operator()() const
Get the constant value held by this cell.
Definition constant_cell.hpp:114
T value_type
The type of the constant value.
Definition constant_cell.hpp:65
constant_cell(const T &value)
Construct a cell with a constant value.
Definition constant_cell.hpp:75
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
std::shared_ptr< observer > ref
Shared pointer to an observer.
Definition types.hpp:37
Concept specifying that T is either a Cell or a value that can be converted to a constant_cell.
Definition constant_cell.hpp:141
Defines the cell protocol.
Definition types.hpp:128
Definition boolean.hpp:26
constant_cell< T > value(const T &value)
Definition constant_cell.hpp:132
auto ensure_cell(const Cell auto &thing)
Ensure that thing is a Cell.
Definition constant_cell.hpp:155
Key identifying a constant_cell.
Definition constant_cell.hpp:32
const T value
Constant value.
Definition constant_cell.hpp:34
constant_key(T value)
Create a constant cell key identifying a constant_cell with a given value.
Definition constant_cell.hpp:42
std::size_t hash() const noexcept override
Compute the hash code for this key.
Definition constant_cell.hpp:50
bool eq(const key &k) const noexcept override
Compare this key to another key for equality.
Definition constant_cell.hpp:44