Observables Usage
An interface can declare an observable object. This observable will notify observers (callback functions) when a certain event occurs.
This interface declares the observable "ObservablePing" that will notify observers when the function "Ping" is called:
// This example shows an interface declaring an observable.//---------------------------------------------------------------------------------------- // Signal fired when Ping() is called. //---------------------------------------------------------------------------------------- MAXON_OBSERVABLE ( void , ObservablePing, ( maxon::Int32 count), maxon::ObservableCombinerRunAllComponent); };
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. static void WatchPing( maxon::Int count) { DiagnosticOutput ( "Count: @" , count); }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().AddObserver(WatchPing) iferr_return ;It is also possible to add lambdas to the observable.
// This example adds a lambda to the "ObservablePing" observable. g_observeMe.ObservablePing().AddObserver( []( maxon::Int count) { DiagnosticOutput ( "Count: @" , count); }) iferr_return ;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();