Skip to content

Commit 257ad18

Browse files
Refactoring of EventHandler
1 parent 166b1df commit 257ad18

File tree

3 files changed

+66
-34
lines changed

3 files changed

+66
-34
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,8 @@ add_library(
430430
${PROJECT_SOURCE_DIR}/src/Output.cpp
431431
${PROJECT_SOURCE_DIR}/src/Utilities.h
432432
${PROJECT_SOURCE_DIR}/src/Utilities.cpp
433+
${PROJECT_SOURCE_DIR}/src/EventHandler.h
434+
${PROJECT_SOURCE_DIR}/src/EventHandler.cpp
433435
${PROJECT_SOURCE_DIR}/src/Tasks/TaskBase.h
434436
${PROJECT_SOURCE_DIR}/src/Tasks/TaskBase.cpp
435437
${PROJECT_SOURCE_DIR}/src/TaskHandler.h

src/EventHandler.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
The Supporting Hyperplane Optimization Toolkit (SHOT).
3+
4+
@author Andreas Lundell, Åbo Akademi University
5+
6+
@section LICENSE
7+
This software is licensed under the Eclipse Public License 2.0.
8+
Please see the README and LICENSE files for more information.
9+
*/
10+
11+
#include "EventHandler.h"
12+
13+
namespace SHOT
14+
{
15+
16+
EventHandler::EventHandler(EnvironmentPtr envPtr) : env(envPtr)
17+
{
18+
}
19+
20+
void EventHandler::notify(const E_EventType& event, std::any args) const
21+
{
22+
env->output->outputTrace(
23+
"Notifying callbacks for event: " + std::to_string(static_cast<int>(event)) + " (args)");
24+
auto it = notificationCallbacks.find(event);
25+
if(it != notificationCallbacks.end())
26+
{
27+
for(const auto& callback : it->second)
28+
{
29+
callback(args);
30+
}
31+
}
32+
}
33+
34+
void EventHandler::notify(const E_EventType& event) const
35+
{
36+
env->output->outputTrace(
37+
"Notifying callbacks for event: " + std::to_string(static_cast<int>(event)) + " (no args)");
38+
notify(event, std::any());
39+
}
40+
41+
bool EventHandler::hasDataProvider(const E_EventType& event) const
42+
{
43+
return dataProviders.find(event) != dataProviders.end()
44+
|| parameterizedDataProviders.find(event) != parameterizedDataProviders.end();
45+
}
46+
47+
bool EventHandler::hasNotificationCallbacks(const E_EventType& event) const
48+
{
49+
auto it = notificationCallbacks.find(event);
50+
return it != notificationCallbacks.end() && !it->second.empty();
51+
}
52+
53+
bool EventHandler::hasAnyCallbacks(const E_EventType& event) const
54+
{
55+
return hasDataProvider(event) || hasNotificationCallbacks(event);
56+
}
57+
58+
} // namespace SHOT

src/EventHandler.h

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ namespace SHOT
5656
class EventHandler
5757
{
5858
public:
59-
inline EventHandler(EnvironmentPtr envPtr) : env(envPtr) {};
59+
EventHandler(EnvironmentPtr envPtr);
6060

6161
/**
6262
* @brief Unified callback registration method
@@ -134,31 +134,14 @@ class EventHandler
134134
* @param event The event type to notify
135135
* @param args Arguments to pass to the callbacks (wrapped in std::any)
136136
*/
137-
void notify(const E_EventType& event, std::any args) const
138-
{
139-
env->output->outputTrace(
140-
"Notifying callbacks for event: " + std::to_string(static_cast<int>(event)) + " (args)");
141-
auto it = notificationCallbacks.find(event);
142-
if(it != notificationCallbacks.end())
143-
{
144-
for(const auto& callback : it->second)
145-
{
146-
callback(args);
147-
}
148-
}
149-
}
137+
void notify(const E_EventType& event, std::any args) const;
150138

151139
/**
152140
* @brief Notify callbacks with no arguments
153141
*
154142
* @param event The event type to notify
155143
*/
156-
void notify(const E_EventType& event) const
157-
{
158-
env->output->outputTrace(
159-
"Notifying callbacks for event: " + std::to_string(static_cast<int>(event)) + " (no args)");
160-
notify(event, std::any());
161-
}
144+
void notify(const E_EventType& event) const;
162145

163146
/**
164147
* @brief Request data from a registered data provider (no arguments)
@@ -217,34 +200,23 @@ class EventHandler
217200
* @param event The event type to check
218201
* @return true if a data provider is registered, false otherwise
219202
*/
220-
bool hasDataProvider(const E_EventType& event) const
221-
{
222-
return dataProviders.find(event) != dataProviders.end()
223-
|| parameterizedDataProviders.find(event) != parameterizedDataProviders.end();
224-
}
203+
bool hasDataProvider(const E_EventType& event) const;
225204

226205
/**
227206
* @brief Check if notification callbacks are registered for an event
228207
*
229208
* @param event The event type to check
230209
* @return true if notification callbacks are registered, false otherwise
231210
*/
232-
bool hasNotificationCallbacks(const E_EventType& event) const
233-
{
234-
auto it = notificationCallbacks.find(event);
235-
return it != notificationCallbacks.end() && !it->second.empty();
236-
}
211+
bool hasNotificationCallbacks(const E_EventType& event) const;
237212

238213
/**
239214
* @brief Check if any callbacks (data providers or notifications) are registered for an event
240215
*
241216
* @param event The event type to check
242217
* @return true if any callbacks are registered, false otherwise
243218
*/
244-
bool hasAnyCallbacks(const E_EventType& event) const
245-
{
246-
return hasDataProvider(event) || hasNotificationCallbacks(event);
247-
}
219+
bool hasAnyCallbacks(const E_EventType& event) const;
248220

249221
private:
250222
/// Map of event types to their registered notification callbacks

0 commit comments

Comments
 (0)