Skip to content
Sebastian Jeckel edited this page Jun 19, 2014 · 9 revisions

Header

#include "react/Observer.h"

Summary

Contains the observer template classes and functions.

Constants

ObserverAction

Description

Observer functions can return values of this type to control further processing.

Definition
namespace react
{
    enum class ObserverAction
    {
        next,
        stop_and_detach
    };
}

Classes

Observer

Description

An instance of this class provides a unique handle to an observer which can be used to detach it explicitly. It also holds a strong reference to the observed subject, so while it exists the subject and therefore the observer will not be destroyed.

If the handle is destroyed without calling Detach(), the lifetime of the observer is tied to the subject.

Synopsis
namespace react
{
    template <typename D>
    class Observer
    {
    public:
        // Constructor
        Observer();
        Observer(Observer&& other);

        // Assignemnt
        Observer& operator=(Observer&& other)

        bool IsValid() const;

        void Detach();
    };
}

ScopedObserver

Description

Takes ownership of an observer and automatically detaches it on scope exit.

Synopsis
template <typename D>
class ScopedObserver
{
public:
    // Constructor
    ScopedObserver(Observer<D>&& obs);

    // Assignemnt
    Observer& operator=(Observer&& other)
};

Functions

Synopsis
namespace react
{
    // Creates an observer of the given subject
    Observer<D> Observe(const Signal<D,S>& subject, F&& func);
    Observer<D> Observe(const Events<D,E>& subject, F&& func);
    Observer<D> Observe(const Events<D,E>& subject,
                        const SignalPack<D,TDepValues...>& depPack, F&& func);
}

Observe

Syntax
// (1)
template
<
    typename D,
    typename S,
    typename F
>
Observer<D> Observe(const Signal<D,S>& subject, F&& func);

// (2)
template
<
    typename D,
    typename E,
    typename F
>
Observer<D> Observe(const Events<D,E>& subject, F&& func);

// (3)
template
<
    typename D,
    typename F,
    typename E,
    typename ... TDepValues
>
Observer<D> Observe(const Events<D,E>& subject,
                    const SignalPack<D,TDepValues...>& depPack, F&& func);
Semantics

(1) When the signal value s of subject changes, func(s) is called.

(2) For every event e in subject, func(e) is called.

(3) Similar to (2), but the synchronized values of signals in depPack are passed to func as additional arguments. Changes of signals in depPack do not trigger an update - only received events do.

The signature of func should be equivalent to:

  • (1) TRet func(const S&)
  • (2) TRet func(const E&)
  • (3) TRet func(const E&, const TDepValues& ...)

TRet can be either ObserverAction or void.

By returning ObserverAction::stop_and_detach, the observer function can request its own detachment. Returning ObserverAction::next keeps the observer attached. Using a void return type is the same as always returning ObserverAction::next.

Graph

(1)
Drawing

(2)
Drawing

(3)
Drawing