-
Notifications
You must be signed in to change notification settings - Fork 131
Observer.h
#include "react/Observer.h"
Contains the observer template classes and functions.
Observer functions can return values of this type to control further processing.
namespace react
{
enum class ObserverAction
{
next,
stop_and_detach
};
}
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.
namespace react
{
template <typename D>
class Observer
{
public:
// Constructor
Observer();
Observer(Observer&& other);
// Assignemnt
Observer& operator=(Observer&& other)
bool IsValid() const;
void Detach();
};
}
Takes ownership of an observer and automatically detaches it on scope exit.
template <typename D>
class ScopedObserver
{
public:
// Constructor
ScopedObserver(Observer<D>&& obs);
// Assignemnt
Observer& operator=(Observer&& other)
};
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);
}
// (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);
(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
.
(1)
(2)
(3)