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.
class ObserveMeInterface : MAXON_INTERFACE_BASES (maxon::ObserverObjectInterface) { MAXON_INTERFACE (ObserveMeInterface, MAXON_REFERENCE_NORMAL , "net.maxonexample.interfaces.observeme" );
public : //---------------------------------------------------------------------------------------- // Function makes "Ping". //---------------------------------------------------------------------------------------- MAXON_METHOD void Ping();

//---------------------------------------------------------------------------------------- // 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();

延伸阅读

MAXON_INTERFACE_BASES
#define MAXON_INTERFACE_BASES(...)
定义: objectbase.h:977
iferr_return
#define iferr_return
定义: resultbase.h:1434
MAXON_OBSERVABLE
#define MAXON_OBSERVABLE(RETTYPE, NAME,...)
Defined Observables for the source processor.
定义: observable.h:736
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
定义: debugdiagnostics.h:166
MAXON_METHOD
#define MAXON_METHOD
定义: interfacebase.h:855
maxon::Int32
int32_t Int32
32 bit signed integer datatype.
定义: apibase.h:172
maxon::Int
Int64 Int
signed 32/64 bit int, size depends on the platform
定义: apibase.h:184
MAXON_INTERFACE
#define MAXON_INTERFACE(Name, REFKIND, ID)
定义: objectbase.h:1048
MAXON_REFERENCE_NORMAL
#define MAXON_REFERENCE_NORMAL(DUMMY)
定义: interfacebase.h:957

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1