LibCT 2.0

Include/EventListener.h

Go to the documentation of this file.
00001 // ------------------------------------------------------------------
00012 #ifndef _LIBCT_EVENTLISTENER_H
00013 #define _LIBCT_EVENTLISTENER_H
00014 
00015 #include "EventType.h"
00016 #include "Defines.h"
00017 #include "EventManager.h"
00018 
00019 #include <vector>
00020 #include <list>
00021 #include <string>
00022 
00023 namespace LibCT
00024 {
00026         class EventOccurance;
00027         class Project;
00028 
00030         typedef std::list<EventOccurance*>      EventOccuranceList;
00031         typedef std::list<ListenerHandle>       ListenerHandleList;
00032 
00034         enum EventListenerMode
00035         {
00036                 EventListenerModeBuffered,      
00037                 EventListenerModeImmediate      
00038         };
00039 
00041 
00046         class EventListener
00047         {
00048         public:
00050                 EventListener();
00051 
00053                 virtual ~EventListener();
00054 
00056                 template<typename T>
00057                 void ListenFor(
00058                         const std::string& name,                                
00059                         void (T::*Callback)(EventOccurance*)    
00060                         )
00061                 {
00062                         EventType* pEventType = EventManager::GetInstance()->GetEvent(name);
00063                         if(!pEventType)
00064                         {
00065                                 // Need to register the event
00066                                 LibCT::EventManager::GetInstance()->RegisterEvent(name);
00067                                 pEventType = LibCT::EventManager::GetInstance()->GetEvent(name);
00068                         }
00069                         if(pEventType && !IsListeningFor(name))
00070                         {
00071                                 m_ListenerHandles.push_back(pEventType->RegisterListener<T>((T*)this, Callback));
00072                         }
00073                 }
00074                 
00076                 void StopListeningFor(
00077                         const std::string& name         
00078                         );
00079 
00082                 bool IsListeningFor(
00083                         const std::string& name         
00084                         );
00085 
00088                 EventListenerMode GetMode() const;
00089 
00091                 void SetMode(
00092                         EventListenerMode mode          
00093                         );
00094 
00095         private:
00096                 EventOccuranceList      m_EventOccurances;      
00097                 ListenerHandleList      m_ListenerHandles;      
00098                 EventListenerMode       m_Mode;                         
00099 
00101                 void AddEventOccurance(
00102                         EventOccurance* pEventOccurance         
00103                         );
00104 
00106                 void PrioiritiseEventOccurances();
00107 
00109                 void ClearEventOccurances();
00110 
00111                 friend class EventType;
00112         };
00113 };
00114 
00115 #endif // _LIBCT_EVENTLISTENER_H