rotor
Event loop friendly C++ actor micro-framework
 
Loading...
Searching...
No Matches
message.h
1#pragma once
2
3//
4// Copyright (c) 2019-2024 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
5//
6// Distributed under the MIT Software License
7//
8
9#include "arc.hpp"
10#include "address.hpp"
11#include <typeindex>
12#include <deque>
13
14#if defined(_MSC_VER)
15#pragma warning(push)
16#pragma warning(disable : 4251)
17#endif
18
19namespace rotor {
20
21struct message_base_t;
22
36struct ROTOR_API message_visitor_t {
37 virtual ~message_visitor_t() = default;
38
40 virtual bool try_visit(const message_base_t &message, void *context) const = 0;
41};
42
52struct message_base_t : public arc_base_t<message_base_t> {
53 virtual ~message_base_t() = default;
54
62 const void *type_index;
63
66
69
71 inline message_base_t(const void *type_index_, const address_ptr_t &addr)
72 : type_index(type_index_), address{addr} {}
73};
74
75namespace message_support {
76ROTOR_API const void *register_type(const std::type_index &type_index) noexcept;
77}
78
83template <typename T> struct message_t : public message_base_t {
84
86 using payload_t = T;
87
96 struct ROTOR_API visitor_t {
97 virtual ~visitor_t() = default;
98
100 virtual void on(const message_t &, void *) {}
101 };
102
104 template <typename... Args>
105 message_t(const address_ptr_t &addr, Args &&...args)
106 : message_base_t{message_type, addr}, payload{std::forward<Args>(args)...} {}
107
110
112 static const void *message_type;
113};
114
115template <typename T> const void *message_t<T>::message_type = message_support::register_type(typeid(message_t<T>));
116
119
121using messages_queue_t = std::deque<message_ptr_t>;
122
124template <typename M, typename... Args> auto make_message(const address_ptr_t &addr, Args &&...args) -> message_ptr_t {
125 assert(addr);
126 return message_ptr_t{new message_t<M>(addr, std::forward<Args>(args)...)};
127}
128
145template <typename M, typename... Args>
146auto make_routed_message(const address_ptr_t &addr, const address_ptr_t &route_addr, Args &&...args) -> message_ptr_t {
147 assert(addr);
148 auto message = message_ptr_t{new message_t<M>(addr, std::forward<Args>(args)...)};
149 message->next_route = route_addr;
150 return message;
151}
152
153} // namespace rotor
154
155#if defined(_MSC_VER)
156#pragma warning(pop)
157#endif
Basic namespace for all rotor functionalities.
Definition rotor.hpp:21
intrusive_ptr_t< message_base_t > message_ptr_t
intrusive pointer for message
Definition message.h:118
intrusive_ptr_t< address_t > address_ptr_t
intrusive pointer for address
Definition address.hpp:57
std::deque< message_ptr_t > messages_queue_t
structure to hold messages (intrusive pointers)
Definition message.h:121
boost::intrusive_ref_counter< T, counter_policy_t > arc_base_t
base class to inject ref-counter with the specified policy
Definition arc.hpp:24
auto make_routed_message(const address_ptr_t &addr, const address_ptr_t &route_addr, Args &&...args) -> message_ptr_t
constructs message by constructing it's payload; after delivery to destination address (to all subscr...
Definition message.h:146
auto make_message(const address_ptr_t &addr, Args &&...args) -> message_ptr_t
constructs message by constructing it's payload; intrusive pointer for the message is returned
Definition message.h:124
boost::intrusive_ptr< T > intrusive_ptr_t
alias for intrusive pointer
Definition arc.hpp:27
Base class for rotor message.
Definition message.h:52
address_ptr_t next_route
post-delivery destination address, see make_routed_message() for usage
Definition message.h:68
message_base_t(const void *type_index_, const address_ptr_t &addr)
constructor which takes destination address
Definition message.h:71
const void * type_index
unique message type pointer.
Definition message.h:62
address_ptr_t address
message destination address
Definition message.h:65
struct visitor_t concrete message type visitor
Definition message.h:96
virtual void on(const message_t &, void *)
visit concrete message
Definition message.h:100
the generic message meant to hold user-specific payload
Definition message.h:83
static const void * message_type
unique per-message-type pointer used for routing
Definition message.h:112
T payload_t
alias for payload type
Definition message.h:86
message_t(const address_ptr_t &addr, Args &&...args)
forwards args for payload construction
Definition message.h:105
T payload
user-defined payload
Definition message.h:109
Abstract message visitor interface.
Definition message.h:36
virtual bool try_visit(const message_base_t &message, void *context) const =0
returns true if a message has been successfully processed