20 #ifndef __OpenKarto_Event_h__ 21 #define __OpenKarto_Event_h__ 82 template <
class TArgs>
83 class AbstractDelegate
90 AbstractDelegate(
void* pTarget)
93 assert(m_pTarget != 0);
100 AbstractDelegate(
const AbstractDelegate& rOther)
101 : m_pTarget(rOther.m_pTarget)
103 assert(m_pTarget != 0);
109 virtual ~AbstractDelegate()
119 virtual void Notify(
const void* pSender, TArgs& rArguments) = 0;
125 virtual AbstractDelegate* Clone()
const = 0;
130 void* GetTarget()
const 143 return m_pTarget == rOther.m_pTarget;
151 kt_bool operator<(const AbstractDelegate<TArgs>& rOther)
const 153 return m_pTarget < rOther.m_pTarget;
167 template <
class TObj,
class TArgs,
bool withSender=true>
168 class Delegate:
public AbstractDelegate<TArgs>
171 typedef void (TObj::*NotifyMethod)(
const void*, TArgs&);
173 Delegate(TObj* pObject, NotifyMethod method)
174 : AbstractDelegate<TArgs>(pObject)
175 , m_ReceiverObject(pObject)
176 , m_ReceiverMethod(method)
183 Delegate(
const Delegate& rDelegate)
184 : AbstractDelegate<TArgs>(rDelegate)
185 , m_ReceiverObject(rDelegate.m_ReceiverObject)
186 , m_ReceiverMethod(rDelegate.m_ReceiverMethod)
198 void Notify(
const void* pSender, TArgs& rArguments)
200 (m_ReceiverObject->*m_ReceiverMethod)(pSender, rArguments);
203 AbstractDelegate<TArgs>* Clone()
const 205 return new Delegate(*
this);
214 Delegate& operator=(
const Delegate& rOther)
218 this->m_pTarget = rOther.m_pTarget;
219 this->m_ReceiverObject = rOther.m_ReceiverObject;
220 this->m_ReceiverMethod = rOther.m_ReceiverMethod;
226 TObj* m_ReceiverObject;
227 NotifyMethod m_ReceiverMethod;
237 template <
class TObj,
class TArgs>
238 class Delegate<TObj, TArgs, false>:
public AbstractDelegate<TArgs>
241 typedef void (TObj::*NotifyMethod)(TArgs&);
243 Delegate(TObj* pObject, NotifyMethod method)
244 : AbstractDelegate<TArgs>(pObject)
245 , m_ReceiverObject(pObject)
246 , m_ReceiverMethod(method)
253 Delegate(
const Delegate& rDelegate)
254 : AbstractDelegate<TArgs>(rDelegate)
255 , m_ReceiverObject(rDelegate.m_ReceiverObject)
256 , m_ReceiverMethod(rDelegate.m_ReceiverMethod)
268 void Notify(
const void*, TArgs& rArguments)
270 (m_ReceiverObject->*m_ReceiverMethod)(rArguments);
273 AbstractDelegate<TArgs>* Clone()
const 275 return new Delegate(*
this);
284 Delegate& operator=(
const Delegate& rOther)
288 this->m_pTarget = rOther.m_pTarget;
289 this->m_ReceiverObject = rOther.m_ReceiverObject;
290 this->m_ReceiverMethod = rOther.m_ReceiverMethod;
297 TObj* m_ReceiverObject;
298 NotifyMethod m_ReceiverMethod;
312 template <
class TArgs,
bool hasSender = true,
bool senderIsConst = true>
313 class FunctionDelegate:
public AbstractDelegate<TArgs>
319 typedef void (*NotifyMethod)(
const void*, TArgs&);
325 FunctionDelegate(NotifyMethod method)
326 : AbstractDelegate<TArgs>(*
reinterpret_cast<void**
>(&method))
327 , m_ReceiverMethod(method)
335 FunctionDelegate(
const FunctionDelegate& rOther)
336 : AbstractDelegate<TArgs>(rOther)
337 , m_ReceiverMethod(rOther.m_ReceiverMethod)
344 virtual ~FunctionDelegate()
349 void Notify(
const void* pSender, TArgs& rArguments)
351 (*m_ReceiverMethod)(pSender, rArguments);
354 AbstractDelegate<TArgs>* Clone()
const 356 return new FunctionDelegate(*
this);
365 FunctionDelegate& operator=(
const FunctionDelegate& rOther)
369 this->m_pTarget = rOther.m_pTarget;
370 this->m_ReceiverMethod = rOther.m_ReceiverMethod;
379 NotifyMethod m_ReceiverMethod;
393 template <
class TArgs>
394 class FunctionDelegate<TArgs, true, false>:
public AbstractDelegate<TArgs>
400 typedef void (*NotifyMethod)(
void*, TArgs&);
406 FunctionDelegate(NotifyMethod method)
407 : AbstractDelegate<TArgs>(*
reinterpret_cast<void**
>(&method))
408 , m_ReceiverMethod(method)
416 FunctionDelegate(
const FunctionDelegate& rOther)
417 : AbstractDelegate<TArgs>(rOther)
418 , m_ReceiverMethod(rOther.m_ReceiverMethod)
425 virtual ~FunctionDelegate()
430 void Notify(
const void* pSender, TArgs& rArguments)
432 (*m_ReceiverMethod)(
const_cast<void*
>(pSender), rArguments);
435 AbstractDelegate<TArgs>* Clone()
const 437 return new FunctionDelegate(*
this);
446 FunctionDelegate& operator=(
const FunctionDelegate& rOther)
450 this->m_pTarget = rOther.m_pTarget;
451 this->m_ReceiverMethod = rOther.m_ReceiverMethod;
460 NotifyMethod m_ReceiverMethod;
474 template <
class TArgs,
bool senderIsConst>
475 class FunctionDelegate<TArgs, false, senderIsConst>:
public AbstractDelegate<TArgs>
481 typedef void (*NotifyMethod)(TArgs&);
487 FunctionDelegate(NotifyMethod method)
488 : AbstractDelegate<TArgs>(*
reinterpret_cast<void**
>(&method))
489 , m_ReceiverMethod(method)
497 FunctionDelegate(
const FunctionDelegate& rOther)
498 : AbstractDelegate<TArgs>(rOther)
499 , m_ReceiverMethod(rOther.m_ReceiverMethod)
506 virtual ~FunctionDelegate()
511 void Notify(
const void* , TArgs& rArguments)
513 (*m_ReceiverMethod)(rArguments);
516 AbstractDelegate<TArgs>* Clone()
const 518 return new FunctionDelegate(*
this);
527 FunctionDelegate& operator=(
const FunctionDelegate& rOther)
531 this->m_pTarget = rOther.m_pTarget;
532 this->m_ReceiverMethod = rOther.m_ReceiverMethod;
541 NotifyMethod m_ReceiverMethod;
558 template <
class TArgs>
559 class NotificationStrategy
565 NotificationStrategy()
572 virtual ~NotificationStrategy()
580 virtual void Notify(
const void* sender, TArgs& arguments) = 0;
585 virtual void Add(
const AbstractDelegate<TArgs>& pDelegate) = 0;
590 virtual void Remove(
const AbstractDelegate<TArgs>& pDelegate) = 0;
595 virtual void Clear() = 0;
600 virtual bool IsEmpty()
const = 0;
612 template <
class TArgs>
613 class DefaultStrategy :
public NotificationStrategy<TArgs>
633 DefaultStrategy(
const DefaultStrategy& rOther)
641 virtual ~DefaultStrategy()
647 void Notify(
const void* pSender, TArgs& rArguments)
651 (*iter)->Notify(pSender, rArguments);
655 void Add(
const AbstractDelegate<TArgs>& rDelegate)
659 AbstractDelegate<TArgs>* pDelegate = rDelegate.Clone();
660 m_Observers.Add(pDelegate);
663 void Remove(
const AbstractDelegate<TArgs>& rDelegate)
669 if (*(*iter) == rDelegate)
681 m_Observers.RemoveAt(index);
696 return m_Observers.IsEmpty();
703 DefaultStrategy& operator=(
const DefaultStrategy& rOther)
719 DelegateList m_Observers;
805 template <
class TArgs>
823 , m_Strategy(rStrategy)
846 m_Strategy.Add(rDelegate);
858 m_Strategy.Remove(rDelegate);
866 Notify(pSender, args);
879 void Notify(
const void* pSender, TArgs& rArgs)
881 DefaultStrategy<TArgs>* pStrats = NULL;
892 pStrats =
new DefaultStrategy<TArgs>(m_Strategy);
898 pStrats->Notify(pSender, rArgs);
947 return m_Strategy.IsEmpty();
989 template <
class TArgs>
1037 #define EXPORT_KARTO_EVENT(declspec, T) \ 1038 template class declspec karto::BasicEvent<T>; 1047 template <
class TObj,
class TArgs>
1048 static Delegate<TObj, TArgs, true>
delegate(TObj* pObj,
void (TObj::*NotifyMethod)(
const void*, TArgs&))
1050 return Delegate<TObj, TArgs, true>(pObj, NotifyMethod);
1053 template <
class TObj,
class TArgs>
1054 static Delegate<TObj, TArgs, false>
delegate(TObj* pObj,
void (TObj::*NotifyMethod)(TArgs&))
1056 return Delegate<TObj, TArgs, false>(pObj, NotifyMethod);
1059 template <
class TArgs>
1060 static FunctionDelegate<TArgs, true, true>
delegate(
void (*NotifyMethod)(
const void*, TArgs&))
1062 return FunctionDelegate<TArgs, true, true>(NotifyMethod);
1065 template <
class TArgs>
1066 static FunctionDelegate<TArgs, true, false>
delegate(
void (*NotifyMethod)(
void*, TArgs&))
1068 return FunctionDelegate<TArgs, true, false>(NotifyMethod);
1071 template <
class TArgs>
1072 static FunctionDelegate<TArgs, false>
delegate(
void (*NotifyMethod)(TArgs&))
1074 return FunctionDelegate<TArgs, false>(NotifyMethod);
1081 #endif // __OpenKarto_Event_h__
void operator-=(const AbstractDelegate< TArgs > &rDelegate)
TFSIMD_FORCE_INLINE bool operator==(const Matrix3x3 &m1, const Matrix3x3 &m2)
static EventArguments & Empty()
DefaultStrategy< TArgs > m_Strategy
void operator+=(const AbstractDelegate< TArgs > &rDelegate)
static FunctionDelegate< TArgs, false > delegate(void(*NotifyMethod)(TArgs &))
void operator()(const void *pSender, TArgs &args)
#define karto_forEach(listtype, list)
void Notify(const void *pSender, TArgs &rArgs)
kt_bool IsEnabled() const
AbstractEvent(const DefaultStrategy< TArgs > &rStrategy)
#define karto_const_forEach(listtype, list)