diff --git a/include/etl/endianness.h b/include/etl/endianness.h index 99522171..5db22523 100644 --- a/include/etl/endianness.h +++ b/include/etl/endianness.h @@ -86,9 +86,9 @@ namespace etl private: - static etl::endian get() + ETL_CONSTEXPR static etl::endian get() { - static const union U + static ETL_CONSTANT union U { U() : ui32(0x12345678UL) diff --git a/include/etl/unaligned_type.h b/include/etl/unaligned_type.h new file mode 100644 index 00000000..4fe25b04 --- /dev/null +++ b/include/etl/unaligned_type.h @@ -0,0 +1,108 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2022 jwellbelove + +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_UNALIGNED_TYPE_INCLUDED +#define ETL_UNALIGNED_TYPE_INCLUDED + +///\defgroup unaligned_types unaligned_types +/// Unaligned types utilities +///\ingroup utilities + +#include "platform.h" +#include "type_traits.h" +#include "endianness.h" + +namespace etl +{ + template ::value, void>::type> + class unaligned_type + { + public: + + typedef type T; + + //************************************************************************* + /// + //************************************************************************* + unaligned_type(T value) + { + // Same as host? + if (Endian != etl::endianness::get()) + { + value = etl::reverse_bytes(value); + } + + mempy(storage, &value, sizeof(T)); + } + + //************************************************************************* + /// + //************************************************************************* + operator T() const + { + T value; + + mempy(&value, storage, sizeof(T)); + + // Same as host? + if (Endian != etl::endianness::get()) + { + value = etl::reverse_bytes(value); + } + + return value; + } + + private: + + char storage[sizeof(T)]; + }; + + typedef unaligned_type net_char_t; + typedef unaligned_type net_schar_t; + typedef unaligned_type net_uchar_t; + typedef unaligned_type net_int32_t_t; + typedef unaligned_type net_uint32_t_t; + +#if ETL_CPP11_SUPPORTED + typedef unaligned_type host_char_t; + typedef unaligned_type host_schar_t; + typedef unaligned_type host_uchar_t; + typedef unaligned_type host_int32_t_t; + typedef unaligned_type host_uint32_t_t; +#endif + +#if ETL_CPP11_SUPPORTED + template + using unaligned_type_t = typename etl::unaligned_type::type +#endif +} + +#endif