00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if !defined(_XRB_EVENTQUEUE_HPP_)
00012 #define _XRB_EVENTQUEUE_HPP_
00013
00014 #include "xrb.hpp"
00015
00016 #include <set>
00017
00018 #include "xrb_event.hpp"
00019 #include "xrb_framehandler.hpp"
00020
00021 namespace Xrb
00022 {
00023
00024 class EventHandler;
00025
00039 class EventQueue : public FrameHandler
00040 {
00041 public:
00042
00045 EventQueue ();
00049 virtual ~EventQueue ();
00050
00059 void EnqueueEvent (
00060 EventHandler *event_handler,
00061 Event const *event);
00066 void DeleteEventsBelongingToHandler (
00067 EventHandler *event_handler);
00073 void ScheduleMatchingEventsForDeletion (bool (*EventMatchingFunction)(Event const *));
00084 template <typename ParameterType>
00085 void ScheduleMatchingEventsForDeletion (
00086 bool (*EventMatchingFunction)(Event const *, ParameterType),
00087 ParameterType parameter);
00101 template <typename Parameter1Type, typename Parameter2Type>
00102 void ScheduleMatchingEventsForDeletion (
00103 bool (*EventMatchingFunction)(Event const *, Parameter1Type, Parameter2Type),
00104 Parameter1Type parameter1,
00105 Parameter2Type parameter2);
00106
00107 protected:
00108
00112 virtual void HandleFrame ();
00113
00114 private:
00115
00116
00117 static inline Uint32 MaxEventID ()
00118 {
00119 return UINT32_UPPER_BOUND;
00120 }
00121
00122
00123 void EnqueueBufferedEvents ();
00124
00125 void CompactEventIDs ();
00126
00127
00128 class EventBinding
00129 {
00130 private:
00131
00132 EventHandler *m_event_handler;
00133 Event const *m_event;
00134
00135 public:
00136
00137 EventBinding (
00138 EventHandler *const event_handler,
00139 Event const *const event)
00140 {
00141 m_event_handler = event_handler;
00142 m_event = event;
00143 }
00144
00145 inline EventHandler *GetEventHandler () const
00146 {
00147 return m_event_handler;
00148 }
00149 inline Event const *GetEvent () const
00150 {
00151 return m_event;
00152 }
00153 };
00154
00155
00156
00157 struct OrderEventBindingsByEventTime
00158 {
00159 bool operator () (
00160 EventBinding const &left_operand,
00161 EventBinding const &right_operand) const;
00162 };
00163
00164 typedef std::multiset<EventBinding, OrderEventBindingsByEventTime> TimeOrderedEventBindingSet;
00165
00166
00167 TimeOrderedEventBindingSet m_time_ordered_event_queue;
00168
00169
00170
00171
00172 TimeOrderedEventBindingSet m_buffered_time_ordered_event_queue;
00173
00174
00175
00176
00177
00178 Uint32 m_current_event_id;
00179
00180 };
00181
00182 template <typename ParameterType>
00183 void EventQueue::ScheduleMatchingEventsForDeletion (
00184 bool (*EventMatchingFunction)(Event const *, ParameterType),
00185 ParameterType parameter)
00186 {
00187
00188 for (TimeOrderedEventBindingSet::iterator it = m_time_ordered_event_queue.begin(),
00189 it_end = m_time_ordered_event_queue.end();
00190 it != it_end;
00191 ++it)
00192 {
00193 Event const *event = it->GetEvent();
00194 ASSERT1(event != NULL);
00195
00196 if (!event->IsScheduledForDeletion())
00197
00198 if (EventMatchingFunction(event, parameter))
00199 event->ScheduleForDeletion();
00200 }
00201 }
00202
00203 template <typename Parameter1Type, typename Parameter2Type>
00204 void EventQueue::ScheduleMatchingEventsForDeletion (
00205 bool (*EventMatchingFunction)(Event const *, Parameter1Type, Parameter2Type),
00206 Parameter1Type parameter1,
00207 Parameter2Type parameter2)
00208 {
00209
00210 for (TimeOrderedEventBindingSet::iterator it = m_time_ordered_event_queue.begin(),
00211 it_end = m_time_ordered_event_queue.end();
00212 it != it_end;
00213 ++it)
00214 {
00215 Event const *event = it->GetEvent();
00216 ASSERT1(event != NULL);
00217
00218 if (!event->IsScheduledForDeletion())
00219
00220 if (EventMatchingFunction(event, parameter1, parameter2))
00221 event->ScheduleForDeletion();
00222 }
00223 }
00224
00225 }
00226
00227 #endif // !defined(_XRB_EVENTQUEUE_HPP_)
00228