9 #ifndef FAIR_MQ_EVENTMANAGER_H 10 #define FAIR_MQ_EVENTMANAGER_H 18 #include <unordered_map> 22 #include <boost/any.hpp> 23 #include <boost/functional/hash.hpp> 24 #include <boost/signals2.hpp> 61 template<
typename E,
typename ...Args>
62 using Signal = boost::signals2::signal<void(
typename E::KeyType, Args...)>;
64 template<
typename E,
typename ...Args>
65 auto Subscribe(
const std::string& subscriber, std::function<
void(
typename E::KeyType, Args...)> callback) ->
void 67 const std::type_index event_type_index{
typeid(E)};
68 const std::type_index callback_type_index{
typeid(std::function<void(
typename E::KeyType, Args...)>)};
69 const auto signalsKey = std::make_pair(event_type_index, callback_type_index);
70 const auto connectionsKey = std::make_pair(subscriber, signalsKey);
72 const auto connection = GetSignal<E, Args...>(signalsKey)->connect(callback);
75 std::lock_guard<std::mutex> lock{fMutex};
77 if (fConnections.find(connectionsKey) != fConnections.end())
79 fConnections.at(connectionsKey).disconnect();
80 fConnections.erase(connectionsKey);
82 fConnections.insert({connectionsKey, connection});
86 template<
typename E,
typename ...Args>
87 auto Unsubscribe(
const std::string& subscriber) ->
void 89 const std::type_index event_type_index{
typeid(E)};
90 const std::type_index callback_type_index{
typeid(std::function<void(
typename E::KeyType, Args...)>)};
91 const auto signalsKey = std::make_pair(event_type_index, callback_type_index);
92 const auto connectionsKey = std::make_pair(subscriber, signalsKey);
94 std::lock_guard<std::mutex> lock{fMutex};
96 fConnections.at(connectionsKey).disconnect();
97 fConnections.erase(connectionsKey);
100 template<
typename E,
typename ...Args>
101 auto Emit(
typename E::KeyType key, Args... args)
const ->
void 103 const std::type_index event_type_index{
typeid(E)};
104 const std::type_index callback_type_index{
typeid(std::function<void(
typename E::KeyType, Args...)>)};
105 const auto signalsKey = std::make_pair(event_type_index, callback_type_index);
107 (*GetSignal<E, Args...>(signalsKey))(key, std::forward<Args>(args)...);
111 using SignalsKey = std::pair<std::type_index, std::type_index>;
113 using SignalsValue = boost::any;
114 using SignalsMap = std::unordered_map<SignalsKey, SignalsValue, boost::hash<SignalsKey>>;
115 mutable SignalsMap fSignals;
117 using ConnectionsKey = std::pair<std::string, SignalsKey>;
119 using ConnectionsValue = boost::signals2::connection;
120 using ConnectionsMap = std::unordered_map<ConnectionsKey, ConnectionsValue, boost::hash<ConnectionsKey>>;
121 ConnectionsMap fConnections;
123 mutable std::mutex fMutex;
125 template<
typename E,
typename ...Args>
126 auto GetSignal(
const SignalsKey& key)
const -> std::shared_ptr<Signal<E, Args...>>
128 std::lock_guard<std::mutex> lock{fMutex};
130 if (fSignals.find(key) == fSignals.end())
134 auto signal = std::make_shared<Signal<E, Args...>>();
135 fSignals.insert(std::make_pair(key, signal));
138 return boost::any_cast<std::shared_ptr<Signal<E, Args...>>>(fSignals.at(key));
Definition: EventManager.h:33
Manages event callbacks from different subscribers.
Definition: EventManager.h:53
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23