Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
compute_state.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_COMPUTE_STATE_HPP
19#define LIVE_CELLS_COMPUTE_STATE_HPP
20
21#include <memory>
22#include <utility>
23#include <concepts>
24#include <cassert>
25
26#include "observable.hpp"
27#include "cell_state.hpp"
28#include "exceptions.hpp"
29#include "observer_cell_state.hpp"
30
31namespace live_cells {
32
40 template <typename T>
41 concept Computable = std::invocable<T,observer::ref>;
42
49 template <Computable C>
51 public:
55 typedef std::invoke_result_t<C,observer::ref> value_type;
56
64 template <typename... Args>
65 requires std::constructible_from<C,Args...>
66 compute_cell_state(key_ref k, Args&&... args) :
67 cell_state(k),
68 compute(std::forward<Args>(args)...) {}
69
77 if (stale) {
78 try {
79 value_ = compute(observer_ptr());
80 }
81 catch (const stop_compute_exception &) {
82 // Prevent value from being updated
83 }
84
85 stale = !is_active();
86 }
87
88 return value_;
89 }
90
91 protected:
96
101 std::shared_ptr<observer> observer_ptr() {
102 return std::dynamic_pointer_cast<observer>(this->shared_from_this());
103 }
104
105 void will_update(const key_ref &k) override {
106 handle_will_update([this] {
108 });
109 }
110
111 void update(const key_ref &k, bool changed) override {
112 handle_update(changed, [this] (bool changed) {
113 notify_update(changed);
114 });
115 }
116
117 void init() override {
120 }
121
122 void pause() override {
125 }
126
127 private:
131 value_type value_;
132 };
133
134} // live_cells
135
136#endif /* LIVE_CELLS_COMPUTE_STATE_HPP */
Maintains the state of a stateful cell.
Definition cell_state.hpp:43
virtual void notify_will_update()
Notify the observers that the cell's value will change.
Definition live_cells.cpp:76
virtual void notify_update(bool did_change=true)
Notify the observers that the cell's value has changed.
Definition live_cells.cpp:92
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
bool is_active() const
Does the cell have at least one observer?
Definition cell_state.hpp:136
Cell state for a cell which computes a value as a function of one or more argument cells.
Definition compute_state.hpp:50
void pause() override
Called after the last observer is removed.
Definition compute_state.hpp:122
C compute
Compute value function.
Definition compute_state.hpp:95
value_type value()
Retrieve the latest cached value.
Definition compute_state.hpp:76
std::shared_ptr< observer > observer_ptr()
Get an observer::ref for this, that can be passed to add_observer and remove_observer of Cell.
Definition compute_state.hpp:101
void update(const key_ref &k, bool changed) override
Notifies this observer that the value of the Cell identified by k has changed.
Definition compute_state.hpp:111
std::invoke_result_t< C, observer::ref > value_type
Shorthand for computed value type.
Definition compute_state.hpp:55
void init() override
Called before the first observer is added.
Definition compute_state.hpp:117
compute_cell_state(key_ref k, Args &&... args)
Create a computed cell state.
Definition compute_state.hpp:66
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 compute_state.hpp:105
Dynamically type key container.
Definition keys.hpp:76
Provides functionality for observing a cell from a cell_state.
Definition observer_cell_state.hpp:21
void init_observer_state()
Initialize the cell observation state.
Definition observer_cell_state.hpp:40
void handle_will_update(const std::invocable auto &notify_will_update)
Handle a observer::will_update call.
Definition observer_cell_state.hpp:66
void pause_observer_state()
Pause the cell observation state.
Definition observer_cell_state.hpp:50
bool stale
Does the value have to be recomputed?
Definition observer_cell_state.hpp:26
void handle_update(bool changed, const std::invocable< bool > auto &notify_update)
Handle an observer::update call.
Definition observer_cell_state.hpp:120
Defines the interface for observing changes to the value of a Cell.
Definition types.hpp:32
Defines a type that computes a value when the call operator operator() is invoked.
Definition compute_state.hpp:41
Definition boolean.hpp:26
Exception indicating that the value of a cell should not be computed.
Definition exceptions.hpp:33