|
1 | 1 | /******************************************************************************** |
2 | | - * Copyright (C) 2014-2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * |
| 2 | + * Copyright (C) 2014-2025 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * |
3 | 3 | * * |
4 | 4 | * This software is distributed under the terms of the * |
5 | 5 | * GNU Lesser General Public Licence (LGPL) version 3, * |
@@ -57,27 +57,8 @@ class EventManager |
57 | 57 | template<typename E, typename ...Args> |
58 | 58 | using Signal = boost::signals2::signal<void(typename E::KeyType, Args...)>; |
59 | 59 |
|
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; |
81 | 62 |
|
82 | 63 | template<typename E, typename ...Args> |
83 | 64 | auto Unsubscribe(const std::string& subscriber) -> void |
@@ -119,21 +100,58 @@ class EventManager |
119 | 100 | mutable std::mutex fMutex; |
120 | 101 |
|
121 | 102 | 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 | + |
123 | 135 | { |
124 | 136 | std::lock_guard<std::mutex> lock{fMutex}; |
125 | 137 |
|
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); |
132 | 141 | } |
133 | | - |
134 | | - return boost::any_cast<std::shared_ptr<Signal<E, Args...>>>(fSignals.at(key)); |
| 142 | + fConnections.insert({connectionsKey, connection}); |
135 | 143 | } |
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)>); |
137 | 155 |
|
138 | 156 | } // namespace fair::mq |
139 | 157 |
|
|
0 commit comments