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

actor.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_MESSAGE_BUS_ACTOR_HPP
10 #define EAGINE_MESSAGE_BUS_ACTOR_HPP
11 
12 #include "subscriber.hpp"
13 
14 namespace eagine::msgbus {
15 //------------------------------------------------------------------------------
21 template <
22  std::size_t N,
23  template <std::size_t> class Subscriber = static_subscriber>
24 class actor
25  : public connection_user
26  , public friend_of_endpoint {
27  using friend_of_endpoint::_accept_message;
28  using friend_of_endpoint::_make_endpoint;
29  using friend_of_endpoint::_move_endpoint;
30 
31 public:
33  actor(actor&&) = delete;
34 
36  actor(const actor&) = delete;
37 
39  auto operator=(actor&&) = delete;
40 
42  auto operator=(const actor&) = delete;
43 
45  auto bus() noexcept -> endpoint& {
46  return _endpoint;
47  }
48 
50  auto add_connection(std::unique_ptr<connection> conn) -> bool final {
51  return _endpoint.add_connection(std::move(conn));
52  }
53 
54  void allow_subscriptions() {
55  _subscriber.allow_subscriptions();
56  }
57 
60  void process_one() {
61  _endpoint.update();
62  _subscriber.process_one();
63  }
64 
67  void process_all() {
68  _endpoint.update();
69  _subscriber.process_all();
70  }
71 
72 protected:
73  auto _process_message(
74  message_id msg_id,
76  const message_view& message) -> bool {
77  // TODO: use message age
78  if(!_accept_message(_endpoint, msg_id, message)) {
79  if(!is_special_message(msg_id)) {
80  _endpoint.block_message_type(msg_id);
81  }
82  }
83  return true;
84  }
85 
87  template <
88  typename Class,
89  typename... MsgMaps,
90  typename = std::enable_if_t<sizeof...(MsgMaps) == N>>
91  actor(main_ctx_object obj, Class* instance, MsgMaps... msg_maps)
92  : _endpoint{_make_endpoint(
93  std::move(obj),
94  {this, EAGINE_THIS_MEM_FUNC_C(_process_message)})}
95  , _subscriber{_endpoint, instance, msg_maps...} {
96  _subscriber.announce_subscriptions();
97  }
98 
100  template <
101  typename Derived,
102  typename Class,
103  typename... MsgMaps,
104  typename = std::enable_if_t<
105  (sizeof...(MsgMaps) == N) && std::is_base_of_v<actor, Derived>>>
106  actor(Derived&& temp, Class* instance, MsgMaps... msg_maps) noexcept
107  : _endpoint{_move_endpoint(
108  std::move(temp._endpoint),
109  {this, EAGINE_THIS_MEM_FUNC_C(_process_message)})}
110  , _subscriber{_endpoint, instance, msg_maps...} {}
111 
112  ~actor() noexcept override {
113  try {
114  _subscriber.retract_subscriptions();
115  _endpoint.finish();
116  } catch(...) {
117  }
118  }
119 
120 private:
121  endpoint _endpoint;
122  Subscriber<N> _subscriber;
123 };
124 //------------------------------------------------------------------------------
125 } // namespace eagine::msgbus
126 
127 #endif // EAGINE_MESSAGE_BUS_ACTOR_HPP
actor(main_ctx_object obj, Class *instance, MsgMaps... msg_maps)
Constructor usable from derived classes.
Definition: actor.hpp:91
auto update() -> bool
Updates the internal state, sends and receives pending messages.
Base class for main context objects.
Definition: main_ctx_object.hpp:71
static constexpr auto is_special_message(message_id msg_id) noexcept
Indicates if the specified message id denotes a special message bus message.
Definition: message.hpp:36
@ endpoint
Message bus client endpoint.
Base class for message bus actors with fixed number of message handlers.
Definition: actor.hpp:24
auto bus() noexcept -> endpoint &
Returns a reference to the associated endpoint.
Definition: actor.hpp:45
void process_all()
Processes all enqueued messages for which there are handlers.
Definition: actor.hpp:67
auto operator=(actor &&)=delete
Not moved assignable.
Interface for classes that can use message bus connections.
Definition: connection.hpp:155
Combines message information and a non-owning view to message content.
Definition: message.hpp:288
Message bus code is placed in this namespace.
Definition: eagine.hpp:58
#define EAGINE_THIS_MEM_FUNC_C(FUNC)
Macro for creating object of member_function_constant in member functions.
Definition: mem_func_const.hpp:206
Message bus client endpoint that can send and receive messages.
Definition: endpoint.hpp:30
auto add_connection(std::unique_ptr< connection > conn) -> bool final
Adds a connection for communication with a message bus router.
actor(Derived &&temp, Class *instance, MsgMaps... msg_maps) noexcept
Constructor usable from derived classes.
Definition: actor.hpp:106
actor(actor &&)=delete
Not move constructible.
void finish()
Says to the message bus that this endpoint is disconnecting.
Definition: endpoint.hpp:154
void block_message_type(message_id)
Sends a message to router to start blocking message type for this endpoint.
Class storing two identifier values representing class/method pair.
Definition: message_id.hpp:25
auto add_connection(std::unique_ptr< connection > conn) -> bool final
Adds a connection to the associated endpoint.
Definition: actor.hpp:50
Base for classes that need access to enpoint internal functionality.
Definition: endpoint.hpp:501
std::chrono::duration< float > message_age
Alias for message age type.
Definition: message.hpp:54
void process_one()
Processes a single enqueued message for which there is a handler.
Definition: actor.hpp:60

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