OGLplus  (0.59.0) a C++ wrapper for rendering APIs

ability.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_MESSAGE_BUS_SERVICE_ABILITY_HPP
10 #define EAGINE_MESSAGE_BUS_SERVICE_ABILITY_HPP
11 
12 #include "../serialize.hpp"
13 #include "../subscriber.hpp"
14 
15 namespace eagine::msgbus {
16 //------------------------------------------------------------------------------
17 template <typename Base = subscriber>
18 class ability_provider : public Base {
19  using This = ability_provider;
20 
21 protected:
22  using Base::Base;
23 
24  void add_methods() {
25  Base::add_methods();
26  Base::add_method(
27  this, EAGINE_MSG_MAP(Ability, query, This, _handle_query));
28  }
29 
30 public:
31  virtual auto can_handle(message_id) -> bool = 0;
32 
33 private:
34  auto _handle_query(const message_context& msg_ctx, stored_message& message)
35  -> bool {
36  message_id msg_id{};
37  if(default_deserialize_message_type(msg_id, message.content())) {
38  if(can_handle()) {
39  msg_ctx.bus().respond_to(
40  message,
41  EAGINE_MSG_ID(Ability, response),
42  {message.content()});
43  }
44  }
45  return true;
46  }
47 };
48 //------------------------------------------------------------------------------
49 template <typename Base = subscriber>
50 class ability_tester : public Base {
51 
52  using This = ability_tester;
53 
54 protected:
55  using Base::Base;
56 
57  void add_methods() {
58  Base::add_methods();
59  Base::add_method(
60  this, EAGINE_MSG_MAP(Ability, response, This, _handle_response));
61  }
62 
63 public:
64  void find_handler(message_id msg_id) {
65  std::array<byte, 32> temp{};
66  auto serialized{default_serialize(msg_id, cover(temp))};
67  EAGINE_ASSERT(serialized);
68 
69  message_view message{extract(serialized)};
70  this->bus().broadcast(EAGINE_MSG_ID(Ability, query), message);
71  }
72 
73  virtual void on_handler_found(identifier_t target_id, message_id) = 0;
74 
75 private:
76  auto _handle_response(const message_context&, stored_message& message)
77  -> bool {
78  message_id msg_id{};
79  if(default_deserialize_message_type(msg_id, message.content())) {
80  on_handler_found(message.source_id, msg_id);
81  }
82  return true;
83  }
84 };
85 //------------------------------------------------------------------------------
86 } // namespace eagine::msgbus
87 
88 #endif // EAGINE_MESSAGE_BUS_SERVICE_ABILITY_HPP
#define EAGINE_MSG_MAP(CLASS_ID, METHOD_ID, CLASS, METHOD)
Constructs an instance of static message handler map.
Definition: handler_map.hpp:72
#define EAGINE_MSG_ID(API, NAME)
Macro for instantiating objects of static_message_id.
Definition: message_id.hpp:148
static constexpr auto cover(T *addr, S size) noexcept -> span_if_mutable< T >
Creates a span starting at the specified pointer and specified length.
Definition: span.hpp:465
static constexpr auto extract(api_result_value< Result, api_result_validity::never > &) noexcept -> Result &
Overload of extract for api_result_value.
Definition: c_api_wrap.hpp:270
auto default_serialize(T &value, memory::block blk) -> serialization_result< memory::const_block >
Uses the default backend to serialize a value into a memory block.
Definition: serialize.hpp:191
Message bus code is placed in this namespace.
Definition: eagine.hpp:58
std::uint64_t identifier_t
The underlying integer type for eagine::identifier.
Definition: identifier_t.hpp:19
auto bus() noexcept -> auto &
Returns a reference to the associated endpoint.
Definition: subscriber.hpp:38
auto default_deserialize_message_type(message_id &msg_id, memory::const_block blk)
Default-deserializes the specified message id from a memory block.
Definition: serialize.hpp:266
@ message_id
The message type id has been verified.

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).