observable


The os_observable class implements the Observer pattern. It represents an observable object which can have one or more observers. An observer can be any class object which can register a callback function with the observable object. When an observable object changes, an application calling notify_observers() method on the observable, will cause all of its observers to be notified of the change by a call to the callback function registered by the observers. The order in which the observers are notified is unspecified. The observer object does not have to inherit from any specific class in order to become an observer.

Use of member template feature in the add, remove, and notify observer methods gives the flexibility of not having the observer and observable classes to inherit from a specific interface class. Therefore, the compiler must be able to support member templates and OS_ENABLE_MEMBER_TEMPLATE_FEATURES must be defined to use these classes.

Library

Framework<ToolKit>

Declaration

#include <ospace/framework/observable.h>

template< class Observable >
class os_observable

Interface

Constructor
os_observable( Observable* observable )
Constructs an observable object. The observable object will be observed by the observers. If any changes are made to the observable object, then notify_observers() method should be invoked to inform the observers.
Destructor
~os_observable()
Removes all observers and frees internal resources.
add_observer
template< class Observer >
void add_observer( Observer* observer , void (Observer::*callback)( Observable* ) )
Adds observer to the list of observers. The callback function of the observer will be called when notify_observers() method is called. This observable object will be passed as the first argument to the callback function.
add_observer_arg
template< class Observer, class Arg >
void add_observer_arg( Observer* observer , void (Observer::*callback)( Observable*, Arg* ) )
Adds observer to the list of observers. The callback function of the observer will be called when notify_observers( Arg ) method is called. When the callback function is invoked, this observable object will be passed as its first argument and the second argument will be the first argument passed to the notify_observers( Arg ) method.
notify_observers
void notify_observers()
Notifies all observers by invoking their registered callback functions. This observable object will be passed as the first argument to the invoked callback function.
notify_observers
template< class Arg >
void notify_observers( Arg* arg )
Notifies all observers by invoking their registered callback functions. This observable object will be passed as the first argument and arg will be passed as the second argument to the invoked callback function.
remove_observer
template< class Observer >
void remove_observer( Observer* observer )
Removes all instances of observer object registered. If the same observer object is registered more than once with different callback functions, then all of them are removed. Currently, there is no method to remove only one of them.

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.