blob: a2a2c224d7181bd1bb451c8eb6ccb7ad3656aa0a [file] [log] [blame]
#pragma once
#include "messanger_service.hpp"
#include <boost/asio.hpp>
namespace utils
{
template <class Service>
class MessangerT
{
public:
explicit MessangerT(boost::asio::execution_context& execution_context) :
service_(boost::asio::use_service<Service>(execution_context)),
context_(service_.create())
{}
~MessangerT()
{
service_.destroy(context_);
}
template <class EventType>
void on_receive(std::function<void(const EventType&)> handler)
{
context_.handlers.emplace_back(std::move(handler));
}
template <class EventType>
void send(const EventType& event)
{
service_.send(event);
}
private:
Service& service_;
typename Service::Context& context_;
};
using Messanger = MessangerT<MessangerService>;
} // namespace utils