Observables Implementation

About

An implementation of an interface that declares an observable must implement the behaviour of the observable. The observable must be initiated, freed and invoked.

Implementation

To implement basic observable functionality an implementation must include a maxon::ObserverObjectClass object with MAXON_COMPONENT().

The observable declared with MAXON_OBSERVABLE() is available as a member variable with the defined name and a "_" prefix.

// This example shows an implementation of an interface.
// It implements also an observable that is invoked when the function Ping() is called.
class ObserveMeImpl : public maxon::Component<ObserveMeImpl, ObserveMeInterface>
{
// use ObserverObjectClass to implement basic observer functionality
MAXON_COMPONENT(NORMAL, maxon::ObserverObjectClass);
public:
// implement observable
MAXON_OBSERVABLE_IMPL(ObservablePing);
maxon::Result<void> InitComponent()
{
_count = 0;
return maxon::OK;
}
void FreeComponent()
{
// free observable
_ObservablePing.Free();
}
MAXON_METHOD void Ping()
{
// increment
++_count;
// notify subscribers
iferr (_ObservablePing.Notify(_count))
{
DiagnosticOutput("Observable error: @", err);
}
}
private:
maxon::Int32 _count;
};
Definition: objectbase.h:2641
@ NORMAL
Normal Tag morphing.
int32_t Int32
32 bit signed integer datatype.
Definition: apibase.h:201
return OK
Definition: apibase.h:2747
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:176
#define iferr(...)
Definition: errorbase.h:388
#define MAXON_COMPONENT(KIND,...)
Definition: objectbase.h:2199
#define MAXON_METHOD
Definition: interfacebase.h:1012
#define MAXON_OBSERVABLE_IMPL(NAME,...)
Definition: observable.h:256
#define iferr_scope
Definition: resultbase.h:1386

The observable must be implemented using MAXON_OBSERVABLE_IMPL().

ObservableBaseInterface

Observables are based on maxon::ObservableBaseInterface:

Subscriber notification:

Observers are handled with:

Further Reading