core/vgui/vgui_observable.cxx
Go to the documentation of this file.
00001 // This is core/vgui/vgui_observable.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author fsm
00008 // \brief  See vgui_observable.h for a description of this file.
00009 
00010 #include "vgui_observable.h"
00011 #include <vgui/vgui_observer.h>
00012 #include <vcl_algorithm.h>
00013 
00014 //
00015 // Base class for classes which want to broadcast messages to vgui_observers
00016 // using notify(m).
00017 //
00018 
00019 //------------------------------------------------------------------------------
00020 
00021 vgui_observable::~vgui_observable()
00022 {
00023 }
00024 
00025 //------------------------------------------------------------------------------
00026 
00027 void vgui_observable::attach(vgui_observer *o)
00028 {
00029   observers.push_back(o);
00030 }
00031 
00032 void vgui_observable::detach(vgui_observer *o)
00033 {
00034   vcl_vector<vgui_observer*>::iterator o_iter = vcl_find(observers.begin(),
00035                                                          observers.end(),
00036                                                          o);
00037 
00038   if (o_iter != observers.end())
00039     observers.erase(o_iter);
00040 }
00041 
00042 void vgui_observable::get_observers(vcl_vector<vgui_observer*> &o_list) const
00043 {
00044   for  (vcl_vector<vgui_observer*>::const_iterator o_iter = observers.begin();
00045         o_iter != observers.end(); ++o_iter)
00046     o_list.push_back(*o_iter);
00047 }
00048 
00049 void vgui_observable::notify() const
00050 {
00051   // send an update() to each observer :
00052   for (vcl_vector<vgui_observer*>::const_iterator oi = observers.begin();
00053        oi != observers.end(); ++oi)
00054     (*oi) -> update(this);
00055 }
00056 
00057 void vgui_observable::notify(const vgui_message &m) const
00058 {
00059   // send an update(m) message to each observer :
00060   for (vcl_vector<vgui_observer*>::const_iterator oi = observers.begin();
00061        oi != observers.end(); ++oi)
00062     (*oi) -> update(m);
00063 }
00064 
00065 //------------------------------------------------------------------------------