xref: /openbmc/telemetry/src/utils/messanger.hpp (revision c1dbac16fe3c705b773155f607a4541cf2ad79fa)
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:
MessangerT(boost::asio::execution_context & execution_context)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(const MessangerT&) = delete;
20     MessangerT& operator=(const MessangerT&) = delete;
21     MessangerT(MessangerT&&) = delete;
22     MessangerT& operator=(MessangerT&&) = delete;
23 
~MessangerT()24     ~MessangerT()
25     {
26         service_.destroy(context_);
27     }
28 
29     template <class EventType>
on_receive(std::function<void (const EventType &)> handler)30     void on_receive(std::function<void(const EventType&)> handler)
31     {
32         context_.handlers.emplace_back(std::move(handler));
33     }
34 
35     template <class EventType>
send(const EventType & event)36     void send(const EventType& event)
37     {
38         service_.send(event);
39     }
40 
41   private:
42     Service& service_;
43     typename Service::Context& context_;
44 };
45 
46 using Messanger = MessangerT<MessangerService>;
47 
48 } // namespace utils
49