diff --git a/include/etl/message_packet.h b/include/etl/message_packet.h index da5141e3..df4d6d29 100644 --- a/include/etl/message_packet.h +++ b/include/etl/message_packet.h @@ -76,25 +76,18 @@ namespace etl private: - using ThisPacketType = etl::message_packet; + template + static constexpr bool IsMessagePacket = etl::is_same_v< etl::remove_const_t>, etl::message_packet>; template - static constexpr bool IsMessagePacket = etl::is_same_v, ThisPacketType>; + static constexpr bool IsInMessageList = etl::is_one_of_v>, TMessageTypes...>; template - static constexpr bool IsInMessageList = etl::is_one_of_v, TMessageTypes...>; - - template - static constexpr bool IsIMessage = etl::is_same_v, etl::imessage>; - - template - static constexpr bool IsDerivedFromIMessage = etl::is_base_of_v>; + static constexpr bool IsIMessage = etl::is_same_v>, etl::imessage>; public: //******************************************** - /// Default constructor - //******************************************** #include "private/diagnostic_uninitialized_push.h" message_packet() : valid(false) @@ -102,89 +95,40 @@ namespace etl } #include "private/diagnostic_pop.h" - //******************************************** - /// Copy constructor - //******************************************** -#include "private/diagnostic_uninitialized_push.h" - message_packet(const message_packet& other) - : valid(other.is_valid()) - { - if (valid) - { - add_new_message(other.get()); - } - } -#include "private/diagnostic_pop.h" - - //******************************************** - /// Move constructor - //******************************************** -#include "private/diagnostic_uninitialized_push.h" - message_packet(message_packet&& other) - : valid(other.is_valid()) - { - if (valid) - { - add_new_message(etl::move(other.get())); - } - } -#include "private/diagnostic_pop.h" - -#include "private/diagnostic_uninitialized_push.h" //******************************************** /// //******************************************** - explicit message_packet(const etl::imessage& msg) +#include "private/diagnostic_uninitialized_push.h" + template + explicit message_packet(T&& msg) : valid(true) { - if (accepts(msg)) + if constexpr (IsIMessage) { - add_new_message(msg); - valid = true; + if (accepts(msg)) + { + add_new_message(etl::forward(msg)); + valid = true; + } + else + { + valid = false; + } + + ETL_ASSERT(valid, ETL_ERROR(unhandled_message_exception)); + } + else if constexpr (IsInMessageList) + { + add_new_message_type(etl::forward(msg)); + } + else if constexpr (IsMessagePacket) + { + copy(etl::forward(msg)); } else { - valid = false; + ETL_STATIC_ASSERT(IsInMessageList, "Message not in packet type list"); } - - ETL_ASSERT(valid, ETL_ERROR(unhandled_message_exception)); - } -#include "private/diagnostic_pop.h" - -#include "private/diagnostic_uninitialized_push.h" - //******************************************** - /// - //******************************************** - explicit message_packet(etl::imessage&& msg) - : valid(true) - { - if (accepts(msg)) - { - add_new_message(etl::move(msg)); - valid = true; - } - else - { - valid = false; - } - - ETL_ASSERT(valid, ETL_ERROR(unhandled_message_exception)); - } -#include "private/diagnostic_pop.h" - -#include "private/diagnostic_uninitialized_push.h" - //******************************************** - /// Enabled for types that are not a message packet or etl::imessage - /// Invokes a static assert for types not in the message type list. - //******************************************** - template && - !IsIMessage, int>> - explicit message_packet(T&& msg) - : valid(true) - { - ETL_STATIC_ASSERT(IsInMessageList, "Message type not in packet type list"); - - add_new_message_type(etl::forward(msg)); } #include "private/diagnostic_pop.h" @@ -286,7 +230,7 @@ namespace etl //********************************************** template static ETL_CONSTEXPR - typename etl::enable_if_t, bool> + typename etl::enable_if::value, bool>::type accepts() { return accepts(); @@ -294,7 +238,7 @@ namespace etl enum { - SIZE = etl::largest::size, + SIZE = etl::largest::size, ALIGNMENT = etl::largest::alignment }; @@ -344,11 +288,11 @@ namespace etl /// Only enabled for types that are in the typelist. //******************************************** template - etl::enable_if_t> + etl::enable_if_t>, TMessageTypes...>, void> add_new_message_type(TMessage&& msg) { void* p = data; - new (p) etl::remove_cvref_t((etl::forward(msg))); + new (p) etl::remove_reference_t((etl::forward(msg))); } #include "private/diagnostic_pop.h"