template class EE::ComputedValue

Overview

A read-only observable whose value is synchronously derived from explicit dependencies. More…

#include <computedvalue.hpp>

template <typename T, typename Calculator, typename... Dependencies>
class ComputedValue {
public:
    // typedefs

    typedef T ValueType;
    typedef typename ObservableValue<T>::Callback Callback;
    typedef typename ObservableValue<T>::Connection Connection;

    // structs

    struct State;

    // construction

    ComputedValue(Calculator calculator, Dependencies&... dependencies);
    ComputedValue(const ComputedValue&);
    ComputedValue(ComputedValue&&);

    // methods

    ComputedValue& operator=(const ComputedValue&);
    ComputedValue& operator=(ComputedValue&&);
    const T& get() const;
    const T& operator*() const;
    const T* operator->() const;
    operator const T &() const;
    Connection observe(Callback callback);
    std::size_t observerCount() const;
    static constexpr std::size_t dependencyCount();
};

Detailed Documentation

A read-only observable whose value is synchronously derived from explicit dependencies.

Dependencies are observed in argument order. A dependency change updates its cached value and recomputes the result before the dependency’s set() returns. Destroying a dependency leaves the computed value at its last result; destroying either endpoint safely expires scoped observers. ComputedValue is single-threaded, like ObservableValue.

Construction

ComputedValue(Calculator calculator, Dependencies&... dependencies)

Creates a computed value and evaluates calculator once from the current dependencies.

The calculator receives the dependency values as const references in the same order in which the dependencies are passed. Every dependency must outlive this object if further updates are expected from it.

Methods

const T& get() const

Returns:

The most recently calculated value.

Connection observe(Callback callback)

Observes later changes to the calculated value.

The callback is not invoked immediately. Read get() when the initial value is needed.

Returns:

A scoped connection; destroying it disconnects the callback.

std::size_t observerCount() const

Returns:

The number of observers currently attached to the calculated output.

static constexpr std::size_t dependencyCount()

Returns:

The number of observable dependencies captured by this computed value.