00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef CEVENTQUEUE_H
00016 #define CEVENTQUEUE_H
00017
00018 #include "IEventQueue.h"
00019 #include "CEvent.h"
00020 #include "CPriorityQueue.h"
00021 #include "CStopwatch.h"
00022 #include "IArchMultithread.h"
00023 #include "stdmap.h"
00024 #include "stdset.h"
00025
00027
00031 class CEventQueue : public IEventQueue {
00032 public:
00033 CEventQueue();
00034 virtual ~CEventQueue();
00035
00036
00037 virtual void adoptBuffer(IEventQueueBuffer*);
00038 virtual bool getEvent(CEvent& event, double timeout = -1.0);
00039 virtual bool dispatchEvent(const CEvent& event);
00040 virtual void addEvent(const CEvent& event);
00041 virtual CEventQueueTimer*
00042 newTimer(double duration, void* target);
00043 virtual CEventQueueTimer*
00044 newOneShotTimer(double duration, void* target);
00045 virtual void deleteTimer(CEventQueueTimer*);
00046 virtual void adoptHandler(CEvent::Type type,
00047 void* target, IEventJob* handler);
00048 virtual void removeHandler(CEvent::Type type, void* target);
00049 virtual void removeHandlers(void* target);
00050 virtual CEvent::Type
00051 registerType(const char* name);
00052 virtual CEvent::Type
00053 registerTypeOnce(CEvent::Type& type, const char* name);
00054 virtual bool isEmpty() const;
00055 virtual IEventJob* getHandler(CEvent::Type type, void* target) const;
00056 virtual const char* getTypeName(CEvent::Type type);
00057
00058 private:
00059 UInt32 saveEvent(const CEvent& event);
00060 CEvent removeEvent(UInt32 eventID);
00061 bool hasTimerExpired(CEvent& event);
00062 double getNextTimerTimeout() const;
00063
00064 private:
00065 class CTimer {
00066 public:
00067 CTimer(CEventQueueTimer*, double timeout, double initialTime,
00068 void* target, bool oneShot);
00069 ~CTimer();
00070
00071 void reset();
00072
00073 CTimer& operator-=(double);
00074
00075 operator double() const;
00076
00077 bool isOneShot() const;
00078 CEventQueueTimer*
00079 getTimer() const;
00080 void* getTarget() const;
00081 void fillEvent(CTimerEvent&) const;
00082
00083 bool operator<(const CTimer&) const;
00084
00085 private:
00086 CEventQueueTimer* m_timer;
00087 double m_timeout;
00088 void* m_target;
00089 bool m_oneShot;
00090 double m_time;
00091 };
00092 typedef std::set<CEventQueueTimer*> CTimers;
00093 typedef CPriorityQueue<CTimer> CTimerQueue;
00094 typedef std::map<UInt32, CEvent> CEventTable;
00095 typedef std::vector<UInt32> CEventIDList;
00096 typedef std::map<CEvent::Type, const char*> CTypeMap;
00097 typedef std::map<CEvent::Type, IEventJob*> CTypeHandlerTable;
00098 typedef std::map<void*, CTypeHandlerTable> CHandlerTable;
00099
00100 CArchMutex m_mutex;
00101
00102
00103 CEvent::Type m_nextType;
00104 CTypeMap m_typeMap;
00105
00106
00107 IEventQueueBuffer* m_buffer;
00108
00109
00110 CEventTable m_events;
00111 CEventIDList m_oldEventIDs;
00112
00113
00114 CStopwatch m_time;
00115 CTimers m_timers;
00116 CTimerQueue m_timerQueue;
00117 CTimerEvent m_timerEvent;
00118
00119
00120 CHandlerTable m_handlers;
00121 };
00122
00123 #endif