blob: a2a2c224d7181bd1bb451c8eb6ccb7ad3656aa0a [file] [log] [blame]
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +01001#pragma once
2
3#include "messanger_service.hpp"
4
5#include <boost/asio.hpp>
6
7namespace utils
8{
9
10template <class Service>
11class MessangerT
12{
13 public:
14 explicit MessangerT(boost::asio::execution_context& execution_context) :
15 service_(boost::asio::use_service<Service>(execution_context)),
16 context_(service_.create())
17 {}
18
19 ~MessangerT()
20 {
21 service_.destroy(context_);
22 }
23
24 template <class EventType>
25 void on_receive(std::function<void(const EventType&)> handler)
26 {
27 context_.handlers.emplace_back(std::move(handler));
28 }
29
30 template <class EventType>
31 void send(const EventType& event)
32 {
33 service_.send(event);
34 }
35
36 private:
37 Service& service_;
38 typename Service::Context& context_;
39};
40
41using Messanger = MessangerT<MessangerService>;
42
43} // namespace utils