contrib/rpl/rgrl/rgrl_object.cxx
Go to the documentation of this file.
00001 #include "rgrl_object.h"
00002 #include "rgrl_command.h"
00003 #include "rgrl_event.h"
00004 
00005 rgrl_object::
00006 rgrl_object()
00007   : debug_flag_( 0 ),
00008     warning_( true ),
00009     observer_count_( 0 )
00010 {}
00011 
00012 rgrl_object::
00013 ~rgrl_object()
00014 {
00015 }
00016 
00017 void
00018 rgrl_object::
00019 set_debug_flag(  unsigned int debugFlag ) const
00020 {
00021   debug_flag_ = debugFlag;
00022 }
00023 
00024 unsigned int
00025 rgrl_object::
00026 debug_flag() const
00027 {
00028   return debug_flag_;
00029 }
00030 
00031 void
00032 rgrl_object::
00033 set_warning( bool flag ) const
00034 {
00035   warning_ = flag;
00036 }
00037 
00038 bool
00039 rgrl_object::
00040 warning() const
00041 {
00042   return warning_;
00043 }
00044 
00045 unsigned int
00046 rgrl_object::
00047 add_observer( rgrl_event_sptr event, rgrl_command_sptr cmd )
00048 {
00049   rgrl_object_observer new_observer( cmd, event );
00050   observers_[observer_count_] = new_observer;
00051   observer_count_++;
00052   return observer_count_ - 1;
00053 }
00054 
00055 rgrl_command_sptr
00056 rgrl_object::
00057 get_command(unsigned int tag)
00058 {
00059   observer_map::iterator i =  observers_.find( tag );
00060 
00061   if ( i == observers_.end() )
00062     return 0;
00063 
00064   return i->second.command_;
00065 }
00066 
00067 void
00068 rgrl_object::
00069 invoke_event( const rgrl_event & event)
00070 {
00071   typedef observer_map::iterator obs_itr;
00072   obs_itr i = observers_.begin();
00073   for ( ; i != observers_.end(); ++i) {
00074     rgrl_event_sptr e =  i->second.event_;
00075     if (e->is_same_type(event)) {
00076       i->second.command_->execute(this, event);
00077     }
00078   }
00079 }
00080 
00081 void
00082 rgrl_object::
00083 invoke_event( const rgrl_event & event) const
00084 {
00085   typedef observer_map::const_iterator const_obs_itr;
00086   const_obs_itr i = observers_.begin();
00087   for ( ; i != observers_.end(); ++i) {
00088     const rgrl_event_sptr e =  i->second.event_;
00089     if (e->is_same_type(event)) {
00090       i->second.command_->execute(this, event);
00091     }
00092   }
00093 }
00094 
00095 void
00096 rgrl_object::
00097 remove_observer(unsigned int tag)
00098 {
00099   observer_map::iterator i =  observers_.find( tag );
00100 
00101   if ( i == observers_.end() ) return;
00102   observers_.erase(tag);
00103 }
00104 
00105 bool
00106 rgrl_object::
00107 has_observer( const rgrl_event & event ) const
00108 {
00109   typedef observer_map::const_iterator const_obs_itr;
00110   const_obs_itr i = observers_.begin();
00111   for ( ; i != observers_.end(); ++i) {
00112     const rgrl_event_sptr e = i->second.event_;
00113     if ( e->is_same_type(event) ) {
00114       return true;
00115     }
00116   }
00117   return false;
00118 }