From 2b2a542aa1b73ec1b56fb9cfac5e586dd3aaf23b Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Thu, 3 Jun 2021 20:45:36 +0100 Subject: [PATCH] Added more static asserts for alignment --- include/etl/mem_cast.h | 56 ++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/include/etl/mem_cast.h b/include/etl/mem_cast.h index 4d05b9a6..1cc5ee0f 100644 --- a/include/etl/mem_cast.h +++ b/include/etl/mem_cast.h @@ -110,6 +110,8 @@ namespace etl static ETL_CONSTANT size_t Size = Size_; static ETL_CONSTANT size_t Alignment = Alignment_; + ETL_STATIC_ASSERT(etl::is_power_of_2::value, "Alignment must be a power of 2"); + //*********************************** /// Default constructor //*********************************** @@ -125,10 +127,26 @@ namespace etl 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"); + ETL_STATIC_ASSERT(Alignment >= Other_Alignment, "Other alignment is incompatible"); + ETL_STATIC_ASSERT(etl::is_power_of_2::value, "Other alignment must be a power of 2"); memcpy(buffer, other.buffer, Size_); } + + //*********************************** + /// Assignment operator + //*********************************** + template + 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 is incompatible"); + ETL_STATIC_ASSERT(etl::is_power_of_2::value, "Other alignment must be a power of 2"); + + memcpy(buffer, rhs.buffer, Size_); + + return *this; + } //*********************************** /// Assign from value @@ -286,20 +304,6 @@ namespace etl return *static_cast(p); } - //*********************************** - /// Assignment operator - //*********************************** - template - 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"); - - memcpy(buffer, rhs.buffer, Size_); - - return *this; - } - //*********************************** /// Get the size of the buffer //*********************************** @@ -374,6 +378,17 @@ namespace etl { } + //*********************************** + /// Assignment operator + //*********************************** + mem_cast_ptr& operator =(const mem_cast_ptr& rhs) + { + pbuffer = rhs.pbuffer; + buffer_size = rhs.buffer_size; + + return *this; + } + //*********************************** /// Assign from value //*********************************** @@ -540,17 +555,6 @@ namespace etl return *reinterpret_cast(p); } - //*********************************** - /// Assignment operator - //*********************************** - mem_cast_ptr& operator =(const mem_cast_ptr& rhs) - { - pbuffer = rhs.pbuffer; - buffer_size = rhs.buffer_size; - - return *this; - } - //*********************************** /// Get the size of the buffer //***********************************