/****************************************************************************** The MIT License(MIT) Embedded Template Library. https://github.com/ETLCPP/etl https://www.etlcpp.com Copyright(c) 2020 John Wellbelove Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions : The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ #ifndef ETL_MESSAGE_PACKET_INCLUDED #define ETL_MESSAGE_PACKET_INCLUDED #include "platform.h" #include "alignment.h" #include "error_handler.h" #include "largest.h" #include "message.h" #include "static_assert.h" #include "type_list.h" #include "utility.h" #include namespace etl { #if ETL_USING_CPP17 && !defined(ETL_MESSAGE_PACKET_FORCE_CPP03_IMPLEMENTATION) //*************************************************************************** // The definition for all message types. //*************************************************************************** template class message_packet { private: template static constexpr bool IsMessagePacket = etl::is_same_v>, etl::message_packet>; template static constexpr bool IsInMessageList = etl::is_one_of_v>, TMessageTypes...>; template static constexpr bool IsIMessage = etl::is_same_v>, etl::imessage>; public: using message_types = etl::type_list; //******************************************** #include "private/diagnostic_uninitialized_push.h" constexpr message_packet() noexcept : valid(false) { } #include "private/diagnostic_pop.h" //******************************************** /// //******************************************** #include "private/diagnostic_uninitialized_push.h" template || IsInMessageList, int>::type> explicit message_packet(T&& msg) : valid(true) { if constexpr (IsIMessage) { 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 { ETL_STATIC_ASSERT(IsInMessageList, "Message not in packet type list"); } } #include "private/diagnostic_pop.h" //********************************************** message_packet(const message_packet& other) { valid = other.is_valid(); if (valid) { add_new_message(other.get()); } } #if ETL_USING_CPP11 //********************************************** message_packet(message_packet&& other) { valid = other.is_valid(); if (valid) { add_new_message(etl::move(other.get())); } } #endif //********************************************** void copy(const message_packet& other) { valid = other.is_valid(); if (valid) { add_new_message(other.get()); } } //********************************************** void copy(message_packet&& other) { valid = other.is_valid(); if (valid) { add_new_message(etl::move(other.get())); } } //********************************************** #include "private/diagnostic_uninitialized_push.h" message_packet& operator=(const message_packet& rhs) { delete_current_message(); valid = rhs.is_valid(); if (valid) { add_new_message(rhs.get()); } return *this; } #include "private/diagnostic_pop.h" //********************************************** #include "private/diagnostic_uninitialized_push.h" message_packet& operator=(message_packet&& rhs) { delete_current_message(); valid = rhs.is_valid(); if (valid) { add_new_message(etl::move(rhs.get())); } return *this; } #include "private/diagnostic_pop.h" //******************************************** ~message_packet() { delete_current_message(); } //******************************************** etl::imessage& get() ETL_NOEXCEPT { return *static_cast(data); } //******************************************** const etl::imessage& get() const ETL_NOEXCEPT { return *static_cast(data); } //******************************************** bool is_valid() const { return valid; } //********************************************** static ETL_CONSTEXPR bool accepts(etl::message_id_t id) { return (accepts_message(id) || ...); } //********************************************** static ETL_CONSTEXPR bool accepts(const etl::imessage& msg) { return accepts(msg.get_message_id()); } //********************************************** template static ETL_CONSTEXPR bool accepts() { return (accepts_message() || ...); } //********************************************** template static ETL_CONSTEXPR typename etl::enable_if::value, bool>::type accepts() { return accepts(); } enum { SIZE = etl::largest::size, ALIGNMENT = etl::largest::alignment }; private: //********************************************** template static bool accepts_message() { return Id1 == Id2; } //********************************************** template static bool accepts_message(etl::message_id_t id2) { return Id1 == id2; } //******************************************** #include "private/diagnostic_uninitialized_push.h" void delete_current_message() { if (valid) { etl::imessage* pmsg = static_cast(data); #if ETL_HAS_VIRTUAL_MESSAGES pmsg->~imessage(); #else delete_message(pmsg); #endif } } #include "private/diagnostic_pop.h" #if !ETL_HAS_VIRTUAL_MESSAGES //******************************************** void delete_message(etl::imessage* pmsg) { (delete_message_type(pmsg) || ...); } //******************************************** template bool delete_message_type(etl::imessage* pmsg) { if (TType::ID == pmsg->get_message_id()) { TType* p = static_cast(pmsg); p->~TType(); return true; } else { return false; } } #endif //******************************************** void add_new_message(const etl::imessage& msg) { (add_new_message_type(msg) || ...); } //******************************************** void add_new_message(etl::imessage&& msg) { (add_new_message_type(etl::move(msg)) || ...); } #include "private/diagnostic_uninitialized_push.h" //******************************************** /// Only enabled for types that are in the typelist. //******************************************** template etl::enable_if_t< etl::is_one_of_v>, TMessageTypes...>, void> add_new_message_type(TMessage&& msg) { void* p = data; new (p) etl::remove_reference_t((etl::forward(msg))); } #include "private/diagnostic_pop.h" #include "private/diagnostic_uninitialized_push.h" //******************************************** template bool add_new_message_type(const etl::imessage& msg) { if (TType::ID == msg.get_message_id()) { void* p = data; new (p) TType(static_cast(msg)); return true; } else { return false; } } #include "private/diagnostic_pop.h" #include "private/diagnostic_uninitialized_push.h" //******************************************** template bool add_new_message_type(etl::imessage&& msg) { if (TType::ID == msg.get_message_id()) { void* p = data; new (p) TType(static_cast(msg)); return true; } else { return false; } } #include "private/diagnostic_pop.h" typename etl::aligned_storage::type data; bool valid; }; //*************************************************************************** // The definition for no message types. //*************************************************************************** template <> class message_packet<> { private: template static constexpr bool IsMessagePacket = etl::is_same_v>, etl::message_packet<>>; template static constexpr bool IsInMessageList = false; template static constexpr bool IsIMessage = etl::is_same_v>, etl::imessage>; public: using message_types = etl::type_list<>; //******************************************** #include "private/diagnostic_uninitialized_push.h" constexpr message_packet() noexcept {} #include "private/diagnostic_pop.h" //********************************************** message_packet(const message_packet& /*other*/) {} #if ETL_USING_CPP11 //********************************************** message_packet(message_packet&& /*other*/) {} #endif //********************************************** void copy(const message_packet& /*other*/) {} //********************************************** void copy(message_packet&& /*other*/) {} //********************************************** #include "private/diagnostic_uninitialized_push.h" message_packet& operator=(const message_packet& /*rhs*/) { return *this; } #include "private/diagnostic_pop.h" //********************************************** #include "private/diagnostic_uninitialized_push.h" message_packet& operator=(message_packet&& /*rhs*/) { return *this; } #include "private/diagnostic_pop.h" //******************************************** ~message_packet() {} //******************************************** bool is_valid() const { return false; } //********************************************** static ETL_CONSTEXPR bool accepts(etl::message_id_t /*id*/) { return false; } //********************************************** static ETL_CONSTEXPR bool accepts(const etl::imessage& /*msg*/) { return false; } //********************************************** template static ETL_CONSTEXPR bool accepts() { return false; } //********************************************** template static ETL_CONSTEXPR typename etl::enable_if::value, bool>::type accepts() { return false; } enum { SIZE = 0, ALIGNMENT = 1 }; }; //*************************************************************************** /// Helper to turn etl::type_list into /// etl::message_packet template struct message_packet_from_type_list; template struct message_packet_from_type_list> { using type = etl::message_packet; }; template using message_packet_from_type_list_t = typename message_packet_from_type_list::type; #else #include "private/message_packet_cpp03.h" #endif } // namespace etl #endif