Observables Usage

Table of Contents

About

An interface can declare an observable object. This observable will notify observers (callback functions) when a certain event occurs.

Usage

This interface declares the observable "ObservablePing" that will notify observers when the function "Ping" is called:

// This example shows an interface declaring an observable.
namespace maxon
{
class ObserveMeInterface : MAXON_INTERFACE_BASES(maxon::ObserverObjectInterface)
{
MAXON_INTERFACE(ObserveMeInterface, MAXON_REFERENCE_NORMAL, "net.maxonexample.interfaces.observeme");
public:
//----------------------------------------------------------------------------------------
// Function makes "Ping".
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
// Signal fired when Ping() is called.
//----------------------------------------------------------------------------------------
MAXON_OBSERVABLE(Result<void>, ObservablePing, (maxon::Int32 count), maxon::ObservableCombinerRunAllComponent);
};
}
Py_ssize_t count
Definition: abstract.h:640
MAXON_INTERFACE(ObserveMeInterface, MAXON_REFERENCE_NORMAL, "net.maxonexample.interfaces.observeme")
MAXON_METHOD void Ping()
MAXON_OBSERVABLE(Result< void >, ObservablePing,(maxon::Int32 count), maxon::ObservableCombinerRunAllComponent)
int32_t Int32
32 bit signed integer datatype.
Definition: apibase.h:190
#define MAXON_REFERENCE_NORMAL(FREEIMPL)
Definition: interfacebase.h:1184
#define MAXON_METHOD
Definition: interfacebase.h:1012
#define MAXON_INTERFACE_BASES(...)
Definition: objectbase.h:1049
The maxon namespace contains all declarations of the MAXON API.
Definition: autoweight.h:14

Such a callback function must have the same return value and argument list as defined in the observable declaration:

// This example shows a function that can be used as an observer.
{
DiagnosticOutput("Count: @", count);
return maxon::OK;
}
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:202
return OK
Definition: apibase.h:2735
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:170

The observable is accessible by the function of its name. An observer is added with maxon::ObservableBaseInterface::AddObserver():

// This example adds an observer to the "ObservablePing" observable.
g_observeMe.ObservablePing(true).AddObserver(WatchPing) iferr_return;
#define iferr_return
Definition: resultbase.h:1524

It is also possible to add lambdas to the observable.

// This example adds a lambda to the "ObservablePing" observable.
g_observeMe.ObservablePing(true).AddObserver(
{
DiagnosticOutput("Count: @", count);
return maxon::OK;

The observer functions are called when the specific event is triggered.

// This example calls the Ping() function which will notify all registered observers.
g_observeMe.Ping();

Further Reading