Skip to content

Commit 24ec3ba

Browse files
ktfdennisklein
authored andcommitted
feat(EventManager)!: Out of line some methods
BREAKING CHANGE: `fair::mq::PropertyChangeAsString` moved to `<fairmq/EventManager.h>` (from `<fairmq/Properties.h>`)
1 parent e4f258c commit 24ec3ba

File tree

4 files changed

+73
-35
lines changed

4 files changed

+73
-35
lines changed

fairmq/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
################################################################################
2-
# Copyright (C) 2012-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH #
2+
# Copyright (C) 2012-2025 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH #
33
# #
44
# This software is distributed under the terms of the #
55
# GNU Lesser General Public Licence (LGPL) version 3, #
@@ -119,6 +119,7 @@ if(BUILD_FAIRMQ)
119119
Channel.cxx
120120
Device.cxx
121121
DeviceRunner.cxx
122+
EventManager.cxx
122123
JSONParser.cxx
123124
MemoryResources.cxx
124125
Plugin.cxx

fairmq/EventManager.cxx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/********************************************************************************
2+
* Copyright (C) 2025 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3+
* *
4+
* This software is distributed under the terms of the *
5+
* GNU Lesser General Public Licence (LGPL) version 3, *
6+
* copied verbatim in the file "LICENSE" *
7+
********************************************************************************/
8+
#include "EventManager.h"
9+
10+
#include <string>
11+
#include <typeindex>
12+
13+
template std::shared_ptr<
14+
fair::mq::EventManager::Signal<fair::mq::PropertyChangeAsString, std::string>>
15+
fair::mq::EventManager::GetSignal<fair::mq::PropertyChangeAsString, std::string>(
16+
const std::pair<std::type_index, std::type_index>& key) const;
17+
18+
template void fair::mq::EventManager::Subscribe<fair::mq::PropertyChangeAsString, std::string>(
19+
const std::string& subscriber,
20+
std::function<void(typename fair::mq::PropertyChangeAsString::KeyType, std::string)>);

fairmq/EventManager.h

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/********************************************************************************
2-
* Copyright (C) 2014-2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
2+
* Copyright (C) 2014-2025 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
33
* *
44
* This software is distributed under the terms of the *
55
* GNU Lesser General Public Licence (LGPL) version 3, *
@@ -57,27 +57,8 @@ class EventManager
5757
template<typename E, typename ...Args>
5858
using Signal = boost::signals2::signal<void(typename E::KeyType, Args...)>;
5959

60-
template<typename E, typename ...Args>
61-
auto Subscribe(const std::string& subscriber, std::function<void(typename E::KeyType, Args...)> callback) -> void
62-
{
63-
const std::type_index event_type_index{typeid(E)};
64-
const std::type_index callback_type_index{typeid(std::function<void(typename E::KeyType, Args...)>)};
65-
const auto signalsKey = std::make_pair(event_type_index, callback_type_index);
66-
const auto connectionsKey = std::make_pair(subscriber, signalsKey);
67-
68-
const auto connection = GetSignal<E, Args...>(signalsKey)->connect(callback);
69-
70-
{
71-
std::lock_guard<std::mutex> lock{fMutex};
72-
73-
if (fConnections.find(connectionsKey) != fConnections.end())
74-
{
75-
fConnections.at(connectionsKey).disconnect();
76-
fConnections.erase(connectionsKey);
77-
}
78-
fConnections.insert({connectionsKey, connection});
79-
}
80-
}
60+
template<typename E, typename... Args>
61+
auto Subscribe(const std::string& subscriber, std::function<void(typename E::KeyType, Args...)> callback) -> void;
8162

8263
template<typename E, typename ...Args>
8364
auto Unsubscribe(const std::string& subscriber) -> void
@@ -119,21 +100,58 @@ class EventManager
119100
mutable std::mutex fMutex;
120101

121102
template<typename E, typename ...Args>
122-
auto GetSignal(const SignalsKey& key) const -> std::shared_ptr<Signal<E, Args...>>
103+
auto GetSignal(const SignalsKey& key) const -> std::shared_ptr<Signal<E, Args...>>;
104+
}; /* class EventManager */
105+
106+
struct PropertyChangeAsString : Event<std::string> {};
107+
108+
template<typename E, typename... Args>
109+
auto EventManager::GetSignal(const SignalsKey& key) const -> std::shared_ptr<Signal<E, Args...>>
110+
{
111+
std::lock_guard<std::mutex> lock{fMutex};
112+
113+
if (fSignals.find(key) == fSignals.end()) {
114+
// wrapper is needed because boost::signals2::signal is neither copyable nor movable
115+
// and I don't know how else to insert it into the map
116+
auto signal = std::make_shared<Signal<E, Args...>>();
117+
fSignals.insert(std::make_pair(key, signal));
118+
}
119+
120+
return boost::any_cast<std::shared_ptr<Signal<E, Args...>>>(fSignals.at(key));
121+
}
122+
123+
template<typename E, typename... Args>
124+
auto EventManager::Subscribe(const std::string& subscriber,
125+
std::function<void(typename E::KeyType, Args...)> callback) -> void
126+
{
127+
const std::type_index event_type_index{typeid(E)};
128+
const std::type_index callback_type_index{
129+
typeid(std::function<void(typename E::KeyType, Args...)>)};
130+
const auto signalsKey = std::make_pair(event_type_index, callback_type_index);
131+
const auto connectionsKey = std::make_pair(subscriber, signalsKey);
132+
133+
const auto connection = GetSignal<E, Args...>(signalsKey)->connect(callback);
134+
123135
{
124136
std::lock_guard<std::mutex> lock{fMutex};
125137

126-
if (fSignals.find(key) == fSignals.end())
127-
{
128-
// wrapper is needed because boost::signals2::signal is neither copyable nor movable
129-
// and I don't know how else to insert it into the map
130-
auto signal = std::make_shared<Signal<E, Args...>>();
131-
fSignals.insert(std::make_pair(key, signal));
138+
if (fConnections.find(connectionsKey) != fConnections.end()) {
139+
fConnections.at(connectionsKey).disconnect();
140+
fConnections.erase(connectionsKey);
132141
}
133-
134-
return boost::any_cast<std::shared_ptr<Signal<E, Args...>>>(fSignals.at(key));
142+
fConnections.insert({connectionsKey, connection});
135143
}
136-
}; /* class EventManager */
144+
}
145+
146+
extern template std::shared_ptr<
147+
fair::mq::EventManager::Signal<fair::mq::PropertyChangeAsString, std::string>>
148+
fair::mq::EventManager::GetSignal<fair::mq::PropertyChangeAsString, std::string>(
149+
const std::pair<std::type_index, std::type_index>& key) const;
150+
151+
extern template void
152+
fair::mq::EventManager::Subscribe<fair::mq::PropertyChangeAsString, std::string>(
153+
const std::string& subscriber,
154+
std::function<void(typename fair::mq::PropertyChangeAsString::KeyType, std::string)>);
137155

138156
} // namespace fair::mq
139157

fairmq/Properties.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/********************************************************************************
2-
* Copyright (C) 2014-2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
2+
* Copyright (C) 2014-2025 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
33
* *
44
* This software is distributed under the terms of the *
55
* GNU Lesser General Public Licence (LGPL) version 3, *
@@ -29,7 +29,6 @@ using Property = boost::any;
2929
using Properties = std::map<std::string, Property>;
3030

3131
struct PropertyChange : Event<std::string> {};
32-
struct PropertyChangeAsString : Event<std::string> {};
3332

3433
class PropertyHelper
3534
{

0 commit comments

Comments
 (0)