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