Live Cells C++
Reactive Programming for C++
|
Maintains the state of a stateful cell. More...
#include <cell_state.hpp>
Public Types | |
typedef std::shared_ptr< cell_state > | ref |
Shared pointer to a cell_state . | |
Public Member Functions | |
cell_state (key_ref k) | |
Create a cell state with a given key. | |
cell_state & | operator= (const cell_state &other)=delete |
virtual void | init () |
Called before the first observer is added. | |
virtual void | pause () |
Called after the last observer is removed. | |
virtual void | add_observer (observer::ref o) |
Add an observer to the cell's set of observers. | |
virtual void | remove_observer (observer::ref o) |
Remove an observer from the cell's set of observers. | |
virtual void | notify_will_update () |
Notify the observers that the cell's value will change. | |
virtual void | notify_update (bool did_change=true) |
Notify the observers that the cell's value has changed. | |
Protected Member Functions | |
bool | is_active () const |
Does the cell have at least one observer? | |
Protected Attributes | |
key_ref | key_ |
Key identifying the cell corresponding to this state. | |
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. | |
Maintains the state of a stateful cell.
The state consists of the cell's observers and its value. The base class provides functionality for keeping track of observers added to the cell.
This object is intended to be held in an std::shared_ptr with a single instance per unique cell key. Multiple cell objects with same key reference the same cell_state.
A state is created when the first stateful cell with a given key is created, and is destroyed when the last cell with the given key is destroyed.
|
inline |
Create a cell state with a given key.
k | The key |
|
virtual |
Add an observer to the cell's set of observers.
o | The observer to add |
Called before the first observer is added.
Subclasses should override this method to include initialization logic specific to this cell state.
Reimplemented in live_cells::compute_cell_state< C >, live_cells::compute_cell_state< dynamic_compute_state< F > >, live_cells::compute_cell_state< store_cell_compute_state< C > >, live_cells::dynamic_compute_cell_state< F >, live_cells::mutable_compute_cell_state< T >, live_cells::mutable_compute_cell_state< std::invoke_result_t< F > >, live_cells::peek_cell_state< C >, live_cells::previous_value_cell_state< C >, and live_cells::store_cell_state< C >.
|
inlineprotected |
Does the cell have at least one observer?
true
if the cell has at least one observer. Notify the observers that the cell's value has changed.
This should be called after the value has been changed.
did_change | True if the value of the cell may have changed, false if it is known that the value of the cell has not changed. |
|
virtual |
Notify the observers that the cell's value will change.
This should be called before the value is changed.
Called after the last observer is removed.
Subclasses should override this method to include cleanup logic specific to this cell state.
init()
should be removed in this method. Reimplemented in live_cells::compute_cell_state< C >, live_cells::compute_cell_state< dynamic_compute_state< F > >, live_cells::compute_cell_state< store_cell_compute_state< C > >, live_cells::dynamic_compute_cell_state< F >, live_cells::mutable_compute_cell_state< T >, live_cells::mutable_compute_cell_state< std::invoke_result_t< F > >, live_cells::peek_cell_state< C >, live_cells::previous_value_cell_state< C >, and live_cells::store_cell_state< C >.
|
virtual |
Remove an observer from the cell's set of observers.
Like the remove_observer
method of the Cell
concept, this method should be called the same number of times, for a given observer o, as add_observer()
was called, before the observer is actually removed.
o | The observer to remove |