Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
live_cells::compute_cell_state< C > Class Template Reference

Cell state for a cell which computes a value as a function of one or more argument cells. More...

#include <compute_state.hpp>

Inheritance diagram for live_cells::compute_cell_state< C >:
Collaboration diagram for live_cells::compute_cell_state< C >:

Public Types

typedef std::invoke_result_t< C, observer::refvalue_type
 Shorthand for computed value type.
 
- Public Types inherited from live_cells::cell_state
typedef std::shared_ptr< cell_stateref
 Shared pointer to a cell_state.
 
- Public Types inherited from live_cells::observer
typedef std::shared_ptr< observerref
 Shared pointer to an observer.
 

Public Member Functions

template<typename... Args>
requires std::constructible_from<C,Args...>
 compute_cell_state (key_ref k, Args &&... args)
 Create a computed cell state.
 
value_type value ()
 Retrieve the latest cached value.
 
- Public Member Functions inherited from live_cells::cell_state
 cell_state (key_ref k)
 Create a cell state with a given key.
 
cell_stateoperator= (const cell_state &other)=delete
 
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

std::shared_ptr< observerobserver_ptr ()
 Get an observer::ref for this, that can be passed to add_observer and remove_observer of Cell.
 
void will_update (const key_ref &k) override
 Notifies this observer that the value of the Cell identified by k is going to change.
 
void update (const key_ref &k, bool changed) override
 Notifies this observer that the value of the Cell identified by k has changed.
 
void init () override
 Called before the first observer is added.
 
void pause () override
 Called after the last observer is removed.
 
- Protected Member Functions inherited from live_cells::cell_state
bool is_active () const
 Does the cell have at least one observer?
 
- Protected Member Functions inherited from live_cells::observer_cell_state
void init_observer_state ()
 Initialize the cell observation state.
 
void pause_observer_state ()
 Pause the cell observation state.
 
void handle_will_update (const std::invocable auto &notify_will_update)
 Handle a observer::will_update call.
 
void handle_will_update (const std::invocable auto &pre_update, const std::invocable auto &notify_will_update)
 Handle a observer::will_update call.
 
void handle_update (bool changed, const std::invocable< bool > auto &notify_update)
 Handle an observer::update call.
 
void handle_update (bool changed, const std::invocable auto &did_change, const std::invocable< bool > auto &notify_update, const std::invocable auto &post_update)
 Handle an observer::update call.
 

Protected Attributes

compute
 Compute value function.
 
- Protected Attributes inherited from live_cells::cell_state
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.
 
- Protected Attributes inherited from live_cells::observer_cell_state
bool stale = true
 Does the value have to be recomputed?
 
bool updating = false
 Are the argument cells in the process of updating their values?
 

Detailed Description

template<Computable C>
class live_cells::compute_cell_state< C >

Cell state for a cell which computes a value as a function of one or more argument cells.

The actual computation is defined by the Computable C.

Constructor & Destructor Documentation

◆ compute_cell_state()

template<Computable C>
template<typename... Args>
requires std::constructible_from<C,Args...>
live_cells::compute_cell_state< C >::compute_cell_state ( key_ref k,
Args &&... args )
inline

Create a computed cell state.

Parameters
kKey identifying the cell
argsArguments forwarded to the constructor of C.

Member Function Documentation

◆ init()

template<Computable C>
void live_cells::compute_cell_state< C >::init ( )
inlineoverrideprotectedvirtual

Called before the first observer is added.

Subclasses should override this method to include initialization logic specific to this cell state.

Note
If the state needs to set up observers to other cells, it should be done in this method.

Reimplemented from live_cells::cell_state.

Reimplemented in live_cells::dynamic_compute_cell_state< F >, and live_cells::store_cell_state< C >.

◆ pause()

template<Computable C>
void live_cells::compute_cell_state< C >::pause ( )
inlineoverrideprotectedvirtual

Called after the last observer is removed.

Subclasses should override this method to include cleanup logic specific to this cell state.

Note
Observers added in init() should be removed in this method.

Reimplemented from live_cells::cell_state.

Reimplemented in live_cells::dynamic_compute_cell_state< F >, and live_cells::store_cell_state< C >.

◆ update()

template<Computable C>
void live_cells::compute_cell_state< C >::update ( const key_ref & k,
bool did_change )
inlineoverrideprotectedvirtual

Notifies this observer that the value of the Cell identified by k has changed.

Parameters
kidentifying observable
did_changetrue if the value of the cell may have changed, false if it is know that it hasn't changed.

Implements live_cells::observer.

◆ value()

template<Computable C>
value_type live_cells::compute_cell_state< C >::value ( )
inline

Retrieve the latest cached value.

The value is computed using C::operator(), which is passed a shared_ptr, of type observer, pointing to this.

◆ will_update()

template<Computable C>
void live_cells::compute_cell_state< C >::will_update ( const key_ref & k)
inlineoverrideprotectedvirtual

Notifies this observer that the value of the Cell identified by k is going to change.

Parameters
kidentifying observable

Implements live_cells::observer.


The documentation for this class was generated from the following file: