1 #ifndef __EVENTHANDLER_H_105D52D8_13EC_4ea3_A86B_076B726541EB_
2 #define __EVENTHANDLER_H_105D52D8_13EC_4ea3_A86B_076B726541EB_
132 template<
typename TEventSource = const CEventSource,
typename TEventHandlerArgs = CEventHandlerArgs>
138 template<
typename TEventReceiver>
141 typedef void (TEventReceiver::*FuncType)(TEventSource* pSender, TEventHandlerArgs* pArgs);
145 CClassFunctor(FuncType pFunc) : m_pFunc(pFunc) { }
149 (((TEventReceiver*)pReceiver)->*m_pFunc)((TEventSource*)pSender, (TEventHandlerArgs*)pArgs);
156 typedef std::shared_ptr<IEventFunctor> IEventFunctorPtr;
158 typedef std::map<CEventReceiver*, IEventFunctorPtr> CLASS_HANDLER_MAP;
159 typedef void (*STATICHANDLER)(TEventSource*, TEventHandlerArgs*);
161 typedef std::vector<STATICHANDLER> STATIC_HANDLER_LIST;
164 CLASS_HANDLER_MAP m_ClassSubscribers;
165 STATIC_HANDLER_LIST m_StaticSubscribers;
170 void Invoke(TEventSource* pSender, TEventHandlerArgs* pArgs)
173 typedef typename CLASS_HANDLER_MAP::value_type ClassSubIterT;
174 std::for_each(m_ClassSubscribers.begin(), m_ClassSubscribers.end(), [&](ClassSubIterT& iter)
180 typedef typename STATIC_HANDLER_LIST::value_type StaticSubIterT;
181 std::for_each(m_StaticSubscribers.begin(), m_StaticSubscribers.end(), [&](StaticSubIterT& iter)
183 (*iter)(pSender, pArgs);
195 template<
typename TReceiverClass>
196 inline void Subscribe(TReceiverClass* pReceiver, IEventFunctorPtr spFunctor)
198 assert(pReceiver != NULL && spFunctor.get() != NULL);
200 m_ClassSubscribers[pReceiver] = spFunctor;
223 template<
typename TEventReceiver>
224 inline void Subscribe(TEventReceiver* pReceiver,
void (TEventReceiver::*lpfnHandler)(TEventSource*, TEventHandlerArgs* ))
226 assert(pReceiver != NULL);
228 Subscribe(pReceiver, IEventFunctorPtr(
new CClassFunctor<TEventReceiver>(lpfnHandler)));
236 inline void Subscribe(
void (*lpfnHandler)(TEventSource*, TEventHandlerArgs*))
238 assert(NULL != lpfnHandler);
240 if(m_StaticSubscribers.end() == std::find(m_StaticSubscribers.cbegin(), m_StaticSubscribers.cend(), lpfnHandler))
241 m_StaticSubscribers.push_back( lpfnHandler );
248 inline void UnSubscribe(
void (*lpfnHandler)(TEventSource*, TEventHandlerArgs*))
250 typename STATIC_HANDLER_LIST::const_iterator iter = std::find(m_StaticSubscribers.cbegin(), m_StaticSubscribers.cend(), lpfnHandler);
252 if(iter != m_StaticSubscribers.end())
253 m_StaticSubscribers.erase(iter);
260 template<
typename TReceiverClass>
263 m_ClassSubscribers.erase(pReceiver);
272 m_StaticSubscribers.clear();
274 m_ClassSubscribers.clear();
283 return m_ClassSubscribers.size() + m_StaticSubscribers.size();
292 typedef CEventT<> CEvent;
319 template<
typename TEventSource,
typename TEventHandlerArgs>
322 assert(pEvent != NULL);
324 pEvent->Invoke((TEventSource *)
this, (TEventHandlerArgs *)pArgs);
330 template<
typename TEventSource,
typename TEventHandlerArgs>
333 assert(pEvent != NULL);
335 pEvent->Invoke((TEventSource *)
this, (
const TEventHandlerArgs *)pArgs);
403 template<
typename TEventHandlerArgs = CEventHandlerArgs>
407 inline void RaiseEvent(TEventHandlerArgs* pArgs)
422 #endif // __EVENTHANDLER_H_105D52D8_13EC_4ea3_A86B_076B726541EB_
void UnSubscribe(void(*lpfnHandler)(TEventSource *, TEventHandlerArgs *))
void RaiseEvent(CEventT< TEventSource, const TEventHandlerArgs > *pEvent, const TEventHandlerArgs *pArgs)
void Subscribe(void(*lpfnHandler)(TEventSource *, TEventHandlerArgs *))
size_t SubscriberCount() const
void Subscribe(TEventReceiver *pReceiver, void(TEventReceiver::*lpfnHandler)(TEventSource *, TEventHandlerArgs *))
void UnSubscribe(TReceiverClass *pReceiver)
void RaiseEvent(CEventT< TEventSource, TEventHandlerArgs > *pEvent, TEventHandlerArgs *pArgs)