LibCT 2.0

Include/EventType.h

Go to the documentation of this file.
00001 // ------------------------------------------------------------------
00012 #ifndef _LIBCT_EVENTTYPE_H
00013 #define _LIBCT_EVENTTYPE_H
00014 
00015 #include "Functor.h"
00016 #include "TemplateFunctor.h"
00017 #include "EventData.h"
00018 #include "Defines.h"
00019 
00020 #include <set>
00021 #include <string>
00022 #include <assert.h>
00023 
00024 namespace LibCT
00025 {
00027         class EventOccurance;
00028         class EventListener;
00029         class EventType;
00030 
00032         typedef std::pair<EventListener*, Functor*>     ListenerPair;
00033         typedef std::set<ListenerPair>                          ListenerList;
00034         typedef std::pair<EventType*, ListenerPair>     ListenerHandle;
00035 
00037         class EventType
00038         {
00039         public:
00041                 EventType(
00042                         const std::string& name 
00043                         );
00044 
00046                 ~EventType();
00047 
00050                 template<typename T>
00051                 ListenerHandle RegisterListener(
00052                         EventListener* pEventListener,                  
00053                         void (T::*Callback)(EventOccurance*)    
00054                         )
00055                 {
00056                         // New the ListenerList if it's null, so EventTypes that have no listeners don't have the overhead of the std::set
00057                         if(!m_pListeners)
00058                         {
00059                                 m_pListeners = new ListenerList();
00060                         }
00061                         ListenerPair pair(pEventListener, new TemplateFunctor<T, EventOccurance>((T*)pEventListener, Callback));
00062                         m_pListeners->insert(pair);
00063                         return ListenerHandle(this, pair);
00064                 }
00065 
00067                 void UnregisterListener(
00068                         ListenerHandle handle                   
00069                         );
00070 
00072                 void Dispatch(
00073                         int priority,                   
00074                         EventData* pEventData   
00075                         );
00076 
00078                 void DispatchTo(
00079                         EventListener* pTarget, 
00080                         int priority,                   
00081                         EventData* pEventData   
00082                         );
00083 
00086                 const std::string& GetName() const;
00087 
00090                 unsigned int Ref();
00091 
00094                 unsigned int Deref();
00095 
00096         private:
00097                 ListenerList*   m_pListeners;   
00098                 std::string             m_Name;                 
00099                 unsigned int    m_Refs;                 
00100         };
00101 }
00102 
00103 #endif // _LIBCT_EVENTTYPE_H