21#include "../messaging/message_registry.hpp"
81template<
typename Registry,
typename... AllowedPayloadTypes>
87 template<
typename PayloadT>
88 struct FindMessageDef {
96 template<
typename... MessageDefs>
99 using UnderlyingMailbox =
decltype(extract_mailbox_type(
static_cast<Registry*
>(
nullptr)));
101 UnderlyingMailbox mailbox_;
104 template<
typename PayloadT>
105 static constexpr bool is_allowed_type = (std::is_same_v<PayloadT, AllowedPayloadTypes> || ...);
108 template<
typename PayloadT>
109 static constexpr bool is_sendable_type = is_allowed_type<PayloadT>;
111 template<
typename PayloadT>
112 static constexpr bool is_registered_type = Registry::template is_registered<PayloadT>;
126 Registry::template max_size_for_types<AllowedPayloadTypes...>();
133 template<
typename PayloadT>
135 return is_allowed_type<PayloadT>;
149 static_assert(
sizeof...(AllowedPayloadTypes) > 0,
150 "TypedMailbox requires at least one allowed payload type");
153 static_assert((is_registered_type<AllowedPayloadTypes> && ...),
154 "All allowed types must be registered in the message registry");
170 return mailbox_.start();
178 return mailbox_.is_running();
182 return mailbox_.mailbox_id();
201 template<
typename PayloadT>
202 auto send(PayloadT& message, uint32_t dest_mailbox)
205 static_assert(is_sendable_type<PayloadT>,
206 "Message type not sendable from this mailbox. "
207 "Check that PayloadT is in AllowedPayloadTypes or SendOnlyTypes.");
209 static_assert(is_registered_type<PayloadT>,
210 "Message type not registered in the message registry.");
215 .
msg_type = Registry::template get_message_id<PayloadT>(),
224 return mailbox_.send(tims_msg, dest_mailbox);
236 template<
typename PayloadT>
237 auto send(PayloadT& message, uint32_t dest_mailbox, uint64_t timestamp)
240 static_assert(is_sendable_type<PayloadT>,
241 "Message type not sendable from this mailbox. "
242 "Check that PayloadT is in AllowedPayloadTypes or SendOnlyTypes.");
244 static_assert(is_registered_type<PayloadT>,
245 "Message type not registered in the message registry.");
250 .
msg_type = Registry::template get_message_id<PayloadT>(),
252 .timestamp = timestamp,
259 return mailbox_.send(tims_msg, dest_mailbox);
265 template<
typename PayloadT>
269 static_assert(is_sendable_type<PayloadT>,
270 "Message type not sendable from this mailbox.");
272 static_assert(is_registered_type<PayloadT>,
273 "Message type not registered in the message registry.");
275 return mailbox_.send(tims_message, dest_mailbox);
288 template<
typename PayloadT>
290 static_assert(is_allowed_type<PayloadT>,
291 "Message type not allowed in this typed mailbox. "
292 "Check that PayloadT is in the AllowedPayloadTypes list.");
293 static_assert(is_registered_type<PayloadT>,
294 "Message type not registered in the message registry.");
295 return mailbox_.template receive<PayloadT>();
305 template<
typename PayloadT>
307 static_assert(is_allowed_type<PayloadT>,
308 "Message type not allowed in this typed mailbox.");
309 static_assert(is_registered_type<PayloadT>,
310 "Message type not registered in the message registry.");
311 return mailbox_.template receive_for<PayloadT>(timeout);
320 template<
typename PayloadT>
322 static_assert(is_allowed_type<PayloadT>,
323 "Message type not allowed in this typed mailbox.");
324 static_assert(is_registered_type<PayloadT>,
325 "Message type not registered in the message registry.");
326 return mailbox_.template try_receive<PayloadT>();
335 template<
typename Visitor>
337 return mailbox_.receive_any(std::forward<Visitor>(visitor));
347 template<
typename Visitor>
349 return mailbox_.receive_any_for(timeout, std::forward<Visitor>(visitor));
402template<
typename... Ts>
407template<
typename... Ts>
426template<
typename Registry,
typename... SendOnlyTypesInner>
429 template<
typename... MessageDefs>
432 using UnderlyingMailbox =
decltype(extract_mailbox_type(
static_cast<Registry*
>(
nullptr)));
433 UnderlyingMailbox mailbox_;
436 template<
typename PayloadT>
437 static constexpr bool is_send_only_type = (std::is_same_v<PayloadT, SendOnlyTypesInner> || ...);
439 template<
typename PayloadT>
440 static constexpr bool is_registered_type = Registry::template is_registered<PayloadT>;
449 .message_slots = config.message_slots,
451 .send_priority = config.send_priority,
452 .realtime = config.realtime,
453 .mailbox_name = config.mailbox_name
457 template<
typename PayloadT>
459 static_assert(is_send_only_type<PayloadT>,
460 "Message type not in SendOnlyTypes list.");
461 static_assert(is_registered_type<PayloadT>,
462 "Message type not registered in registry.");
466 .
msg_type = Registry::template get_message_id<PayloadT>(),
474 return mailbox_.send(tims_msg, dest_mailbox);
477 template<
typename PayloadT>
479 static_assert(is_send_only_type<PayloadT>,
"Message type not in SendOnlyTypes list.");
480 static_assert(is_registered_type<PayloadT>,
"Type not registered.");
484 .
msg_type = Registry::template get_message_id<PayloadT>(),
486 .timestamp = timestamp,
492 return mailbox_.send(tims_msg, dest_mailbox);
496 template<
typename PayloadT>
499 template<
typename PayloadT>
503 template<
typename Visitor>
510 void stop() { mailbox_.stop(); }
511 auto get_id() const -> uint32_t {
return mailbox_.get_id(); }
530template<
typename Registry,
typename... ReceiveTypesInner,
typename... SendOnlyTypesInner>
533 template<
typename... MessageDefs>
536 using UnderlyingMailbox =
decltype(extract_mailbox_type(
static_cast<Registry*
>(
nullptr)));
537 UnderlyingMailbox mailbox_;
540 template<
typename PayloadT>
541 static constexpr bool is_receive_type = (std::is_same_v<PayloadT, ReceiveTypesInner> || ...);
543 template<
typename PayloadT>
544 static constexpr bool is_send_only_type = (std::is_same_v<PayloadT, SendOnlyTypesInner> || ...);
546 template<
typename PayloadT>
547 static constexpr bool is_sendable_type = is_receive_type<PayloadT> || is_send_only_type<PayloadT>;
549 template<
typename PayloadT>
550 static constexpr bool is_registered_type = Registry::template is_registered<PayloadT>;
555 Registry::template max_size_for_types<ReceiveTypesInner...>();
560 .message_slots = config.message_slots,
562 .send_priority = config.send_priority,
563 .realtime = config.realtime,
564 .mailbox_name = config.mailbox_name
568 template<
typename PayloadT>
570 static_assert(is_sendable_type<PayloadT>,
571 "Message type not sendable from this mailbox.");
572 static_assert(is_registered_type<PayloadT>,
573 "Message type not registered in registry.");
577 .
msg_type = Registry::template get_message_id<PayloadT>(),
585 return mailbox_.send(tims_msg, dest_mailbox);
588 template<
typename PayloadT>
590 static_assert(is_sendable_type<PayloadT>,
"Message type not sendable.");
591 static_assert(is_registered_type<PayloadT>,
"Type not registered.");
595 .
msg_type = Registry::template get_message_id<PayloadT>(),
597 .timestamp = timestamp,
603 return mailbox_.send(tims_msg, dest_mailbox);
607 template<
typename PayloadT>
609 static_assert(is_receive_type<PayloadT>,
610 "Message type not receivable. Only ReceiveTypes can be received.");
611 static_assert(is_registered_type<PayloadT>,
"Type not registered.");
612 return mailbox_.template receive<PayloadT>();
615 template<
typename PayloadT>
617 static_assert(is_receive_type<PayloadT>,
"Only ReceiveTypes can be received.");
618 static_assert(is_registered_type<PayloadT>,
"Type not registered.");
619 return mailbox_.template receive_timed<PayloadT>(timeout_ns);
623 template<
typename Visitor>
625 return mailbox_.receive_any(std::forward<Visitor>(visitor));
630 void stop() { mailbox_.stop(); }
647template<
typename Registry,
typename PayloadT>
660template<
typename Registry,
typename... CommandTypes>
673template<
typename Registry,
typename... DataTypes>
Strongly-typed mailbox for message-based communication.
Compile-time message type registry using MessageDefinition templates.
Build a complete registry with automatic Module and Mailbox aliases.
auto start() -> MailboxResult< void >
auto send(PayloadT &message, uint32_t dest_mailbox, uint64_t timestamp) -> MailboxResult< void >
auto send(PayloadT &message, uint32_t dest_mailbox) -> MailboxResult< void >
auto & get_underlying_mailbox()
const auto & get_underlying_mailbox() const
auto receive_any(Visitor &&visitor) -> MailboxResult< void >
auto receive_timed(int64_t timeout_ns) -> MailboxResult< TimsMessage< PayloadT > >
TypedMailbox(const MailboxConfig &config)
auto receive() -> MailboxResult< TimsMessage< PayloadT > >
auto create() -> MailboxResult< void >
auto destroy() -> MailboxResult< void >
auto send(PayloadT &message, uint32_t dest_mailbox, uint64_t timestamp) -> MailboxResult< void >
TypedMailbox(const MailboxConfig &config)
auto get_id() const -> uint32_t
auto send(PayloadT &message, uint32_t dest_mailbox) -> MailboxResult< void >
auto receive_timed(int64_t timeout_ns) -> MailboxResult< TimsMessage< PayloadT > >=delete
auto start() -> MailboxResult< void >
auto receive() -> MailboxResult< TimsMessage< PayloadT > >=delete
auto receive_any(Visitor &&visitor) -> MailboxResult< void >=delete
Type-restricted mailbox with optimized buffer sizing.
auto send(const TimsMessage< PayloadT > &tims_message, uint32_t dest_mailbox) -> MailboxResult< void >
Send a TimsMessage (type-restricted)
static constexpr size_t max_message_size
Maximum message size for allowed types only.
static constexpr bool is_allowed()
Check if a payload type is allowed in this mailbox.
TypedMailbox & operator=(const TypedMailbox &)=delete
const auto & get_underlying_mailbox() const
TypedMailbox(const MailboxConfig &config)
Construct a typed mailbox.
auto receive_for(std::chrono::milliseconds timeout) -> MailboxResult< TimsMessage< PayloadT > >
Receive with timeout for specific payload type.
auto start() -> MailboxResult< void >
static constexpr size_t num_allowed_types
TypedMailbox(const TypedMailbox &)=delete
auto receive() -> MailboxResult< TimsMessage< PayloadT > >
Blocking receive for specific payload type.
auto send(PayloadT &message, uint32_t dest_mailbox, uint64_t timestamp) -> MailboxResult< void >
Send a message with explicit timestamp (type-restricted)
uint32_t mailbox_id() const
auto & get_underlying_mailbox()
Receive any allowed message using a visitor.
auto send(PayloadT &message, uint32_t dest_mailbox) -> MailboxResult< void >
Send a message (type-restricted)
TypedMailbox(TypedMailbox &&) noexcept=default
auto receive_any_for(std::chrono::milliseconds timeout, Visitor &&visitor) -> MailboxResult< void >
Receive any allowed message type with timeout.
auto receive_any(Visitor &&visitor) -> MailboxResult< void >
Blocking receive any allowed message type using visitor pattern.
auto try_receive() -> MailboxResult< TimsMessage< PayloadT > >
Non-blocking receive (type-restricted)
CommRaT - Modern C++ Real-Time Communication Framework.
Message definition with compile-time ID assignment.
Tag type to mark send-only types in template parameter.
std::tuple< Ts... > Types
std::tuple< Ts... > Types