HomeReferenceSignal.h ⇀ Signal

Signal class

A signal is a reactive variable that can propagate its changes to dependents and react to changes of its dependencies.

Instances of this class act as a proxies to signal nodes. It takes shared ownership of the node, so while it exists, the node will not be destroyed. Copy, move and assignment semantics are similar to std::shared_ptr.

Signals are created by constructor functions, i.e. MakeSignal.

Synopsis

template
<
    typename D,
    typename S
>
class Signal
{
public:
    using DomainT = D;
    using ValueT = S;

    // Constructor
    Signal();
    Signal(const Signal&);  // Copy
    Signal(Signal&&);       // Move

    // Assignment
    Signal& operator=(const Signal&);   // Copy
    Signal& operator=(Signal&& other);  // Move

    // Tests if two Signal instances are equal
    bool Equals(const Signal& other) const;

    // Tests if this instance is linked to a node
    bool IsValid() const;

    // Sets weight override for linked node
    void SetWeightHint(WeightHint hint);

    // Returns the current signal value
    const S& Value() const;
    const S& operator()() const;

    // Equivalent to react::Flatten(*this)
    S Flatten() const;
};

Template parameters

D The domain this signal belongs to. Aliases as member type DomainT.
E Signal value type. Aliased as member type ValueT.
Should be DefaultConstructible, CopyConstructible and CopyAssignable.
Can be MoveConstructible and MoveAssignable to avoid copying when possible.

Constructor member function

Syntax

Signal();                    // (1)
Signal(const Signal& other); // (2)
Signal(Signal&& other);      // (3)

Semantics

(1) Creates an invalid signal that is not linked to a signal node.

(2) Creates a signal that links to the same signal node as other.

(3) Creates a signal that moves shared ownership of the signal node from other to this. As a result, other becomes invalid.

Note: The default constructor creates an invalid proxy, which is equivalent to std::shared_ptr(nullptr).


operator= member function

Syntax

Signal& operator=(const Signal&);   // (1)
Signal& operator=(Signal&& other);  // (2)

Semantics

(1) Links this to the same node as other. If this was already linked to another node, it releases its previous ownership.

(2) Transfers shared ownership of the linked node from other to this. If this was already linked to another node, it release its previous ownership. As a result, other becomes invalid.


Equals member function

Syntax

bool Equals(const Signal& other) const;

Semantics

Returns true, if both this and other link to the same signal node. This function is used to compare two signals, because == is used as a combination operator instead.


IsValid member function

Syntax

bool IsValid() const;

Semantics

Returns true, if this is linked to a signal node.


Value, operator() member function

Syntax

const S& Value() const;
const S& operator()() const;

Semantics

Returns a const reference to the current signal value.


Flatten member function

Syntax

S Flatten() const;

Semantics

Semantically equivalent to the respective free function in namespace react.