mirror of
https://github.com/ETLCPP/etl.git
synced 2026-11-09 14:49:53 +08:00
Renamed mem_type to mem_cast
This commit is contained in:
parent
a5f6d9f033
commit
abf8d7bb14
@ -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 <stdint.h>
|
||||
|
||||
@ -41,10 +41,10 @@ SOFTWARE.
|
||||
namespace etl
|
||||
{
|
||||
//*****************************************************************************
|
||||
/// mem_type
|
||||
/// mem_cast
|
||||
//*****************************************************************************
|
||||
template <size_t Size_, size_t Alignment_>
|
||||
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 <size_t Other_Size, size_t Other_Alignment>
|
||||
ETL_CONSTEXPR mem_type(const mem_type<Other_Size, Other_Alignment>& other)
|
||||
ETL_CONSTEXPR mem_cast(const mem_cast<Other_Size, Other_Alignment>& 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<T>::value, "Alignment of T is incompatible");
|
||||
|
||||
return *reinterpret_cast<T*>(buffer);
|
||||
return *static_cast<T*>(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<T>::value, "Alignment of T is incompatible");
|
||||
|
||||
return *reinterpret_cast<T*>(buffer);
|
||||
return *static_cast<T*>(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<T>::value, "Alignment of T is incompatible");
|
||||
|
||||
return *reinterpret_cast<T*>(buffer);
|
||||
return *static_cast<T*>(buffer);
|
||||
}
|
||||
|
||||
//***********************************
|
||||
template <size_t Other_Size, size_t Other_Alignment>
|
||||
ETL_CONSTEXPR mem_type& operator =(const mem_type<Other_Size, Other_Alignment>& rhs)
|
||||
ETL_CONSTEXPR mem_cast& operator =(const mem_cast<Other_Size, Other_Alignment>& 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<Size, Alignment> buffer;
|
||||
etl::uninitialized_buffer<Size, 1U, Alignment> buffer;
|
||||
};
|
||||
|
||||
//*****************************************************************************
|
||||
/// mem_type_ptr
|
||||
/// mem_cast_ptr
|
||||
//*****************************************************************************
|
||||
template <size_t Size_>
|
||||
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 <size_t Other_Size>
|
||||
ETL_CONSTEXPR mem_type_ptr(const mem_type_ptr<Other_Size>& other, char* pbuffer_)
|
||||
ETL_CONSTEXPR mem_cast_ptr(const mem_cast_ptr<Other_Size>& other, char* pbuffer_)
|
||||
: pbuffer(pbuffer_)
|
||||
{
|
||||
ETL_STATIC_ASSERT(Size >= Other_Size, "Other size is too large");
|
||||
@ -198,7 +204,7 @@ namespace etl
|
||||
|
||||
//***********************************
|
||||
template <size_t Other_Size>
|
||||
mem_type_ptr& operator =(const mem_type_ptr<Other_Size>& rhs)
|
||||
mem_cast_ptr& operator =(const mem_cast_ptr<Other_Size>& 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 <typename... TTypes>
|
||||
class mem_type_var : public etl::mem_type<etl::largest<TTypes...>::size,
|
||||
etl::largest<TTypes...>::alignment>
|
||||
class mem_cast_types : public etl::mem_cast<etl::largest<TTypes...>::size,
|
||||
etl::largest<TTypes...>::alignment>
|
||||
{
|
||||
};
|
||||
#endif
|
||||
@ -395,6 +395,14 @@ namespace etl
|
||||
using largest_t = typename largest<T...>::type;
|
||||
#endif
|
||||
|
||||
#if ETL_CPP17_SUPPORTED
|
||||
template <typename... T>
|
||||
inline constexpr size_t largest_size_v = largest<T...>::size;
|
||||
|
||||
template <typename... T>
|
||||
inline constexpr size_t largest_alignment_v = largest<T...>::alignment;
|
||||
#endif
|
||||
|
||||
#else
|
||||
/*[[[cog
|
||||
import cog
|
||||
|
||||
141
test/test_mem_cast.cpp
Normal file
141
test/test_mem_cast.cpp
Normal file
@ -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 <array>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
struct Data
|
||||
{
|
||||
char c;
|
||||
double d;
|
||||
std::array<int, 3> a;
|
||||
};
|
||||
|
||||
constexpr size_t Size = etl::largest<char, double, Data>::size;
|
||||
constexpr size_t Alignment = etl::largest<char, double, Data>::alignment;
|
||||
|
||||
// Test variant types.
|
||||
using MemCast = etl::mem_cast<Size, Alignment>;
|
||||
using MemCastTypes = etl::mem_cast_types<char, double, Data>;
|
||||
|
||||
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<char>());
|
||||
|
||||
*reinterpret_cast<double*>(pbuffer) = 1.23;
|
||||
CHECK_EQUAL(1.23, memCast.get<double>());
|
||||
|
||||
*reinterpret_cast<Data*>(pbuffer) = { 123, 1.23, { 1, 2, 3 } };
|
||||
CHECK(123 == memCast.get<Data>().c);
|
||||
CHECK(1.23 == memCast.get<Data>().d);
|
||||
CHECK((std::array { 1, 2, 3 }) == memCast.get<Data>().a);
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -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 <array>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
struct Data
|
||||
{
|
||||
char c;
|
||||
short s;
|
||||
std::array<int, 10> a;
|
||||
};
|
||||
|
||||
constexpr size_t Size = etl::largest<char, double, Data>::size;
|
||||
constexpr size_t Alignment = etl::largest<char, double, Data>::alignment;
|
||||
|
||||
// Test variant types.
|
||||
using MemType = etl::mem_type<Size, Alignment>;
|
||||
using MemTypeVar = etl::mem_type_var<char, double, Data>;
|
||||
|
||||
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());
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -1309,7 +1309,7 @@
|
||||
<ClInclude Include="..\..\include\etl\cumulative_moving_average.h" />
|
||||
<ClInclude Include="..\..\include\etl\delegate.h" />
|
||||
<ClInclude Include="..\..\include\etl\delegate_service.h" />
|
||||
<ClInclude Include="..\..\include\etl\experimental\mem_type.h" />
|
||||
<ClInclude Include="..\..\include\etl\experimental\mem_cast.h" />
|
||||
<ClInclude Include="..\..\include\etl\file_error_numbers.h" />
|
||||
<ClInclude Include="..\..\include\etl\flags.h" />
|
||||
<ClInclude Include="..\..\include\etl\format_spec.h" />
|
||||
@ -4495,7 +4495,7 @@
|
||||
<ClCompile Include="..\test_list_shared_pool.cpp" />
|
||||
<ClCompile Include="..\test_make_string.cpp" />
|
||||
<ClCompile Include="..\test_mean.cpp" />
|
||||
<ClCompile Include="..\test_mem_type.cpp" />
|
||||
<ClCompile Include="..\test_mem_cast.cpp" />
|
||||
<ClCompile Include="..\test_message_packet.cpp" />
|
||||
<ClCompile Include="..\test_message_router_registry.cpp" />
|
||||
<ClCompile Include="..\test_multi_array.cpp" />
|
||||
|
||||
@ -1110,7 +1110,7 @@
|
||||
<ClInclude Include="..\..\include\etl\private\variant_new.h">
|
||||
<Filter>ETL\Private</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\etl\experimental\mem_type.h">
|
||||
<ClInclude Include="..\..\include\etl\experimental\mem_cast.h">
|
||||
<Filter>ETL\Experimental</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
@ -2534,7 +2534,7 @@
|
||||
<ClCompile Include="..\test_variant_new.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\test_mem_type.cpp">
|
||||
<ClCompile Include="..\test_mem_cast.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user