9 #ifndef FAIR_MQ_EVENTMANAGER_H 10 #define FAIR_MQ_EVENTMANAGER_H 16 #include <unordered_map> 20 #include <boost/any.hpp> 21 #include <boost/functional/hash.hpp> 22 #include <boost/signals2.hpp> 59 template<
typename E,
typename ...Args>
60 using Signal = boost::signals2::signal<void(
typename E::KeyType, Args...)>;
62 template<
typename E,
typename ...Args>
63 auto Subscribe(
const std::string& subscriber, std::function<
void(
typename E::KeyType, Args...)> callback) ->
void 65 const std::type_index event_type_index{
typeid(E)};
66 const std::type_index callback_type_index{
typeid(std::function<void(
typename E::KeyType, Args...)>)};
67 const auto signalsKey = std::make_pair(event_type_index, callback_type_index);
68 const auto connectionsKey = std::make_pair(subscriber, signalsKey);
70 const auto connection = GetSignal<E, Args...>(signalsKey)->connect(callback);
73 std::lock_guard<std::mutex> lock{fMutex};
75 if (fConnections.find(connectionsKey) != fConnections.end())
77 fConnections.at(connectionsKey).disconnect();
78 fConnections.erase(connectionsKey);
80 fConnections.insert({connectionsKey, connection});
84 template<
typename E,
typename ...Args>
85 auto Unsubscribe(
const std::string& subscriber) ->
void 87 const std::type_index event_type_index{
typeid(E)};
88 const std::type_index callback_type_index{
typeid(std::function<void(
typename E::KeyType, Args...)>)};
89 const auto signalsKey = std::make_pair(event_type_index, callback_type_index);
90 const auto connectionsKey = std::make_pair(subscriber, signalsKey);
92 std::lock_guard<std::mutex> lock{fMutex};
94 fConnections.at(connectionsKey).disconnect();
95 fConnections.erase(connectionsKey);
98 template<
typename E,
typename ...Args>
99 auto Emit(
typename E::KeyType key, Args... args)
const ->
void 101 const std::type_index event_type_index{
typeid(E)};
102 const std::type_index callback_type_index{
typeid(std::function<void(
typename E::KeyType, Args...)>)};
103 const auto signalsKey = std::make_pair(event_type_index, callback_type_index);
105 (*GetSignal<E, Args...>(signalsKey))(key, std::forward<Args>(args)...);
109 using SignalsKey = std::pair<std::type_index, std::type_index>;
111 using SignalsValue = boost::any;
112 using SignalsMap = std::unordered_map<SignalsKey, SignalsValue, boost::hash<SignalsKey>>;
113 mutable SignalsMap fSignals;
115 using ConnectionsKey = std::pair<std::string, SignalsKey>;
117 using ConnectionsValue = boost::signals2::connection;
118 using ConnectionsMap = std::unordered_map<ConnectionsKey, ConnectionsValue, boost::hash<ConnectionsKey>>;
119 ConnectionsMap fConnections;
121 mutable std::mutex fMutex;
123 template<
typename E,
typename ...Args>
124 auto GetSignal(
const SignalsKey& key)
const -> std::shared_ptr<Signal<E, Args...>>
126 std::lock_guard<std::mutex> lock{fMutex};
128 if (fSignals.find(key) == fSignals.end())
132 auto signal = std::make_shared<Signal<E, Args...>>();
133 fSignals.insert(std::make_pair(key, signal));
136 return boost::any_cast<std::shared_ptr<Signal<E, Args...>>>(fSignals.at(key));
Definition: EventManager.h:31
Manages event callbacks from different subscribers.
Definition: EventManager.h:51
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23