Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "messanger_service.hpp" |
| 4 | |
| 5 | #include <boost/asio.hpp> |
| 6 | |
| 7 | namespace utils |
| 8 | { |
| 9 | |
| 10 | template <class Service> |
| 11 | class 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 | |
| 41 | using Messanger = MessangerT<MessangerService>; |
| 42 | |
| 43 | } // namespace utils |