contrib/rpl/rgrl/rgrl_object.h

Go to the documentation of this file.
00001 #ifndef rgrl_object_h_
00002 #define rgrl_object_h_
00003 //:
00004 // \file
00005 // \brief Base class for most rgrl classes
00006 // \author Charlene Tsai
00007 // \date April 2004
00008 
00009 #include <vbl/vbl_ref_count.h>
00010 
00011 #include <vcl_map.h>
00012 
00013 #include "rgrl_command_sptr.h"
00014 #include "rgrl_event_sptr.h"
00015 #include "rgrl_macros.h"
00016 #include "rgrl_command.h"
00017 #include "rgrl_event.h"
00018 
00019 //: Observer class to bind a command with an event
00020 class rgrl_object_observer
00021 {
00022  public:
00023   rgrl_object_observer() {}
00024   rgrl_object_observer(rgrl_command_sptr c,
00025                        rgrl_event_sptr event )
00026     :command_(c),
00027      event_(event)
00028   {}
00029   ~rgrl_object_observer(){}
00030   rgrl_command_sptr command_;
00031   rgrl_event_sptr event_;
00032 };
00033 
00034 //: rgrl_object implements callbacks (via object/observer), and debug flags.
00035 //
00036 //  Most rgrl classes should be a subclas of rgrl_object.
00037 class rgrl_object
00038   : public vbl_ref_count
00039 {
00040  public:
00041   //:
00042   rgrl_object();
00043 
00044   //: copy constructor
00045   rgrl_object( const rgrl_object& that )
00046     : vbl_ref_count(), debug_flag_(that.debug_flag_), warning_(that.warning_),
00047       observers_(that.observers_), observer_count_(that.observer_count_)
00048   {    }   //suppress copying of reference count between objects
00049 
00050 
00051   //: assignment operator
00052   const rgrl_object& operator=( const rgrl_object& rhs )
00053   {
00054     //suppress copying of reference count between objects
00055     debug_flag_     = rhs.debug_flag_;
00056     warning_        = rhs.warning_;
00057     observers_      = rhs.observers_;
00058     observer_count_ = rhs.observer_count_;
00059     return *this;
00060   }
00061   //:
00062   virtual ~rgrl_object();
00063 
00064   static const vcl_type_info& type_id()
00065   { return typeid(rgrl_object); }
00066 
00067   virtual bool is_type( const vcl_type_info& type ) const
00068   { return (typeid(rgrl_object) == type)!=0; }
00069 
00070   //: Set the value of the debug flag. A non-zero value turns debugging on.
00071   void set_debug_flag( unsigned int debugFlag ) const;
00072 
00073   //: Get the value of the debug flag.
00074   unsigned int debug_flag() const;
00075 
00076   //: Set the flag for warning messages
00077   void set_warning(bool) const;
00078 
00079   //: Get the warning flag
00080   bool warning() const;
00081 
00082   //: Allow people to add/remove/invoke observers (callbacks) to any rgrl object.
00083   //
00084   // This is an implementation of the subject/observer design
00085   // pattern. An observer is added by specifying an event to respond
00086   // to and an rgrl_ommand to execute. It returns an unsigned long tag
00087   // which can be used later to remove the event or retrieve the
00088   // command.
00089   unsigned int add_observer( rgrl_event_sptr event, rgrl_command_sptr );
00090 
00091   //: Get the command associated with the given tag.
00092   rgrl_command_sptr get_command(unsigned int tag);
00093 
00094   //: Call \a execute(.) on all the rgrl_commands observing this event id.
00095   void invoke_event( const rgrl_event & );
00096 
00097   //: Call \a execute(.) on all the rgrl_commands observing this event id.
00098   //
00099   //  The actions triggered by this call doesn't modify this object.
00100   void invoke_event( const rgrl_event & ) const;
00101 
00102   //: Remove the observer with this tag value.
00103   void remove_observer(unsigned int tag);
00104 
00105   //: Return true if an observer is registered for this event.
00106   bool has_observer( const rgrl_event & event ) const;
00107 
00108  private:
00109 #if 0
00110   //: copy constructor and =operator are disabled on purpose
00111   rgrl_object( const rgrl_object& );
00112   void operator=( const rgrl_object& );
00113 #endif
00114 
00115   // For debugging
00116   mutable unsigned int debug_flag_;
00117   mutable bool warning_;
00118 
00119   // For event handling
00120   typedef vcl_map< unsigned, rgrl_object_observer > observer_map;
00121   observer_map observers_;
00122   unsigned int observer_count_;
00123 };
00124 
00125 #endif

Generated on Mon Mar 8 05:27:38 2010 for contrib/rpl/rgrl by  doxygen 1.5.1