Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
peek_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_PEEK_CELL_HPP
19#define LIVE_CELLS_PEEK_CELL_HPP
20
21#include "keys.hpp"
22#include "cell_state.hpp"
23#include "stateful_cell.hpp"
24
25namespace live_cells {
29 struct peek_cell_key : value_key<key_ref> {
31 };
32
39 template <typename C>
40 class peek_cell_state : public cell_state, public observer {
41 public:
51
52 void will_update(const key_ref &k) override {}
53 void update(const key_ref &k, bool) override {}
54
55 protected:
56 void init() override {
58 cell.add_observer(std::dynamic_pointer_cast<observer>(this->shared_from_this()));
59 }
60
61 void pause() override {
62 cell.remove_observer(std::dynamic_pointer_cast<observer>(this->shared_from_this()));
64 }
65
66 private:
68 C cell;
69 };
70
75 template <Cell C>
76 class peek_cell : public stateful_cell<peek_cell_state<C>> {
78
79 public:
83 typedef C::value_type value_type;
84
90 peek_cell(const C &cell) :
92 cell(cell) {}
93
99 typename C::value_type value() const {
100 return cell.value();
101 }
102
109 typename C::value_type operator()() const {
110 argument_tracker::global().track_argument(*this);
111 return value();
112 }
113
114 private:
118 const C cell;
119 };
120
129 template <typename C>
130 auto peek(const C &cell) {
131 return peek_cell<C>(cell);
132 }
133
134 namespace ops {
135
147 constexpr auto peek = [] (const Cell auto &cell) {
148 return live_cells::peek(cell);
149 };
150
151 } // ops
152
153} // live_cells
154
155#endif /* LIVE_CELLS_PEEK_CELL_HPP */
static argument_tracker & global()
Definition tracker.hpp:99
Maintains the state of a stateful cell.
Definition cell_state.hpp:43
virtual void init()
Called before the first observer is added.
Definition cell_state.hpp:68
virtual void pause()
Called after the last observer is removed.
Definition cell_state.hpp:79
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
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
Defines the interface for observing changes to the value of a Cell.
Definition types.hpp:32
Maintains the state of a peek_cell.
Definition peek_cell.hpp:40
void update(const key_ref &k, bool) override
Notifies this observer that the value of the Cell identified by k has changed.
Definition peek_cell.hpp:53
void init() override
Called before the first observer is added.
Definition peek_cell.hpp:56
peek_cell_state(key_ref k, C cell)
Create a peek_cell state.
Definition peek_cell.hpp:48
void will_update(const key_ref &k) override
Notifies this observer that the value of the Cell identified by k is going to change.
Definition peek_cell.hpp:52
void pause() override
Called after the last observer is removed.
Definition peek_cell.hpp:61
A Cell that reads the value of another Cell without notifying its observers when it changes.
Definition peek_cell.hpp:76
C::value_type operator()() const
Get the value of the argument cell and track it as a dependency.
Definition peek_cell.hpp:109
C::value_type value() const
Get the value of the argument cell.
Definition peek_cell.hpp:99
C::value_type value_type
The type of value held by this cell.
Definition peek_cell.hpp:83
peek_cell(const C &cell)
Create a peek cell that reads the value of cell.
Definition peek_cell.hpp:90
Base class for a cell with a state.
Definition stateful_cell.hpp:42
Base class for a key distinguished from other keys by one or more values.
Definition keys.hpp:147
value_key(key_ref value, Ts... rest)
Create a key distinguished from other keys by one or more values.
Definition keys.hpp:167
Defines the cell protocol.
Definition types.hpp:128
constexpr auto peek
Operator for reading the value of a cell without reacting to changes.
Definition peek_cell.hpp:147
Definition boolean.hpp:26
auto peek(const C &cell)
Create a Cell that reads the value of cell but does not notify its observers when the value has chang...
Definition peek_cell.hpp:130
Key identifying a peek_cell.
Definition peek_cell.hpp:29