123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719 |
- #pragma once
- //
- // a delegate
- //
- class Delegate
- {
- public:
- inline Delegate () : m_handlers (NULL)
- {
- m_DelegateID [0] = 0;
- m_MoveFirst = NULL;
- m_MoveLast = NULL;
- m_ForEachBreak = false;
- }
- inline Delegate (const char * DelegateID) : m_handlers (NULL)
- {
- strncpy (m_DelegateID, DelegateID, sizeof(m_DelegateID));
- m_MoveFirst = NULL;
- m_MoveLast = NULL;
- m_ForEachBreak = false;
- }
- inline virtual ~Delegate ()
- {
- RemoveAll ();
- }
- inline virtual void AddDelegateHandler (DelegateHandler * handler)
- {
- try
- {
- DSingleLock Lock (& ((DMutex &)m_handlers), true);
- if (! IsExist (handler))
- {
- m_handlers.Add (handler);
- AdjustPosition ();
- }
- else
- {
- delete handler;
- }
- }
- catch (...)
- {
- }
- }
- inline Delegate & operator += (DelegateHandler * handler)
- {
- AddDelegateHandler (handler);
- return *this;
- }
- inline virtual void RemoveDelegateHandler (DelegateHandler * handler)
- {
- try
- {
- RemoveHandler (handler);
- delete handler;
- }
- catch (...)
- {
- }
- }
- inline Delegate & operator -= (DelegateHandler * handler)
- {
- RemoveDelegateHandler (handler);
- return *this;
- }
- inline void operator () (const void * sender, DelegateArgs * arg)
- {
- Invoke (sender, arg);
- }
- inline virtual void Invoke (const void * sender, DelegateArgs * arg)
- {
- int Size = GetSize ();
- if (Size == 0)
- return;
- DSingleLock Lock (& m_handlers.GetMutex (), true);
- if (Size <= DelegateMaxHandler)
- {
- DelegateHandler * arTemp [DelegateMaxHandler];
- const Array <DelegateHandler *> & ar = m_handlers;
- int NbOfElem = ar.CopyTo (arTemp, DelegateMaxHandler);
- for (int Index=0; Index<NbOfElem; Index++)
- {
- DelegateHandler * h = arTemp[Index];
- #ifndef _DEBUG
- try
- #endif
- {
- h->Invoke (sender, arg);
- }
- #ifndef _DEBUG
- catch (...)
- {
- Beep (1000, 1000);
- OnDelegateInvokeFailed (h);
- }
- #endif
- }
- }
- else
- {
- FixArray <DelegateHandler *> arTemp (m_handlers.GetSize ());
- const Array <DelegateHandler *> & ar = m_handlers;
- int NbOfElem = ar.CopyTo (arTemp);
- for (int Index=0; Index<NbOfElem; Index++)
- {
- DelegateHandler * h = arTemp[Index];
- #ifndef _DEBUG
- try
- #endif
- {
- h->Invoke (sender, arg);
- }
- #ifndef _DEBUG
- catch (...)
- {
- Beep (1000, 1000);
- OnDelegateInvokeFailed (h);
- }
- #endif
- }
- }
- }
- inline int GetSize () const
- {
- const Array <DelegateHandler *> * ar = & m_handlers;
- return ar->GetSize ();
- }
- inline bool IsEmpty () const
- {
- const Array <DelegateHandler *> * ar = & m_handlers;
- return ar->IsEmpty ();
- }
- inline bool IsExist (const DelegateHandler * handler) const
- {
- for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
- {
- DelegateHandler * curHandler = Iter ();
- if (curHandler->IsEqual (* handler))
- return true;
- }
- return false;
- }
- inline void RemoveAll ()
- {
- m_handlers.RemoveAll ();
- }
- inline virtual void AddFirst (DelegateHandler * handler)
- {
- try
- {
- DSingleLock Lock (& ((DMutex &)m_handlers), true);
- if (! IsExist (handler))
- m_handlers.InsertFirst (handler);
- else
- delete handler;
- }
- catch (...)
- {
- }
- }
- inline virtual void AddLast (DelegateHandler * handler)
- {
- try
- {
- DSingleLock Lock (& ((DMutex &)m_handlers), true);
- if (! IsExist (handler))
- m_handlers.InsertLast (handler);
- else
- delete handler;
- }
- catch (...)
- {
- }
- }
- template <class pfnForEach> void ForEach (pfnForEach pFunction)
- {
- int Size = GetSize ();
- if (Size == 0)
- return;
- DSingleLock Lock (& m_handlers.GetMutex (), true);
- m_ForEachBreak = false;
- if (Size <= DelegateMaxHandler)
- {
- DelegateHandler * arTemp [DelegateMaxHandler];
- // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
- const Array <DelegateHandler *> & ar = m_handlers;
- int NbOfElem = ar.CopyTo (arTemp, DelegateMaxHandler);
- for (int Index=0; Index<NbOfElem; Index++)
- {
- if (m_ForEachBreak)
- break;
- DelegateHandler * h = arTemp[Index];
- try
- {
- pFunction (h);
- }
- catch (...)
- {
- #ifdef _DEBUG
- Beep (1000, 1000);
- OnDelegateInvokeFailed (h);
- #endif
- }
- }
- }
- else
- {
- FixArray <DelegateHandler *> arTemp (m_handlers.GetSize ());
- // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
- const Array <DelegateHandler *> & ar = m_handlers;
- int NbOfElem = ar.CopyTo (arTemp);
- for (int Index=0; Index<NbOfElem; Index++)
- {
- if (m_ForEachBreak)
- break;
- DelegateHandler * h = arTemp[Index];
- try
- {
- pFunction (h);
- }
- catch (...)
- {
- #ifdef _DEBUG
- Beep (1000, 1000);
- OnDelegateInvokeFailed (h);
- #endif
- }
- }
- }
- }
- template <class pfnForEach, class Parameter> void ForEach (pfnForEach pFunction, Parameter para)
- {
- int Size = GetSize ();
- if (Size == 0)
- return;
- DSingleLock Lock (& m_handlers.GetMutex (), true);
- m_ForEachBreak = false;
- if (Size <= DelegateMaxHandler)
- {
- DelegateHandler * arTemp [DelegateMaxHandler];
- // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
- const Array <DelegateHandler *> & ar = m_handlers;
- int NbOfElem = ar.CopyTo (arTemp, DelegateMaxHandler);
- for (int Index=0; Index<NbOfElem; Index++)
- {
- if (m_ForEachBreak)
- break;
- DelegateHandler * h = arTemp[Index];
- try
- {
- pFunction (h, para);
- }
- catch (...)
- {
- #ifdef _DEBUG
- Beep (1000, 1000);
- OnDelegateInvokeFailed (h);
- #endif
- }
- }
- }
- else
- {
- FixArray <DelegateHandler *> arTemp (m_handlers.GetSize ());
- // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
- const Array <DelegateHandler *> & ar = m_handlers;
- int NbOfElem = ar.CopyTo (arTemp);
- for (int Index=0; Index<NbOfElem; Index++)
- {
- if (m_ForEachBreak)
- break;
- DelegateHandler * h = arTemp[Index];
- try
- {
- pFunction (h, para);
- }
- catch (...)
- {
- #ifdef _DEBUG
- Beep (1000, 1000);
- OnDelegateInvokeFailed (h);
- #endif
- }
- }
- }
- }
- template <class pfnForEach> void ForEach (pfnForEach pFunction, Delegate * SThis)
- {
- int Size = GetSize ();
- if (Size == 0)
- return;
- DSingleLock Lock (& m_handlers.GetMutex (), true);
- m_ForEachBreak = false;
- if (Size <= DelegateMaxHandler)
- {
- DelegateHandler * arTemp [DelegateMaxHandler];
- // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
- const Array <DelegateHandler *> & ar = m_handlers;
- int NbOfElem = ar.CopyTo (arTemp, DelegateMaxHandler);
- for (int Index=0; Index<NbOfElem; Index++)
- {
- if (m_ForEachBreak)
- break;
- DelegateHandler * h = arTemp[Index];
- try
- {
- pFunction (SThis, h);
- }
- catch (...)
- {
- #ifdef _DEBUG
- Beep (1000, 1000);
- OnDelegateInvokeFailed (h);
- #endif
- }
- }
- }
- else
- {
- FixArray <DelegateHandler *> arTemp (m_handlers.GetSize ());
- // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
- const Array <DelegateHandler *> & ar = m_handlers;
- int NbOfElem = ar.CopyTo (arTemp);
- for (int Index=0; Index<NbOfElem; Index++)
- {
- if (m_ForEachBreak)
- break;
- DelegateHandler * h = arTemp[Index];
- try
- {
- pFunction (SThis, h);
- }
- catch (...)
- {
- #ifdef _DEBUG
- Beep (1000, 1000);
- OnDelegateInvokeFailed (h);
- #endif
- }
- }
- }
- }
- template <class pfnForEach, class Parameter> void ForEach (pfnForEach pFunction, Delegate * SThis, Parameter para)
- {
- int Size = GetSize ();
- if (Size == 0)
- return;
- DSingleLock Lock (& m_handlers.GetMutex (), true);
- m_ForEachBreak = false;
- if (Size <= DelegateMaxHandler)
- {
- DelegateHandler * arTemp [DelegateMaxHandler];
- // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
- const Array <DelegateHandler *> & ar = m_handlers;
- int NbOfElem = ar.CopyTo (arTemp, DelegateMaxHandler);
- for (int Index=0; Index<NbOfElem; Index++)
- {
- if (m_ForEachBreak)
- break;
- DelegateHandler * h = arTemp[Index];
- try
- {
- pFunction (SThis, h, para);
- }
- catch (...)
- {
- #ifdef _DEBUG
- Beep (1000, 1000);
- OnDelegateInvokeFailed (h);
- #endif
- }
- }
- }
- else
- {
- FixArray <DelegateHandler *> arTemp (m_handlers.GetSize ());
- // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
- const Array <DelegateHandler *> & ar = m_handlers;
- int NbOfElem = ar.CopyTo (arTemp);
- for (int Index=0; Index<NbOfElem; Index++)
- {
- if (m_ForEachBreak)
- break;
- DelegateHandler * h = arTemp[Index];
- try
- {
- pFunction (SThis, h, para);
- }
- catch (...)
- {
- #ifdef _DEBUG
- Beep (1000, 1000);
- OnDelegateInvokeFailed (h);
- #endif
- }
- }
- }
- }
- inline int TransferTo (Delegate & toDelegate)
- {
- m_handlers.TransferTo (toDelegate.m_handlers);
- return toDelegate.m_handlers.GetSize ();
- }
- inline int TransferFrom (Delegate & fromDelegate)
- {
- return fromDelegate.TransferTo (*this);
- }
- inline int TransferTo (Array <DelegateHandler *> & toArray)
- {
- m_handlers.TransferTo (toArray);
- return toArray.GetSize ();
- }
- inline int TransferFrom (Array <DelegateHandler *> & fromArray)
- {
- fromArray.TransferTo (m_handlers);
- return m_handlers.GetSize ();
- }
- inline int CopyTo (Array <DelegateHandler *> & toArray) const
- {
- for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
- {
- DelegateHandler * h = Iter ();
- toArray.Add (h->Clone ());
- }
- return toArray.GetSize ();
- }
- inline int CopyTo (Delegate & toDelegate) const
- {
- for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
- {
- DelegateHandler * h = Iter ();
- toDelegate.AddDelegateHandler (h->Clone ());
- }
- return toDelegate.GetSize ();
- }
- inline int IndexOf (DelegateHandler * h) const
- {
- return m_handlers.IndexOf (h);
- }
- inline int IndexOf (const char * Name) const
- {
- if (! Name)
- return -1;
- if (Name[0] == 0)
- return -1;
- for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
- {
- DelegateHandler * h = Iter ();
- if (h->m_Name[0] == 0)
- continue;
- if (strcmp (h->m_Name, Name) == 0)
- return Iter.Index ();
- }
- return -1;
- }
- const DelegateHandler * Find (const char * Name) const
- {
- if (! Name)
- return NULL;
- if (Name[0] == 0)
- return NULL;
- for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
- {
- DelegateHandler * h = Iter ();
- if (h->m_Name[0] == 0)
- continue;
- if (strcmp (h->m_Name, Name) == 0)
- return h;
- }
- return NULL;
- }
- DelegateHandler * Find (const char * Name)
- {
- if (! Name)
- return NULL;
- if (Name[0] == 0)
- return NULL;
- for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
- {
- DelegateHandler * h = Iter ();
- if (h->m_Name[0] == 0)
- continue;
- if (strcmp (h->m_Name, Name) == 0)
- return h;
- }
- return NULL;
- }
- template <typename class T> DelegateHandler * Find (const T * This)
- {
- if (! This)
- return NULL;
- for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
- {
- try
- {
- ClassDelegateHandler <T> * h = (ClassDelegateHandler <T> * ) Iter ();
- if (h->GetClassInstance () == This)
- return h;
- }
- catch (...)
- {
- }
- }
- return NULL;
- }
- template <typename class T> const DelegateHandler * Find (const T * This) const
- {
- if (! This)
- return NULL;
- for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
- {
- try
- {
- ClassDelegateHandler <T> * h = (ClassDelegateHandler <T> * ) Iter ();
- if (h->GetClassInstance () == This)
- return h;
- }
- catch (...)
- {
- }
- }
- return NULL;
- }
- template <typename class T> int FindAll (const T * This, Array <DelegateHandler *> * pArray)
- {
- if (! This)
- return 0;
- if (! pArray)
- return 0;
- for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
- {
- try
- {
- ClassDelegateHandler <T> * h = (ClassDelegateHandler <T> * ) Iter ();
- if (h->GetClassInstance () == This)
- pArray->Add (h);
- }
- catch (...)
- {
- }
- }
- return pArray->GetSize ();
- }
- inline DString GetDelegateID (void) const
- {
- return m_DelegateID;
- }
- DString ToString (void) const;
- void MoveToFirst (const DelegateHandler * h)
- {
- m_MoveFirst = h;
- AdjustPosition ();
- }
- void MoveLast (const DelegateHandler * h)
- {
- m_MoveLast = h;
- AdjustPosition ();
- }
- template <typename class T> void MoveToFirst (const T * This)
- {
- const DelegateHandler * h = Find (This);
- if (h)
- {
- m_MoveFirst = h;
- AdjustPosition ();
- }
- }
- template <typename class T> void MoveToLast (const T * This)
- {
- const DelegateHandler * h = Find (This);
- if (h)
- {
- m_MoveLast = h;
- AdjustPosition ();
- }
- }
- void SetForEachBreak (bool b)
- {
- m_ForEachBreak = b;
- }
- protected:
- inline bool RemoveHandler (const DelegateHandler * handler)
- {
- for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
- {
- DelegateHandler * curHandler = Iter ();
- if (curHandler->IsEqual (* handler))
- {
- m_handlers.RemoveAt (Iter);
- return true;
- }
- }
- return false;
- }
- inline void AdjustPosition (void)
- {
- if (m_handlers.IsEmpty ())
- return;
- if (m_MoveFirst)
- {
- DelegateHandler * const nh = (DelegateHandler * const) (m_MoveFirst);
- int Index = m_handlers.IndexOf (nh);
- if (Index > 0)
- m_handlers.Swap (0, Index);
- }
- if (m_MoveLast)
- {
- DelegateHandler * const nh = (DelegateHandler * const) (m_MoveLast);
- int Index = m_handlers.IndexOf (nh);
- if (Index < m_handlers.GetSize ()-1)
- m_handlers.Swap (0, Index);
- }
- }
- protected:
- char m_DelegateID [64];
- ExclusiveArrayOfPtr <DelegateHandler *> m_handlers;
- const DelegateHandler * m_MoveFirst;
- const DelegateHandler * m_MoveLast;
- bool m_ForEachBreak;
- protected:
- static const int DelegateMaxHandler = 64;
- };
|