2 #ifndef _YI_EVENT_DISPATCHER_H_ 3 #define _YI_EVENT_DISPATCHER_H_ 13 #include <condition_variable> 22 class CYISignalEmitEventHandler;
26 enum class EYIPriorityQueuePriority;
27 template<
typename YI_PRIORITY_QUEUE_ITEM,
class YI_PRIORITY_QUEUE_ALLOCATOR>
44 virtual void EventLoopStarted(
const std::shared_ptr<CYIEventDispatcher> &pDispatcher) = 0;
46 virtual void EventLoopExited(
const std::shared_ptr<CYIEventDispatcher> &pDispatcher) = 0;
70 typedef std::deque<std::shared_ptr<CYIEventHandlerProxy>> YI_EVENT_HANDLER_LIST;
71 typedef std::deque<std::shared_ptr<CYIEventFilterProxy>> YI_EVENT_FILTER_LIST;
72 typedef std::deque<IYIEventDispatcherListener *> YI_EVENT_DISPATCHER_LISTENER_LIST;
74 typedef std::vector<std::pair<CYIThreadHandle, std::shared_ptr<CYIEventDispatcher>>> YI_DISPATCHER_MAP;
105 bool Exit(
bool join =
false,
bool waitUntilEmpty =
false);
110 void WaitUntilEventQueueRunning();
120 bool WaitUntilEventPosted(uint64_t timeoutMs = 0);
127 bool ProcessOneEvent();
134 bool ProcessAllEvents();
139 bool ProcessAllEvents(uint64_t timeoutUs);
163 bool ProcessEventsOfTypes(
const std::vector<CYIEvent::Type> &types);
286 bool SendEvent(std::unique_ptr<CYIEvent> pEvent);
309 bool SendEvent(std::unique_ptr<CYIEvent> pEvent,
CYIEventHandler *pDestination);
314 size_t GetEventCount()
const;
326 void DiscardAllPosts();
341 static std::shared_ptr<CYIEventDispatcher> GetDispatcher(
const CYIThreadHandle &threadAffinity);
346 static const std::shared_ptr<CYIEventDispatcher> &GetDefaultDispatcher();
358 static const std::shared_ptr<CYIEventDispatcher> &GetDrawDispatcher();
363 static void SetDispatcher(
const CYIThreadHandle &threadAffinity,
const std::shared_ptr<CYIEventDispatcher> &pDispatcher);
373 static void SetDefaultDispatcher(
const std::shared_ptr<CYIEventDispatcher> &pDispatcher);
378 static void RemoveDefaultDispatcher();
383 static void SetDrawDispatcher(
const std::shared_ptr<CYIEventDispatcher> &pDispatcher);
388 static void RemoveDrawDispatcher();
409 bool PreFilter(
CYIEvent *pEvent,
const std::shared_ptr<CYIEventHandlerProxy> &pDestination);
410 bool PostFilter(
CYIEvent *pEvent,
const std::shared_ptr<CYIEventHandlerProxy> &pDestination);
411 bool DispatchEvent(
CYIEvent *pEvent,
const std::shared_ptr<CYIEventHandlerProxy> &pDestination);
412 bool ProcessEvent(
CYIEvent *pEvent,
const std::shared_ptr<CYIEventHandlerProxy> &pDestination);
414 bool ProcessEventsOfTypes(
const CYIEvent::Type *pFirstItem,
size_t count);
416 void NotifyEventLoopStarted();
417 void NotifyEventLoopExited();
418 void NotifyEventDispatchStarted(
CYIEvent *pEvent);
419 void NotifyEventDispatchEnded(
CYIEvent *pEvent);
426 bool m_eventQueueRunning;
428 std::unique_ptr<CYISignalEmitEventHandler> m_pSignalEmitEventHandler;
429 std::unique_ptr<CYIDeferredDeletionEventHandler> m_pDeferredDeletionEventHandler;
430 std::unique_ptr<CYITaskEventHandler> m_pTaskEventHandler;
432 YI_EVENT_HANDLER_LIST m_eventHandlerList;
433 YI_EVENT_FILTER_LIST m_eventFilterList;
434 YI_EVENT_DISPATCHER_LISTENER_LIST m_eventDispatcherListenerList;
435 std::unique_ptr<YI_EVENT_QUEUE> m_pEventQueue;
441 mutable std::mutex m_eventQueueMutex;
442 std::condition_variable m_eventQueueWaitCondition;
443 std::condition_variable m_waitUntilEventWaitCondition;
445 static YI_DISPATCHER_MAP s_dispatcherMap;
446 static std::shared_ptr<CYIEventDispatcher> s_dispatcherDefault;
448 static std::shared_ptr<CYIEventDispatcher> s_dispatcherDraw;
457 template<
typename YI_TYPE>
470 YI_LOGD(
"YiDeleteLater",
"No event dispatcher. YiDeleteLater called while the application is shutting down, so deleting ptr immediately.");
virtual void EventPostFilteredByHandler(const std::shared_ptr< CYIEventDispatcher > &pDispatcher, CYIEvent *pEvent, CYIEventFilter *pFilter, CYIEventHandler *pHandler)=0
Allows registering of event filters which can be used to perform processing on an event before and af...
Definition: YiEventHandler.h:25
#define YI_LOGD(tag, message,...)
Definition: YiLogger.h:129
Base class for events.
Definition: YiEvent.h:27
static std::shared_ptr< CYIEventDispatcher > GetDispatcher(const CYIThreadHandle &threadAffinity)
virtual ~IYIEventDispatcherListener()
Definition: YiEventDispatcher.h:42
virtual void EventDispatchStarted(const std::shared_ptr< CYIEventDispatcher > &pDispatcher, CYIEvent *pEvent)=0
virtual void EventDispatchEnded(const std::shared_ptr< CYIEventDispatcher > &pDispatcher, CYIEvent *pEvent)=0
Proxy for a CYIEventFilter allowing safe destruction of the internal event filter objects...
Definition: YiEventFilterProxy.h:24
static const std::shared_ptr< CYIEventDispatcher > & GetDefaultDispatcher()
Definition: YiEventDispatcher.h:28
virtual void EventHandled(const std::shared_ptr< CYIEventDispatcher > &pDispatcher, CYIEvent *pEvent, CYIEventHandler *pHandler)=0
Handles events where data deletion needs to be deferred.
Definition: YiDeferredDeletionEventHandler.h:17
static CYIThreadHandle GetCurrentThreadId()
Definition: YiReadWriteMutex.h:16
Definition: YiSignalHandler.h:175
virtual void EventLoopExited(const std::shared_ptr< CYIEventDispatcher > &pDispatcher)=0
virtual void EventLoopStarted(const std::shared_ptr< CYIEventDispatcher > &pDispatcher)=0
virtual void EventPreFilteredByDispatcher(const std::shared_ptr< CYIEventDispatcher > &pDispatcher, CYIEvent *pEvent, CYIEventFilter *pFilter)=0
Priority
Definition: YiEventDispatcher.h:84
Handles events where tasks are executed when the event is processed on an event queue.
Definition: YiTaskEventHandler.h:15
Stores, queues and dispatches events as well as sending notifications to IYIEventDispatcherListener o...
Definition: YiEventDispatcher.h:66
Allows children that inherit from this class to implement processing before and after events are hand...
Definition: YiEventFilter.h:24
void YiDeleteLater(std::unique_ptr< YI_TYPE > pPtr)
Adds the given pointer to the deferred deletion queue.
Definition: YiEventDispatcher.h:458
IYIEventDispatcherListener()
Definition: YiEventDispatcher.h:41
Implementors of this Listener interface can register themselves with the CYIEventDispather and become...
Definition: YiEventDispatcher.h:38
Priority
Priority of the event in the event queue.
Definition: YiEvent.h:99
Definition: YiThreadHandle.h:45
virtual void EventPreFilteredByHandler(const std::shared_ptr< CYIEventDispatcher > &pDispatcher, CYIEvent *pEvent, CYIEventFilter *pFilter, CYIEventHandler *pHandler)=0
Proxy for a CYIEventHandler allowing safe destruction of the internal event handler objects...
Definition: YiEventHandlerProxy.h:25
virtual void EventPostFilteredByDispatcher(const std::shared_ptr< CYIEventDispatcher > &pDispatcher, CYIEvent *pEvent, CYIEventFilter *pFilter)=0
Event for handling deferred deletion of data of type YI_TYPE. SfinaeHelper type is used to control a ...
Definition: YiDeferredDeletionEvent.h:30
Type
The event type of the CYIEvent class instance.
Definition: YiEvent.h:33