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 for ( size_t i = 0; i < m_delegates.size(); i++ )
00112 m_delegates[i](arg);
00113 }
00114
00116 void add(void (*f)(ArgType))
00117 {
00118 inventor::helper::SbDelegate<void, ArgType> delegate = inventor::helper::SbDelegate<void, ArgType>(f);
00119 m_delegates.push_back(delegate);
00120 }
00121
00123 template <typename ClassType>
00124 void add(ClassType& a, void (ClassType::*f)(ArgType))
00125 {
00126 inventor::helper::SbDelegate<void, ArgType> delegate = inventor::helper::SbDelegate<void, ArgType>(a, f);
00127 m_delegates.push_back(delegate);
00128 }
00129
00135 bool remove(void (*f)(ArgType))
00136 {
00137 for ( typename DelegateVector::iterator itDelegate = m_delegates.begin(); itDelegate != m_delegates.end(); itDelegate++ )
00138 {
00139 if ( itDelegate->equal(f) )
00140 {
00141 m_delegates.erase(itDelegate);
00142 return true;
00143 }
00144 }
00145 return false;
00146 }
00147
00153 template <typename ClassType>
00154 bool remove(ClassType& a, void (ClassType::*f)(ArgType))
00155 {
00156 for ( typename DelegateVector::iterator itDelegate = m_delegates.begin(); itDelegate != m_delegates.end(); itDelegate++ )
00157 {
00158 if ( itDelegate->equal(a, f) )
00159 {
00160 m_delegates.erase(itDelegate);
00161 return true;
00162 }
00163 }
00164 return false;
00165 }
00166
00167 private:
00168
00169 typedef std::vector< inventor::helper::SbDelegate<void, ArgType> > DelegateVector;
00171 DelegateVector m_delegates;
00172 };
00173
00174 #endif // _SB_EVENTHANDLER_H_
00175