18#ifndef LIVE_CELLS_CELL_STATE_HPP
19#define LIVE_CELLS_CELL_STATE_HPP
21#include <unordered_map>
25#include "observable.hpp"
43 class cell_state :
public std::enable_shared_from_this<cell_state> {
46 typedef std::shared_ptr<cell_state>
ref;
129 std::unordered_map<observer::ref, std::size_t>
observers;
157 int notify_count = 0;
192 template <
typename S,
typename... Args>
195 return std::make_shared<S>(k, args...);
200 auto it = states.find(k);
202 if (it != states.end()) {
203 if (
auto state = it->second.lock()) {
204 return std::dynamic_pointer_cast<S>(state);
211 auto state = states.emplace(k, std::weak_ptr<S>(std::make_shared<S>(k, args...))).first->second.lock();
212 return std::dynamic_pointer_cast<S>(state);
233 std::unordered_map<key_ref, std::weak_ptr<cell_state>> states;
Maintains the state of a stateful cell.
Definition cell_state.hpp:43
virtual void remove_observer(observer::ref o)
Remove an observer from the cell's set of observers.
Definition live_cells.cpp:62
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
key_ref key_
Key identifying the cell corresponding to this state.
Definition cell_state.hpp:123
std::shared_ptr< cell_state > ref
Shared pointer to a cell_state.
Definition cell_state.hpp:46
cell_state(key_ref k)
Create a cell state with a given key.
Definition cell_state.hpp:53
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
virtual void add_observer(observer::ref o)
Add an observer to the cell's set of observers.
Definition live_cells.cpp:47
std::unordered_map< observer::ref, std::size_t > observers
The set of observers observing changes to the values in the cell corresponding to this state.
Definition cell_state.hpp:129
Dynamically type key container.
Definition keys.hpp:76
virtual bool is_unique() const noexcept
Is this a unique key?
Definition keys.hpp:61
std::shared_ptr< observer > ref
Shared pointer to an observer.
Definition types.hpp:37
Maintains the association between keys and cell states.
Definition cell_state.hpp:164
void remove(key_ref k)
Definition cell_state.hpp:220
std::shared_ptr< S > get(key_ref k, Args... args)
Definition cell_state.hpp:193
static state_manager & global()
Definition cell_state.hpp:169
Definition boolean.hpp:26