Line data Source code
1 : // Copyright 2024 Accenture. 2 : 3 : /** 4 : * \ingroup async 5 : */ 6 : #pragma once 7 : 8 : #include "async/Types.h" 9 : 10 : namespace async 11 : { 12 : /** 13 : * A template class that encapsulates specific event behavior, allowing setEvent to be used without 14 : * explicitly specifying the eventMask. 15 : * 16 : * \tparam EventDispatcher EventDispatcher class, from which the Event is encapsulated. 17 : * \tparam Event Event number to encapsulate. 18 : */ 19 : template<typename EventDispatcher, size_t Event> 20 : class EventPolicy 21 : { 22 : public: 23 : using EventDispatcherType = EventDispatcher; 24 : using HandlerFunctionType = typename EventDispatcher::HandlerFunctionType; 25 : 26 : static EventMaskType const EVENT_MASK = static_cast<EventMaskType>(1U << Event); 27 : 28 : explicit EventPolicy(EventDispatcher& eventDispatcher); 29 : 30 : void setEventHandler(HandlerFunctionType handlerFunction); 31 : void removeEventHandler(); 32 : 33 : void setEvent(); 34 : 35 : private: 36 : EventDispatcher& _eventDispatcher; 37 : }; 38 : 39 : /** 40 : * Inline implementations. 41 : */ 42 : template<typename EventDispatcher, size_t Event> 43 183 : inline EventPolicy<EventDispatcher, Event>::EventPolicy(EventDispatcher& eventDispatcher) 44 183 : : _eventDispatcher(eventDispatcher) 45 : {} 46 : 47 : template<typename EventDispatcher, size_t Event> 48 : inline void 49 364 : EventPolicy<EventDispatcher, Event>::setEventHandler(HandlerFunctionType const handlerFunction) 50 : { 51 364 : _eventDispatcher.setEventHandler(Event, handlerFunction); 52 362 : } 53 : 54 : template<typename EventDispatcher, size_t Event> 55 2 : inline void EventPolicy<EventDispatcher, Event>::removeEventHandler() 56 : { 57 2 : _eventDispatcher.removeEventHandler(Event); 58 : } 59 : 60 : template<typename EventDispatcher, size_t Event> 61 19 : inline void EventPolicy<EventDispatcher, Event>::setEvent() 62 : { 63 19 : _eventDispatcher.setEvents(EVENT_MASK); 64 9 : } 65 : 66 : } // namespace async