mirror of
https://github.com/ETLCPP/etl.git
synced 2026-11-09 14:49:53 +08:00
Updated unaligned_type constructors and tests
This commit is contained in:
parent
3b54c4e7a2
commit
b28c485d1a
@ -299,7 +299,7 @@ namespace etl
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from an address and size.
|
||||
/// Construct from an address and buffer size.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14 unaligned_type(const void* address, size_t buffer_size)
|
||||
{
|
||||
@ -307,7 +307,7 @@ namespace etl
|
||||
|
||||
etl::copy_n(reinterpret_cast<const char*>(address), sizeof(T), this->storage);
|
||||
}
|
||||
|
||||
|
||||
//*************************************************************************
|
||||
/// Copy constructor
|
||||
//*************************************************************************
|
||||
@ -362,56 +362,6 @@ namespace etl
|
||||
return value;
|
||||
}
|
||||
|
||||
// For run time pointer
|
||||
static unaligned_type<T, Endian_>& at_address(void* address)
|
||||
{
|
||||
//auto p = (::new(address) unaligned_type<T, Endian_>());
|
||||
|
||||
//return *p;
|
||||
|
||||
return *reinterpret_cast<unaligned_type<T, Endian_>*>(address);
|
||||
}
|
||||
|
||||
// For const run time pointer
|
||||
static const unaligned_type<T, Endian_>& at_address(const void* address)
|
||||
{
|
||||
return *reinterpret_cast<const unaligned_type<T, Endian_>*>(address);
|
||||
}
|
||||
|
||||
// For run time pointer and size
|
||||
static unaligned_type<T, Endian_>& at_address(void* address, size_t buffer_size)
|
||||
{
|
||||
ETL_ASSERT(sizeof(T) <= buffer_size, ETL_ERROR(etl::unaligned_type_buffer_size));
|
||||
|
||||
return *reinterpret_cast<unaligned_type<T, Endian_>*>(address);
|
||||
}
|
||||
|
||||
// For const run time pointer and size
|
||||
static const unaligned_type<T, Endian_>& at_address(const void* address, size_t buffer_size)
|
||||
{
|
||||
ETL_ASSERT(sizeof(T) <= buffer_size, ETL_ERROR(etl::unaligned_type_buffer_size));
|
||||
|
||||
return *reinterpret_cast<const unaligned_type<T, Endian_>*>(address);
|
||||
}
|
||||
|
||||
// For run time pointer and compile time size
|
||||
template <size_t Size>
|
||||
static unaligned_type<T, Endian_>& at_address(void* address)
|
||||
{
|
||||
ETL_STATIC_ASSERT(sizeof(T) <= Size, "Buffer size to small for type");
|
||||
|
||||
return *reinterpret_cast<unaligned_type<T, Endian_>*>(address);
|
||||
}
|
||||
|
||||
// For const run time pointer and compile time size
|
||||
template <size_t Size>
|
||||
static unaligned_type<T, Endian_>& at_address(const void* address)
|
||||
{
|
||||
ETL_STATIC_ASSERT(sizeof(T) <= Size, "Buffer size to small for type");
|
||||
|
||||
return *reinterpret_cast<const unaligned_type<T, Endian_>*>(address);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Unaligned copy
|
||||
//*************************************************************************
|
||||
|
||||
@ -33,6 +33,8 @@ SOFTWARE.
|
||||
|
||||
#include "etl/private/diagnostic_useless_cast_push.h"
|
||||
|
||||
#include <array>
|
||||
|
||||
namespace
|
||||
{
|
||||
SUITE(test_unaligned_type)
|
||||
@ -149,6 +151,30 @@ namespace
|
||||
CHECK_EQUAL(3.1415927L, be_v3);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_construction_from_buffer)
|
||||
{
|
||||
const std::array<char, 4> buffer = { 0x12, 0x34, 0x56, 0x78 };
|
||||
|
||||
const uint32_t le_value = 0x78563412;
|
||||
const uint32_t be_value = 0x12345678;
|
||||
|
||||
etl::le_uint32_t le_v1(buffer.data());
|
||||
etl::be_uint32_t be_v1(buffer.data());
|
||||
|
||||
etl::le_uint32_t le_v2(buffer.data(), buffer.size());
|
||||
etl::be_uint32_t be_v2(buffer.data(), buffer.size());
|
||||
|
||||
CHECK_EQUAL(le_value, le_v1);
|
||||
CHECK_EQUAL(be_value, be_v1);
|
||||
|
||||
CHECK_EQUAL(le_value, le_v2);
|
||||
CHECK_EQUAL(be_value, be_v2);
|
||||
|
||||
CHECK_THROW(etl::le_uint32_t le_v3(buffer.data(), buffer.size() - 1), etl::unaligned_type_buffer_size);
|
||||
CHECK_THROW(etl::be_uint32_t be_v3(buffer.data(), buffer.size() - 1), etl::unaligned_type_buffer_size);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_endianness)
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user