Skip to main content

BasicEventManager.ixx File

Default implementation of an EventManager. More...

Included Headers

#include <functional> #include <memory> #include <utility> #include <helios.event.Event> #include <helios.event.Dispatcher> #include <helios.event.EventQueue> #include <helios.event.EventManager>

Namespaces Index

namespacehelios
namespaceevent

Event system. More...

Classes Index

classBasicEventManager

Basic implementation of the EventManager. More...

Description

Default implementation of an EventManager.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file BasicEventManager.ixx
3 * @brief Default implementation of an EventManager.
4 */
5module;
6
7#include <functional>
8#include <memory>
9#include <utility>
10
11export module helios.event.BasicEventManager;
12
13import helios.event.EventManager;
14import helios.event.EventQueue;
15import helios.event.Dispatcher;
16import helios.event.Event;
17
18using namespace helios::event;
19
20export namespace helios::event {
21
22
23 /**
24 * @brief Basic implementation of the EventManager.
25 *
26 * This implementation provides the concrete logic for
27 * post() and dispatchAll().
28 */
29 class BasicEventManager final : public EventManager {
30
31 public:
33
34 /**
35 * @brief Constructs a new BasicEventManager, using the specified EventQueue
36 * and the Dispatcher.
37 *
38 * @param eventQueue
39 * @param dispatcher
40 */
41 explicit BasicEventManager(std::unique_ptr<EventQueue> eventQueue,
42 std::unique_ptr<Dispatcher> dispatcher) :
43 EventManager(std::move(eventQueue), std::move(dispatcher))
44 {}
45
46
47 /**
48 * @brief Posts an event based on the specified policy to the EventQueue.
49 *
50 * @param event A unique_ptr to the event that should be posted
51 * @param policy The policy to use for positing the event.
52 * @param func A comparison function for LATEST_WINS policy. If none is specified,
53 * this method will compare the events based on type and tag equality.
54 *
55 * @return EventManager
56 */
58 std::unique_ptr<const Event> event,
59 PostPolicy policy,
60 const std::function<bool(
61 const std::unique_ptr<const Event>& event,
62 const std::unique_ptr<const Event>& evt)>& func
63 ) override {
64
65 std::function<bool(const std::unique_ptr<const Event>& event,
66 const std::unique_ptr<const Event>& evt)> cmpFunc;
67
68 switch (policy) {
69 case APPEND:
70 eventQueue_->add(std::move(event));
71 break;
72 case LATEST_WINS:
73
74 if (!func) {
75 cmpFunc = [](
76 const std::unique_ptr<const Event>& event,
77 const std::unique_ptr<const Event>& evt) {
78 if (!event || !evt) {
79 return false;
80 }
81 return typeid(*event) == typeid(*evt) && event->tag() == evt->tag();
82 };
83 } else {
84 cmpFunc = func;
85 }
86
87 /**
88 * @todo use hashmap instead of queue for faster
89 * lookup
90 */
91 eventQueue_->addOrReplace(std::move(event), cmpFunc);
92 break;
93 default:
94 std::unreachable();
95 }
96
97
98 return *this;
99 }
100
101
102 /**
103 * @brief Dispatches all events of the queue, passing ownership to the
104 * underlying Dispatcher.
105 * Clears the underlying EventQueue.
106 *
107 * @return EventManager
108 */
110 while (!eventQueue_->empty()) {
111 auto e = eventQueue_->next();
112 dispatcher_->dispatch(std::move(e));
113 }
114
115 return *this;
116 }
117
118
119
120 };
121
122}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.