morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
morph::reactive::Signal< T > Class Template Referencefinal

A source value. Reading it with get() inside a tracked run subscribes that run. More...

#include <signal.hpp>

Inheritance diagram for morph::reactive::Signal< T >:
morph::reactive::detail::Node

Public Member Functions

 Signal (Runtime &runtime)
 Constructs a value-initialised signal.
 Signal (Runtime &runtime, T initial, EqualityPolicy policy=EqualityPolicy::Auto)
 Constructs a signal holding initial.
 Signal (Signal const &)=delete
Signal & operator= (Signal const &)=delete
 Signal (Signal &&)=delete
Signal & operator= (Signal &&)=delete
T const & get () const
 Reads the value, subscribing the innermost tracked run.
T const & peek () const noexcept
 Reads the value without subscribing anything.
void set (T value)
 Replaces the value and notifies observers, unless it equals the current one and the policy is EqualityPolicy::Auto.
template<typename F>
requires std::invocable<F, T&>
void mutate (F &&fn)
 Changes the value in place and always notifies observers.
Public Member Functions inherited from morph::reactive::detail::Node
 Node (std::shared_ptr< RuntimeCore > core, std::size_t depth=0)
 Constructs a node. Off the owner this is reported (site::kOffOwner) and the node is not counted towards the core's live nodes, whose counter belongs to the owner.
virtual ~Node ()
 Unlinks this node from its sources and observers, drops every reference the core holds to it, and tells a pull running on it that it is gone.
 Node (Node const &)=delete
Node & operator= (Node const &)=delete
 Node (Node &&)=delete
Node & operator= (Node &&)=delete
void markStale (Colour colour)
 Push phase: raises this node to colour and marks its observers Check.
bool updateIfNecessary ()
 Pull phase: brings this node up to date, recomputing only if a source really changed.
std::size_t countRun (std::uint64_t epoch) noexcept
 Counts one run of this Effect in the flush numbered epoch.
std::size_t depth () const noexcept
 How deeply the scope owning this node is nested; the flush runs shallower Effects first.
bool runsAfter (Node const &other) const noexcept
 Whether this node runs after other when both are queued: it is deeper, or as deep and newer.
std::uint64_t creationOrder () const noexcept
 Where this node stands in its runtime's creation order; among Effects of one depth, the flush runs older ones first.
void settle () noexcept
 Marks this node Clean without running it (a dropped queue entry).

Additional Inherited Members

Protected Member Functions inherited from morph::reactive::detail::Node
RuntimeCore & core () const noexcept
 The runtime core.
bool bornOnOwner () const noexcept
 Whether this node was constructed on the owner; one that was not is refused its work.
void trackRead () const
 Registers this node as a source of the innermost tracked run, if any.
void forceDirty () noexcept
 Marks this node Dirty, so the next pull recomputes it.
bool isPulling () const noexcept
 Whether a pull is bringing this node up to date right now. A read of the node from inside that pull can only come from its own run or from a source's run that reads it: a cycle.
void markObserversStale (Colour colour)
 Marks every observer of this node colour.
void adoptSources (std::vector< Node * > fresh)
 Replaces this node's sources with fresh, unlinking the ones no longer read.
void mergeSources (std::vector< Node * > fresh)
 Adds fresh to this node's sources without dropping any: what a failed run keeps.

Detailed Description

template<typename T>
class morph::reactive::Signal< T >

A source value. Reading it with get() inside a tracked run subscribes that run.

set() skips a value equal to the current one when == is usable for T (kEqualityUsable) and the policy is EqualityPolicy::Auto; mutate() always notifies. Writes are refused, and reported, off the owner or inside a Computed. A read off the owner is reported and subscribes nothing.

Template Parameters
TThe value type; move-constructible and move-assignable.

Definition at line 49 of file signal.hpp.

Constructor & Destructor Documentation

◆ Signal() [1/2]

template<typename T>
morph::reactive::Signal< T >::Signal ( Runtime & runtime)
inlineexplicit

Constructs a value-initialised signal.

Parameters
runtimeThe runtime this signal belongs to.

Definition at line 53 of file signal.hpp.

◆ Signal() [2/2]

template<typename T>
morph::reactive::Signal< T >::Signal ( Runtime & runtime,
T initial,
EqualityPolicy policy = EqualityPolicy::Auto )
inline

Constructs a signal holding initial.

Parameters
runtimeThe runtime this signal belongs to.
initialThe initial value.
policyWhether a write equal to the current value is skipped.

Definition at line 61 of file signal.hpp.

Member Function Documentation

◆ get()

template<typename T>
T const & morph::reactive::Signal< T >::get ( ) const
inlinenodiscard

Reads the value, subscribing the innermost tracked run.

Off the owner the read is reported (detail::site::kOffOwner) and proceeds untracked: it subscribes nothing and leaves the tracking state alone. The value it returns is then read without synchronisation against the owner's writes.

Returns
The current value; valid until the next write.

Definition at line 76 of file signal.hpp.

◆ peek()

template<typename T>
T const & morph::reactive::Signal< T >::peek ( ) const
inlinenodiscardnoexcept

Reads the value without subscribing anything.

Returns
The current value; valid until the next write.

Definition at line 85 of file signal.hpp.

◆ set()

template<typename T>
void morph::reactive::Signal< T >::set ( T value)
inline

Replaces the value and notifies observers, unless it equals the current one and the policy is EqualityPolicy::Auto.

Parameters
valueThe new value.

Definition at line 90 of file signal.hpp.

◆ mutate()

template<typename T>
template<typename F>
requires std::invocable<F, T&>
void morph::reactive::Signal< T >::mutate ( F && fn)
inline

Changes the value in place and always notifies observers.

Template Parameters
FA callable taking T&.
Parameters
fnThe mutation.

Definition at line 108 of file signal.hpp.


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