20 #ifndef ANTKEEPER_EVENT_DISPATCHER_HPP
21 #define ANTKEEPER_EVENT_DISPATCHER_HPP
56 auto iterator = subscribers.emplace(std::type_index(
typeid(T)), shared_subscriber);
59 return std::make_shared<subscription>
61 std::static_pointer_cast<void>(shared_subscriber),
62 [
this, iterator = std::move(iterator)]()
64 this->subscribers.erase(iterator);
80 const auto range = subscribers.equal_range(std::type_index(
typeid(T)));
81 for (
auto i = range.first; i != range.second; ++i)
84 std::any_cast<subscriber<T>>(*(i->second))(message);
89 std::multimap<std::type_index, std::shared_ptr<std::any>> subscribers;
Forwards messages from publishers to subscribers.
std::shared_ptr< subscription > subscribe(subscriber< T > &&subscriber)
Subscribes a function object to messages dispatched by this dispatcher.
void dispatch(const T &message) const
Dispatches a message to subscribers of the message type.
Publish-subscribe messaging.
std::function< void(const T &)> subscriber
Subscriber function object type.