FreePOOMA  2.4.1
Public Types | Public Member Functions
Observable< T > Class Template Reference

The Observable class, along with the Observer class, are used to implement the observer pattern. More...

#include <Observable.h>

Inheritance diagram for Observable< T >:
Inheritance graph
[legend]

List of all members.

Public Types

enum  { deleteEvent = 0 }

Public Member Functions

 Observable (T &o)
 The constructor for Observable initializing the reference to the object being observed, and sets up an empty list of Observers.
 ~Observable ()
 When destructed, an Observable informs all registered objects that it is going away.
int observers () const
 Return the number of observers we have registered.
void attach (Observer< T > *o)
 Allow an Observer to register with this Observable.
void attach (Observer< T > &o)
void detach (Observer< T > *o)
 Allow an Observer to indicate it no longer wants to be informed of events from this Observable.
void detach (Observer< T > &o)
void notify (int event)
 When called, notify calls the notify method in each attached Observer, passing on which observed object this is referring to and what the event code is.
void notify (const ObserverEvent &event)
 When called, notify calls the notify method in each attached Observer, passing on which observed object this is referring to and what the event code is.

Detailed Description

template<class T>
class Observable< T >

The Observable class, along with the Observer class, are used to implement the observer pattern.

In this pattern, there are two sets of objects:

  1. Observable<T> objects, which contain a list of Observer<T> pointers.
  2. Observer<T> objects, which check in as observers of any number of Observable objects.

When the Observer<T> is initialized, it should call the 'attach' method of all Observable<T> objects it needs to watch. When the Observable changes in some way, for example when it changes state is or is deleted, the Observable will call the 'notify' method of all the Obserers registered with it. An Observer<T> can stop watching an object, by calling the 'detach' method of that Observable.

Observable<T> is actually a base or wrapper class for the actual class that is to be observed. The template parameter T is the type of object that is being observed. This is the same template parameter for Observer. Observer<T> can attach as an observer of Observable<T> objects. When Observable<T> notifies its observers of events, it calls the method:

virtual void notify(T &observable, int event);

in each Observer<T>. An Observer<T> can attach to more than one Observable<T>, and can distinguish which one is notifying it by the first argument to notify. 'event' is an argument which can contain an integer code to tell the Observer what is happening. It is up to the class implementing this Observer interface to know how to interpret the event value, based on the type of object it is observing.

Observable contains a list of Observers, methods 'attach(Observer<T>)' and 'detach(Observer<T>)' to add and remove Observes, and a 'notify(int event)' method which will pass on the event to to the notify method of all attached Observer's. Observer can be used as a pass class for some object of type T, as in:

class A : public Observable { ...}

or as a wrapper class for some class A.

When an Observable is deleted, it notifies each registered Observer that it is being deleted by using the reserved event code '0'. When an Observer gets a notification of this, it should NOT try to call 'detach' for that Observable; it should just remember that that Observable is no longer available and assume that it has been 'detached' already.


Member Enumeration Documentation

template<class T>
anonymous enum
Enumerator:
deleteEvent 

Constructor & Destructor Documentation

template<class T>
Observable< T >::Observable ( T &  o) [inline]

The constructor for Observable initializing the reference to the object being observed, and sets up an empty list of Observers.

template<class T>
Observable< T >::~Observable ( ) [inline]

When destructed, an Observable informs all registered objects that it is going away.

It does this by calling notify with the special reserved event code '0'.


Member Function Documentation

template<class T>
int Observable< T >::observers ( ) const [inline]

Return the number of observers we have registered.

template<class T>
void Observable< T >::attach ( Observer< T > *  o) [inline]

Allow an Observer to register with this Observable.

This does not check for duplicates, so if the same object attaches twice, it will be notified twice.

Referenced by Observable< GridLayout< Dim > >::attach(), and DynamicLayout::DynamicLayout().

template<class T>
void Observable< T >::attach ( Observer< T > &  o) [inline]
template<class T>
void Observable< T >::detach ( Observer< T > *  o) [inline]

Allow an Observer to indicate it no longer wants to be informed of events from this Observable.

If the object is not currently registered, this results in an assertion failure.

Referenced by Observable< GridLayout< Dim > >::detach(), and DynamicLayout::~DynamicLayout().

template<class T>
void Observable< T >::detach ( Observer< T > &  o) [inline]
template<class T>
void Observable< T >::notify ( int  event) [inline]

When called, notify calls the notify method in each attached Observer, passing on which observed object this is referring to and what the event code is.

Referenced by Observable< GridLayout< Dim > >::notify(), UniformGridLayout< Dim2 >::notify(), SparseTileLayout< Dim2 >::notify(), GridLayout< Dim2 >::notify(), DynamicLayout::notify(), DynamicLayout::repartition(), and Observable< GridLayout< Dim > >::~Observable().

template<class T>
void Observable< T >::notify ( const ObserverEvent event) [inline]

When called, notify calls the notify method in each attached Observer, passing on which observed object this is referring to and what the event code is.


The documentation for this class was generated from the following file: