diff --git a/include/etl/experimental/mem_type.h b/include/etl/experimental/mem_cast.h similarity index 85% rename from include/etl/experimental/mem_type.h rename to include/etl/experimental/mem_cast.h index 886d5b39..395899fc 100644 --- a/include/etl/experimental/mem_type.h +++ b/include/etl/experimental/mem_cast.h @@ -28,8 +28,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ -#ifndef ETL_MEM_TYPE_INCLUDED -#define ETL_MEM_TYPE_INCLUDED +#ifndef ETL_MEM_CAST_INCLUDED +#define ETL_MEM_CAST_INCLUDED #include @@ -41,10 +41,10 @@ SOFTWARE. namespace etl { //***************************************************************************** - /// mem_type + /// mem_cast //***************************************************************************** template - class mem_type + class mem_cast { public: @@ -52,13 +52,13 @@ namespace etl static ETL_CONSTANT size_t Alignment = Alignment_; //*********************************** - ETL_CONSTEXPR mem_type() + ETL_CONSTEXPR mem_cast() { } //*********************************** template - ETL_CONSTEXPR mem_type(const mem_type& other) + ETL_CONSTEXPR mem_cast(const mem_cast& other) { ETL_STATIC_ASSERT(Size >= Other_Size, "Other size is too large"); ETL_STATIC_ASSERT(Alignment >= Other_Alignment, "Other alignment incompatible"); @@ -73,7 +73,7 @@ namespace etl ETL_STATIC_ASSERT(sizeof(T) <= Size, "Size of T is too large"); ETL_STATIC_ASSERT(Alignment >= etl::alignment_of::value, "Alignment of T is incompatible"); - return *reinterpret_cast(buffer); + return *static_cast(buffer); } //*********************************** @@ -83,7 +83,7 @@ namespace etl ETL_STATIC_ASSERT(sizeof(T) <= Size, "Size of T is too large"); ETL_STATIC_ASSERT(Alignment >= etl::alignment_of::value, "Alignment of T is incompatible"); - return *reinterpret_cast(buffer); + return *static_cast(buffer); } //*********************************** @@ -93,12 +93,12 @@ namespace etl ETL_STATIC_ASSERT(sizeof(T) <= Size, "Size of T is too large"); ETL_STATIC_ASSERT(Alignment >= etl::alignment_of::value, "Alignment of T is incompatible"); - return *reinterpret_cast(buffer); + return *static_cast(buffer); } //*********************************** template - ETL_CONSTEXPR mem_type& operator =(const mem_type& rhs) + ETL_CONSTEXPR mem_cast& operator =(const mem_cast& rhs) { ETL_STATIC_ASSERT(Size >= Other_Size, "RHS size is too large"); ETL_STATIC_ASSERT(Alignment >= Other_Alignment, "RHS alignment incompatible"); @@ -121,41 +121,47 @@ namespace etl } //*********************************** - ETL_CONSTEXPR char* data() const + ETL_CONSTEXPR char* data() + { + return buffer; + } + + //*********************************** + ETL_CONSTEXPR const char* data() const { return buffer; } private: - etl::aligned_storage buffer; + etl::uninitialized_buffer buffer; }; //***************************************************************************** - /// mem_type_ptr + /// mem_cast_ptr //***************************************************************************** template - class mem_type_ptr + class mem_cast_ptr { public: static ETL_CONSTANT size_t Size = Size_; //*********************************** - ETL_CONSTEXPR mem_type_ptr() + ETL_CONSTEXPR mem_cast_ptr() : pbuffer(ETL_NULLPTR) { } //*********************************** - ETL_CONSTEXPR mem_type_ptr(char* pbuffer_) + ETL_CONSTEXPR mem_cast_ptr(char* pbuffer_) : pbuffer(pbuffer_) { } //*********************************** template - ETL_CONSTEXPR mem_type_ptr(const mem_type_ptr& other, char* pbuffer_) + ETL_CONSTEXPR mem_cast_ptr(const mem_cast_ptr& other, char* pbuffer_) : pbuffer(pbuffer_) { ETL_STATIC_ASSERT(Size >= Other_Size, "Other size is too large"); @@ -198,7 +204,7 @@ namespace etl //*********************************** template - mem_type_ptr& operator =(const mem_type_ptr& rhs) + mem_cast_ptr& operator =(const mem_cast_ptr& rhs) { ETL_STATIC_ASSERT(Size >= Other_Size, "RHS size is too large"); @@ -226,11 +232,11 @@ namespace etl #if ETL_CPP11_SUPPORTED //***************************************************************************** - /// mem_type_var + /// mem_cast_var //***************************************************************************** template - class mem_type_var : public etl::mem_type::size, - etl::largest::alignment> + class mem_cast_types : public etl::mem_cast::size, + etl::largest::alignment> { }; #endif diff --git a/include/etl/generators/largest_generator.h b/include/etl/generators/largest_generator.h index 4f2512d1..0c3225a9 100644 --- a/include/etl/generators/largest_generator.h +++ b/include/etl/generators/largest_generator.h @@ -395,6 +395,14 @@ namespace etl using largest_t = typename largest::type; #endif +#if ETL_CPP17_SUPPORTED + template + inline constexpr size_t largest_size_v = largest::size; + + template + inline constexpr size_t largest_alignment_v = largest::alignment; +#endif + #else /*[[[cog import cog diff --git a/test/test_mem_cast.cpp b/test/test_mem_cast.cpp new file mode 100644 index 00000000..56c95841 --- /dev/null +++ b/test/test_mem_cast.cpp @@ -0,0 +1,141 @@ +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2021 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. +******************************************************************************/ + +#include "unit_test_framework.h" + +#include "etl/experimental/mem_cast.h" +#include "etl/largest.h" + +#include +#include +#include +#include + +namespace +{ + struct Data + { + char c; + double d; + std::array a; + }; + + constexpr size_t Size = etl::largest::size; + constexpr size_t Alignment = etl::largest::alignment; + + // Test variant types. + using MemCast = etl::mem_cast; + using MemCastTypes = etl::mem_cast_types; + + char c; + double d; + Data data; + + SUITE(test_mem_cast) + { + //************************************************************************* + TEST(test_size) + { + MemCast memCast; + MemCastTypes memCastTypes; + + CHECK(sizeof(char) <= MemCast::Size); + CHECK(sizeof(short) <= MemCast::Size); + CHECK(sizeof(Data) <= MemCast::Size); + + CHECK(sizeof(char) <= memCast.Size); + CHECK(sizeof(short) <= memCast.Size); + CHECK(sizeof(Data) <= memCast.Size); + + CHECK(sizeof(char) <= memCast.size()); + CHECK(sizeof(short) <= memCast.size()); + CHECK(sizeof(Data) <= memCast.size()); + + CHECK(sizeof(char) <= MemCastTypes::Size); + CHECK(sizeof(short) <= MemCastTypes::Size); + CHECK(sizeof(Data) <= MemCastTypes::Size); + + CHECK(sizeof(char) <= memCastTypes.Size); + CHECK(sizeof(short) <= memCastTypes.Size); + CHECK(sizeof(Data) <= memCastTypes.Size); + + CHECK(sizeof(char) <= memCastTypes.size()); + CHECK(sizeof(short) <= memCastTypes.size()); + CHECK(sizeof(Data) <= memCastTypes.size()); + } + + //************************************************************************* + TEST(test_alignment) + { + MemCast memCast; + MemCastTypes memCastTypes; + + CHECK(alignof(char) <= MemCast::Alignment); + CHECK(alignof(short) <= MemCast::Alignment); + CHECK(alignof(Data) <= MemCast::Alignment); + + CHECK(alignof(char) <= memCast.Alignment); + CHECK(alignof(short) <= memCast.Alignment); + CHECK(alignof(Data) <= memCast.Alignment); + + CHECK(alignof(char) <= memCast.alignment()); + CHECK(alignof(short) <= memCast.alignment()); + CHECK(alignof(Data) <= memCast.alignment()); + + CHECK(alignof(char) <= MemCastTypes::Alignment); + CHECK(alignof(short) <= MemCastTypes::Alignment); + CHECK(alignof(Data) <= MemCastTypes::Alignment); + + CHECK(alignof(char) <= memCastTypes.Alignment); + CHECK(alignof(short) <= memCastTypes.Alignment); + CHECK(alignof(Data) <= memCastTypes.Alignment); + + CHECK(alignof(char) <= memCastTypes.alignment()); + CHECK(alignof(short) <= memCastTypes.alignment()); + CHECK(alignof(Data) <= memCastTypes.alignment()); + } + + //************************************************************************* + TEST(test_mem_cast_to_type) + { + MemCast memCast; + + char* pbuffer = memCast.data(); + *pbuffer = 123; + CHECK_EQUAL(123, memCast.get()); + + *reinterpret_cast(pbuffer) = 1.23; + CHECK_EQUAL(1.23, memCast.get()); + + *reinterpret_cast(pbuffer) = { 123, 1.23, { 1, 2, 3 } }; + CHECK(123 == memCast.get().c); + CHECK(1.23 == memCast.get().d); + CHECK((std::array { 1, 2, 3 }) == memCast.get().a); + } + }; +} diff --git a/test/test_mem_type.cpp b/test/test_mem_type.cpp deleted file mode 100644 index 318a8822..00000000 --- a/test/test_mem_type.cpp +++ /dev/null @@ -1,117 +0,0 @@ -/****************************************************************************** -The MIT License(MIT) - -Embedded Template Library. -https://github.com/ETLCPP/etl -https://www.etlcpp.com - -Copyright(c) 2021 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. -******************************************************************************/ - -#include "unit_test_framework.h" - -#include "etl/experimental/mem_type.h" -#include "etl/largest.h" - -#include -#include -#include -#include - -namespace -{ - struct Data - { - char c; - short s; - std::array a; - }; - - constexpr size_t Size = etl::largest::size; - constexpr size_t Alignment = etl::largest::alignment; - - // Test variant types. - using MemType = etl::mem_type; - using MemTypeVar = etl::mem_type_var; - - SUITE(test_mem_type) - { - TEST(test_alignment) - { - MemType memType; - MemTypeVar memTypeVar; - - CHECK(alignof(char) <= MemType::Alignment); - CHECK(alignof(short) <= MemType::Alignment); - CHECK(alignof(Data) <= MemType::Alignment); - - CHECK(alignof(char) <= memType.Alignment); - CHECK(alignof(short) <= memType.Alignment); - CHECK(alignof(Data) <= memType.Alignment); - - CHECK(alignof(char) <= memType.alignment()); - CHECK(alignof(short) <= memType.alignment()); - CHECK(alignof(Data) <= memType.alignment()); - - CHECK(alignof(char) <= MemTypeVar::Alignment); - CHECK(alignof(short) <= MemTypeVar::Alignment); - CHECK(alignof(Data) <= MemTypeVar::Alignment); - - CHECK(alignof(char) <= memTypeVar.Alignment); - CHECK(alignof(short) <= memTypeVar.Alignment); - CHECK(alignof(Data) <= memTypeVar.Alignment); - - CHECK(alignof(char) <= memTypeVar.alignment()); - CHECK(alignof(short) <= memTypeVar.alignment()); - CHECK(alignof(Data) <= memTypeVar.alignment()); - } - - TEST(test_size) - { - MemTypeVar memType; - MemTypeVar memTypeVar; - - CHECK(sizeof(char) <= MemType::Size); - CHECK(sizeof(short) <= MemType::Size); - CHECK(sizeof(Data) <= MemType::Size); - - CHECK(sizeof(char) <= memType.Size); - CHECK(sizeof(short) <= memType.Size); - CHECK(sizeof(Data) <= memType.Size); - - CHECK(sizeof(char) <= memType.size()); - CHECK(sizeof(short) <= memType.size()); - CHECK(sizeof(Data) <= memType.size()); - - CHECK(sizeof(char) <= MemTypeVar::Size); - CHECK(sizeof(short) <= MemTypeVar::Size); - CHECK(sizeof(Data) <= MemTypeVar::Size); - - CHECK(sizeof(char) <= memTypeVar.Size); - CHECK(sizeof(short) <= memTypeVar.Size); - CHECK(sizeof(Data) <= memTypeVar.Size); - - CHECK(sizeof(char) <= memTypeVar.size()); - CHECK(sizeof(short) <= memTypeVar.size()); - CHECK(sizeof(Data) <= memTypeVar.size()); - } - }; -} diff --git a/test/vs2019/etl.vcxproj b/test/vs2019/etl.vcxproj index f794c3bb..81b374c5 100644 --- a/test/vs2019/etl.vcxproj +++ b/test/vs2019/etl.vcxproj @@ -1309,7 +1309,7 @@ - + @@ -4495,7 +4495,7 @@ - + diff --git a/test/vs2019/etl.vcxproj.filters b/test/vs2019/etl.vcxproj.filters index 820f36d5..3c1d4a64 100644 --- a/test/vs2019/etl.vcxproj.filters +++ b/test/vs2019/etl.vcxproj.filters @@ -1110,7 +1110,7 @@ ETL\Private - + ETL\Experimental @@ -2534,7 +2534,7 @@ Source Files - + Source Files