18#ifndef LIVE_CELLS_OBSERVABLE_H
19#define LIVE_CELLS_OBSERVABLE_H
34 virtual ~ref_base() =
default;
39 virtual key_ref key()
const = 0;
46 struct typed_ref_base : ref_base {
47 virtual T
value()
const = 0;
48 virtual T operator()()
const = 0;
55 struct mutable_cell_ref {
56 virtual ~mutable_cell_ref() =
default;
57 virtual void value(T value) = 0;
64 struct typed_ref : typed_ref_base<typename O::value_type> {
67 typed_ref(O obs) : observable(obs) {}
70 observable.add_observer(obs);
74 observable.remove_observer(obs);
77 key_ref key()
const override {
78 return observable.key();
81 typename O::value_type
value()
const override {
82 return observable.value();
85 typename O::value_type operator()()
const override {
90 template <MutableCell C>
91 struct typed_ref<C> : typed_ref_base<typename C::value_type>, mutable_cell_ref<typename C::value_type> {
94 typed_ref(C obs) : observable(obs) {}
97 observable.add_observer(obs);
101 observable.remove_observer(obs);
104 key_ref key()
const override {
105 return observable.key();
108 typename C::value_type
value()
const override {
109 return observable.value();
112 void value(
typename C::value_type value)
override {
113 observable.value(value);
116 typename C::value_type operator()()
const override {
123 template <
typename T>
149 template <
typename T>
196 template <
typename T>
199 auto &
typed =
dynamic_cast<internal::typed_ref_base<T>&
>(
base);
201 return typed.value();
214 template <
typename T>
217 auto &
typed =
dynamic_cast<internal::mutable_cell_ref<T>&
>(
base);
233 template <
typename T>
236 auto &
typed =
dynamic_cast<internal::typed_ref_base<T>&
>(
base);
249 template <
typename T>
259 const char *what()
const noexcept override {
260 return "Attempt to cast a `cell` to a `typed_cell<>` with a value type"
261 " that is incompatible with the cell's value type.";
275 template <
typename T>
283 template <TypedCell<T> C>
311 ref->add_observer(
obs);
324 ref->remove_observer(
obs);
356 auto &
typed =
dynamic_cast<internal::mutable_cell_ref<T>&
>(
base);
376 std::shared_ptr<internal::typed_ref_base<T>> ref;
421 template <
typename T1,
typename T2>
437 template <
typename T1,
typename T2>
448 return a.
key()->hash();
455 return a.
key()->hash();
Dynamically typed Cell container.
Definition observable.hpp:133
T value() const
Get the value held by the underlying Cell.
Definition observable.hpp:197
void value(T value)
Set the value of the underlying Cell.
Definition observable.hpp:215
cell(O o)
Create a container holding the Cell o.
Definition observable.hpp:141
void remove_observer(observer::ref obs)
Remove an observer from the underlying Cell.
Definition observable.hpp:172
bool operator!=(const cell &a, const cell &b)
Compare two cell's by their keys.
Definition observable.hpp:406
bool operator==(const cell &a, const cell &b)
Compare two cell's by their keys.
Definition observable.hpp:391
void add_observer(observer::ref obs)
Add an observer to the underlying Cell.
Definition observable.hpp:159
std::shared_ptr< internal::ref_base > obs_ref
Pointer to the container holding the underlying cell.
Definition observable.hpp:247
T operator()() const
Get the value held by the underlying Cell and track it as a dependency.
Definition observable.hpp:234
cell(typed_cell< T > c)
Create a container holding the Cell held in c.
Definition observable.hpp:150
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
std::shared_ptr< observer > ref
Shared pointer to an observer.
Definition types.hpp:37
void add_observer(observer::ref o) const
Add an observer to the cell.
Definition stateful_cell.hpp:83
key_ref key() const
Get the key identifying the cell.
Definition stateful_cell.hpp:74
void remove_observer(observer::ref o) const
Remove an observer from the cell.
Definition stateful_cell.hpp:92
Dynamically typed Cell container with a static value type.
Definition observable.hpp:276
typed_cell(cell c)
Create a container holding the Cell held in c.
Definition observable.hpp:297
typed_cell(C cell)
Create a container holding the Cell cell.
Definition observable.hpp:284
void value(T value)
Set the value of the underlying Cell.
Definition observable.hpp:354
void add_observer(observer::ref obs)
Add an observer to the underlying Cell.
Definition observable.hpp:310
void remove_observer(observer::ref obs)
Remove an observer from the underlying Cell.
Definition observable.hpp:323
bool operator==(const typed_cell< T1 > &a, const typed_cell< T2 > &b)
Compare two typed_cell's by their keys.
Definition observable.hpp:422
T value() const
Get the value held by the underlying Cell.
Definition observable.hpp:342
key_ref key() const
Get the key that uniquely identifies the underlying Cell.
Definition observable.hpp:333
bool operator!=(const typed_cell< T1 > &a, const typed_cell< T2 > &b)
Compare two typed_cell's by their keys.
Definition observable.hpp:438
T operator()() const
Get the value held by the underlying Cell and track it as a dependency.
Definition observable.hpp:367
Definition boolean.hpp:26
constant_cell< T > value(const T &value)
Definition constant_cell.hpp:132
Exception thrown when attempting to cast a cell to a typed_cell with a value type that is incompatibl...
Definition observable.hpp:258