00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef _SB_EVENTHANDLER_H_
00025 #define _SB_EVENTHANDLER_H_
00026
00027 #include <Inventor/SbDelegate.h>
00028
00099 template <typename ArgType>
00100 class SbEventHandler
00101 {
00102 public:
00103
00109 void operator()(ArgType arg)
00110 {
00111
00112 DelegateVector delegateCopy = m_delegates;
00113 typename DelegateVector::const_iterator it = delegateCopy.begin();
00114 typename DelegateVector::const_iterator end = delegateCopy.end();
00115 while ( it != end )
00116 {
00117 (*it)(arg);
00118 ++it;
00119 }
00120 }
00121
00123 void add(void (*f)(ArgType))
00124 {
00125 inventor::helper::SbDelegate<void, ArgType> delegate = inventor::helper::SbDelegate<void, ArgType>(f);
00126 m_delegates.push_back(delegate);
00127 }
00128
00130 template <typename ClassType>
00131 void add(ClassType& a, void (ClassType::*f)(ArgType))
00132 {
00133 inventor::helper::SbDelegate<void, ArgType> delegate = inventor::helper::SbDelegate<void, ArgType>(a, f);
00134 m_delegates.push_back(delegate);
00135 }
00136
00142 bool remove(void (*f)(ArgType))
00143 {
00144 for ( typename DelegateVector::iterator itDelegate = m_delegates.begin(); itDelegate != m_delegates.end(); itDelegate++ )
00145 {
00146 if ( itDelegate->equal(f) )
00147 {
00148 m_delegates.erase(itDelegate);
00149 return true;
00150 }
00151 }
00152 return false;
00153 }
00154
00160 template <typename ClassType>
00161 bool remove(ClassType& a, void (ClassType::*f)(ArgType))
00162 {
00163 for ( typename DelegateVector::iterator itDelegate = m_delegates.begin(); itDelegate != m_delegates.end(); itDelegate++ )
00164 {
00165 if ( itDelegate->equal(a, f) )
00166 {
00167 m_delegates.erase(itDelegate);
00168 return true;
00169 }
00170 }
00171 return false;
00172 }
00173
00174 private:
00175
00176 typedef std::vector< inventor::helper::SbDelegate<void, ArgType> > DelegateVector;
00178 DelegateVector m_delegates;
00179 };
00180
00181 #endif // _SB_EVENTHANDLER_H_
00182