ReFRACtor
FullPhysics::Observable< T > Class Template Referenceabstract

Mixin for a class that allows other classes to observe it state. More...

#include <observer.h>

+ Inheritance diagram for FullPhysics::Observable< T >:
+ Collaboration diagram for FullPhysics::Observable< T >:

Classes

class  PointerDead
 
class  PointerEqual
 

Public Member Functions

virtual ~Observable ()
 
virtual void add_observer (Observer< T > &Obs)=0
 Add an observer. More...
 
void add_observer_and_keep_reference (boost::shared_ptr< Observer< T > > &Obs)
 Add an observer and keep a reference to it. More...
 
virtual void remove_observer (Observer< T > &Obs)=0
 Remove an observer. More...
 

Protected Member Functions

void add_observer_do (Observer< T > &Obs, T &t)
 Add an observer. More...
 
void add_observer_do (Observer< T > &Obs)
 
void clean_dead_ptr ()
 Remove any dead pointers. More...
 
void notify_update_do (const T &Self)
 Function to call to notify Observers of a state change. More...
 
void remove_observer_do (Observer< T > &Obs, T &t)
 Remove an observer. More...
 
void remove_observer_do (Observer< T > &Obs)
 

Protected Attributes

std::list< boost::weak_ptr< Observer< T > > > olist
 
std::vector< boost::shared_ptr< Observer< T > > > ref_list
 

Detailed Description

template<class T>
class FullPhysics::Observable< T >

Mixin for a class that allows other classes to observe it state.

When the object state has been updated, the class should call notify_update.

A note on lifetime of objects. When an Observer is destroyed, nothing special needs to be done. The Observable is automatically notified that the Object no longer exists, and shouldn't receive notify_update messages anymore. Likewise, nothing special happens when the Observable is destroyed, this Observe simple receives no more messages from that Observable since it never changes after it is dead.

In some cases, you might actually want the lifetime of the Observer to be controlled by the Observable. This would be for a class that has no other purpose than to observe another class, say something that writes out diagnostic messages whenever a StateVector is updated or something like that. In those cases, you can use the add_observer_and_keep_reference instead of just add_observer. This stashes a copy of the RefPtr so that as long as the Observable exists the registered Observer will also.

The relationship between Observer and Observable is m to n, a Observer can be attached to any number of Observables, and likewise an Observable can have any number of Observers attached.

Definition at line 12 of file observer.h.

Constructor & Destructor Documentation

◆ ~Observable()

template<class T>
virtual FullPhysics::Observable< T >::~Observable ( )
inlinevirtual

Definition at line 93 of file observer.h.

Member Function Documentation

◆ add_observer()

◆ add_observer_and_keep_reference()

template<class T>
void FullPhysics::Observable< T >::add_observer_and_keep_reference ( boost::shared_ptr< Observer< T > > &  Obs)
inline

Add an observer and keep a reference to it.

See the discussion in the Observer class description for details.

Definition at line 107 of file observer.h.

◆ add_observer_do() [1/2]

template<class T>
void FullPhysics::Observable< T >::add_observer_do ( Observer< T > &  Obs,
T &  t 
)
inlineprotected

Add an observer.

Definition at line 148 of file observer.h.

◆ add_observer_do() [2/2]

template<class T>
void FullPhysics::Observable< T >::add_observer_do ( Observer< T > &  Obs)
inlineprotected

Definition at line 159 of file observer.h.

◆ clean_dead_ptr()

template<class T>
void FullPhysics::Observable< T >::clean_dead_ptr ( )
inlineprotected

Remove any dead pointers.

Definition at line 196 of file observer.h.

◆ notify_update_do()

template<class T>
void FullPhysics::Observable< T >::notify_update_do ( const T &  Self)
inlineprotected

Function to call to notify Observers of a state change.

The object should pass itself to this function, so it can be passed to the Observers.

Definition at line 121 of file observer.h.

◆ remove_observer()

◆ remove_observer_do() [1/2]

template<class T>
void FullPhysics::Observable< T >::remove_observer_do ( Observer< T > &  Obs,
T &  t 
)
inlineprotected

Remove an observer.

Definition at line 173 of file observer.h.

◆ remove_observer_do() [2/2]

template<class T>
void FullPhysics::Observable< T >::remove_observer_do ( Observer< T > &  Obs)
inlineprotected

Definition at line 181 of file observer.h.

Member Data Documentation

◆ olist

template<class T>
std::list<boost::weak_ptr<Observer<T> > > FullPhysics::Observable< T >::olist
protected

Definition at line 200 of file observer.h.

◆ ref_list

template<class T>
std::vector<boost::shared_ptr<Observer<T> > > FullPhysics::Observable< T >::ref_list
protected

Definition at line 201 of file observer.h.


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

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:13