Loading...
Searching...
No Matches
mem_cast.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2021 John Wellbelove
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_MEM_CAST_INCLUDED
#define ETL_MEM_CAST_INCLUDED
#include "platform.h"
#include "binary.h"
#include "error_handler.h"
#include "exception.h"
#include "file_error_numbers.h"
#include "largest.h"
#include "memory.h"
#include "placement_new.h"
#include "static_assert.h"
#include "utility.h"
#include <stdint.h>
#include <string.h>
namespace etl
{
//***************************************************************************
//***************************************************************************
class mem_cast_exception : public etl::exception
{
public:
mem_cast_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: exception(reason_, file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class mem_cast_size_exception : public etl::mem_cast_exception
{
public:
mem_cast_size_exception(string_type file_name_, numeric_type line_number_)
: mem_cast_exception(ETL_ERROR_TEXT("mem_cast:size", ETL_MEM_CAST_FILE_ID"A"), file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class mem_cast_nullptr_exception : public etl::mem_cast_exception
{
public:
mem_cast_nullptr_exception(string_type file_name_, numeric_type line_number_)
: mem_cast_exception(ETL_ERROR_TEXT("mem_cast:null pointer", ETL_MEM_CAST_FILE_ID"B"), file_name_, line_number_)
{
}
};
//*****************************************************************************
//*****************************************************************************
template <size_t Size_, size_t Alignment_>
class mem_cast
{
public:
static ETL_CONSTANT size_t Size = Size_;
static ETL_CONSTANT size_t Alignment = Alignment_;
ETL_STATIC_ASSERT((Alignment == 1) || etl::is_power_of_2<Alignment>::value, "Alignment must be a power of 2");
//***********************************
//***********************************
mem_cast()
: buffer()
{
}
//***********************************
//***********************************
template <size_t Other_Size, size_t Other_Alignment>
mem_cast(const mem_cast<Other_Size, Other_Alignment>& other)
{
ETL_STATIC_ASSERT(Size >= Other_Size, "Other size is too large");
memcpy(buffer, other.data(), Other_Size);
}
//***********************************
//***********************************
template <size_t Other_Size, size_t Other_Alignment>
mem_cast& operator=(const mem_cast<Other_Size, Other_Alignment>& rhs)
{
ETL_STATIC_ASSERT(Size >= Other_Size, "RHS size is too large");
memcpy(buffer, rhs.data(), Other_Size);
return *this;
}
//***********************************
//***********************************
template <typename T>
void assign(const T& value)
{
ETL_STATIC_ASSERT(Size >= sizeof(T), "Type size is too large");
::new (static_cast<void*>(buffer.raw)) T(value);
}
//***********************************
//***********************************
template <typename T>
void assign_at_offset(size_t offset, const T& value)
{
char* p = static_cast<char*>(buffer.raw) + offset;
ETL_ASSERT(sizeof(T) <= (Size - offset), ETL_ERROR(etl::mem_cast_size_exception));
::new (p) T(value);
}
//***********************************
//***********************************
template <typename T, size_t Offset>
void assign_at_offset(const T& value)
{
char* p = static_cast<char*>(buffer.raw) + Offset;
ETL_STATIC_ASSERT(sizeof(T) <= (Size - Offset), "Type size is too large");
::new (p) T(value);
}
#if ETL_USING_CPP11
//***********************************
//***********************************
template <typename T, typename... TArgs>
T& emplace(TArgs... args)
{
ETL_STATIC_ASSERT(Size >= sizeof(T), "Type size is too large");
::new (static_cast<void*>(buffer.raw)) T(etl::forward<TArgs>(args)...);
return ref<T>();
}
//***********************************
//***********************************
template <typename T, typename... TArgs>
T& emplace_at_offset(size_t offset, TArgs... args)
{
char* p = static_cast<char*>(buffer.raw) + offset;
ETL_ASSERT(sizeof(T) <= (Size - offset), ETL_ERROR(etl::mem_cast_size_exception));
::new (p) T(etl::forward<TArgs>(args)...);
return ref_at_offset<T>(offset);
}
//***********************************
//***********************************
template <typename T, size_t Offset, typename... TArgs>
T& emplace_at_offset(TArgs... args)
{
char* p = static_cast<char*>(buffer.raw) + Offset;
ETL_STATIC_ASSERT(sizeof(T) <= (Size - Offset), "Type size is too large");
::new (p) T(etl::forward<TArgs>(args)...);
return ref_at_offset<T, Offset>();
}
#endif
//***********************************
//***********************************
template <typename T>
ETL_NODISCARD
T& ref()
{
ETL_STATIC_ASSERT(sizeof(T) <= Size, "Size of type too large for storage");
return *reinterpret_cast<T*>(buffer.raw);
}
//***********************************
//***********************************
template <typename T>
ETL_NODISCARD
const T& ref() const
{
ETL_STATIC_ASSERT(sizeof(T) <= Size, "Size of type too large for storage");
return *reinterpret_cast<const T*>(buffer.raw);
}
//***********************************
//***********************************
template <typename T>
ETL_NODISCARD
T& ref_at_offset(size_t offset)
{
ETL_ASSERT(sizeof(T) <= (Size - offset), ETL_ERROR(etl::mem_cast_size_exception));
return *reinterpret_cast<T*>(buffer.raw + offset);
}
//***********************************
//***********************************
template <typename T>
ETL_NODISCARD
const T& ref_at_offset(size_t offset) const
{
ETL_ASSERT(sizeof(T) <= (Size - offset), ETL_ERROR(etl::mem_cast_size_exception));
return *reinterpret_cast<const T*>(buffer.raw + offset);
}
//***********************************
//***********************************
template <typename T, size_t Offset>
ETL_NODISCARD
T& ref_at_offset()
{
ETL_STATIC_ASSERT(sizeof(T) <= (Size - Offset), "Size of type too large for storage");
return *reinterpret_cast<T*>(buffer.raw + Offset);
}
//***********************************
//***********************************
template <typename T, size_t Offset>
ETL_NODISCARD
const T& ref_at_offset() const
{
ETL_STATIC_ASSERT(sizeof(T) <= (Size - Offset), "Size of type too large for storage");
return *reinterpret_cast<const T*>(buffer.raw + Offset);
}
//***********************************
//***********************************
ETL_NODISCARD
static ETL_CONSTEXPR size_t size()
{
return Size;
}
//***********************************
//***********************************
ETL_NODISCARD
static ETL_CONSTEXPR size_t alignment()
{
return Alignment;
}
//***********************************
//***********************************
ETL_NODISCARD
char* data()
{
return buffer;
}
//***********************************
//***********************************
ETL_NODISCARD
const char* data() const
{
return buffer;
}
private:
etl::uninitialized_buffer<Size, 1U, Alignment> buffer;
};
template <size_t Size_, size_t Alignment_>
ETL_CONSTANT size_t mem_cast<Size_, Alignment_>::Size;
template <size_t Size_, size_t Alignment_>
ETL_CONSTANT size_t mem_cast<Size_, Alignment_>::Alignment;
//*****************************************************************************
//*****************************************************************************
class mem_cast_ptr
{
public:
static ETL_CONSTANT size_t Undefined_Size = etl::integral_limits<size_t>::max;
//***********************************
//***********************************
mem_cast_ptr()
: pbuffer(ETL_NULLPTR)
, buffer_size(Undefined_Size)
{
}
//***********************************
//***********************************
mem_cast_ptr(char* pbuffer_, size_t buffer_size_ = Undefined_Size)
: pbuffer(pbuffer_)
, buffer_size(buffer_size_)
{
}
//***********************************
//***********************************
mem_cast_ptr(const mem_cast_ptr& other)
: pbuffer(other.pbuffer)
, buffer_size(other.buffer_size)
{
}
//***********************************
//***********************************
mem_cast_ptr& operator=(const mem_cast_ptr& rhs)
{
pbuffer = rhs.pbuffer;
buffer_size = rhs.buffer_size;
return *this;
}
//***********************************
//***********************************
template <typename T>
void assign(const T& value)
{
ETL_ASSERT((pbuffer != ETL_NULLPTR), ETL_ERROR(etl::mem_cast_nullptr_exception));
ETL_ASSERT(sizeof(T) <= buffer_size, ETL_ERROR(etl::mem_cast_size_exception));
::new (pbuffer) T(value);
}
//***********************************
//***********************************
template <typename T>
void assign_at_offset(size_t offset, const T& value)
{
ETL_ASSERT((pbuffer != ETL_NULLPTR), ETL_ERROR(etl::mem_cast_nullptr_exception));
char* p = pbuffer + offset;
ETL_ASSERT(sizeof(T) <= (buffer_size - offset), ETL_ERROR(etl::mem_cast_size_exception));
::new (p) T(value);
}
//***********************************
//***********************************
template <typename T, size_t Offset>
void assign_at_offset(const T& value)
{
ETL_ASSERT((pbuffer != ETL_NULLPTR), ETL_ERROR(etl::mem_cast_nullptr_exception));
char* p = pbuffer + Offset;
ETL_ASSERT(sizeof(T) <= (buffer_size - Offset), ETL_ERROR(etl::mem_cast_size_exception));
::new (p) T(value);
}
#if ETL_USING_CPP11
//***********************************
//***********************************
template <typename T, typename... TArgs>
T& emplace(TArgs... args)
{
ETL_ASSERT((pbuffer != ETL_NULLPTR), ETL_ERROR(etl::mem_cast_nullptr_exception));
ETL_ASSERT(sizeof(T) <= buffer_size, ETL_ERROR(etl::mem_cast_size_exception));
::new (pbuffer) T(etl::forward<TArgs>(args)...);
return ref<T>();
}
//***********************************
//***********************************
template <typename T, typename... TArgs>
T& emplace_at_offset(size_t offset, TArgs... args)
{
ETL_ASSERT((pbuffer != ETL_NULLPTR), ETL_ERROR(etl::mem_cast_nullptr_exception));
char* p = pbuffer + offset;
ETL_ASSERT(sizeof(T) <= (buffer_size - offset), ETL_ERROR(etl::mem_cast_size_exception));
::new (p) T(etl::forward<TArgs>(args)...);
return ref_at_offset<T>(offset);
}
//***********************************
//***********************************
template <typename T, size_t Offset, typename... TArgs>
T& emplace_at_offset(TArgs... args)
{
ETL_ASSERT((pbuffer != ETL_NULLPTR), ETL_ERROR(etl::mem_cast_nullptr_exception));
char* p = pbuffer + Offset;
ETL_ASSERT(sizeof(T) <= (buffer_size - Offset), ETL_ERROR(etl::mem_cast_size_exception));
::new (p) T(etl::forward<TArgs>(args)...);
return ref_at_offset<T, Offset>();
}
#endif
//***********************************
//***********************************
template <typename T>
ETL_NODISCARD
T& ref()
{
ETL_ASSERT((pbuffer != ETL_NULLPTR), ETL_ERROR(etl::mem_cast_nullptr_exception));
ETL_ASSERT(sizeof(T) <= buffer_size, ETL_ERROR(etl::mem_cast_size_exception));
T* p = reinterpret_cast<T*>(pbuffer);
return *p;
}
//***********************************
//***********************************
template <typename T>
ETL_NODISCARD
const T& ref() const
{
ETL_ASSERT((pbuffer != ETL_NULLPTR), ETL_ERROR(etl::mem_cast_nullptr_exception));
ETL_ASSERT(sizeof(T) <= buffer_size, ETL_ERROR(etl::mem_cast_size_exception));
const T* p = reinterpret_cast<const T*>(pbuffer);
return *p;
}
//***********************************
//***********************************
template <typename T>
ETL_NODISCARD
T& ref_at_offset(size_t offset)
{
ETL_ASSERT((pbuffer != ETL_NULLPTR), ETL_ERROR(etl::mem_cast_nullptr_exception));
ETL_ASSERT(sizeof(T) <= (buffer_size - offset), ETL_ERROR(etl::mem_cast_size_exception));
T* p = reinterpret_cast<T*>(pbuffer + offset);
return *p;
}
//***********************************
//***********************************
template <typename T>
ETL_NODISCARD
const T& ref_at_offset(size_t offset) const
{
ETL_ASSERT((pbuffer != ETL_NULLPTR), ETL_ERROR(etl::mem_cast_nullptr_exception));
ETL_ASSERT(sizeof(T) <= (buffer_size - offset), ETL_ERROR(etl::mem_cast_size_exception));
const T* p = reinterpret_cast<const T*>(pbuffer + offset);
return *p;
}
//***********************************
//***********************************
template <typename T, size_t Offset>
ETL_NODISCARD
T& ref_at_offset()
{
ETL_ASSERT((pbuffer != ETL_NULLPTR), ETL_ERROR(etl::mem_cast_nullptr_exception));
ETL_ASSERT(sizeof(T) <= (buffer_size - Offset), ETL_ERROR(etl::mem_cast_size_exception));
T* p = reinterpret_cast<T*>(pbuffer + Offset);
return *p;
}
//***********************************
//***********************************
template <typename T, size_t Offset>
ETL_NODISCARD
const T& ref_at_offset() const
{
ETL_ASSERT((pbuffer != ETL_NULLPTR), ETL_ERROR(etl::mem_cast_nullptr_exception));
ETL_ASSERT(sizeof(T) <= (buffer_size - Offset), ETL_ERROR(etl::mem_cast_size_exception));
const T* p = reinterpret_cast<const T*>(pbuffer + Offset);
return *p;
}
//***********************************
//***********************************
ETL_NODISCARD
size_t size() const
{
return buffer_size;
}
//***********************************
//***********************************
ETL_NODISCARD
size_t alignment() const
{
typedef etl::smallest_uint_for_bits<sizeof(uintptr_t) * CHAR_BIT>::type type;
const type p = reinterpret_cast<type>(pbuffer);
return size_t(1U) << etl::count_trailing_zeros(p);
}
//***********************************
//***********************************
void data(char* pbuffer_, size_t buffer_size_ = Undefined_Size)
{
pbuffer = pbuffer_;
buffer_size = buffer_size_;
}
//***********************************
//***********************************
ETL_NODISCARD
char* data()
{
return pbuffer;
}
//***********************************
//***********************************
ETL_NODISCARD
const char* data() const
{
return pbuffer;
}
private:
char* pbuffer;
size_t buffer_size;
};
//*****************************************************************************
//*****************************************************************************
#if ETL_USING_CPP11 && !defined(ETL_MEM_CAST_FORCE_CPP03_IMPLEMENTATION)
template <typename... TTypes>
using mem_cast_types = etl::mem_cast<etl::largest<TTypes...>::size, etl::largest<TTypes...>::alignment>;
#else
template <typename T1, typename T2 = char, typename T3 = char, typename T4 = char, typename T5 = char, typename T6 = char, typename T7 = char,
typename T8 = char, typename T9 = char, typename T10 = char, typename T11 = char, typename T12 = char, typename T13 = char,
typename T14 = char, typename T15 = char, typename T16 = char>
struct mem_cast_types
: public etl::mem_cast< etl::largest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>::size,
etl::largest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>::alignment>
{
};
#endif
} // namespace etl
#endif
binary.h
etl::mem_cast_exception
The base class for mem_cast exceptions.
Definition mem_cast.h:54
etl::mem_cast_nullptr_exception
The exception thrown when the pointer is null.
Definition mem_cast.h:80
etl::mem_cast_ptr::ref_at_offset
ETL_NODISCARD T & ref_at_offset()
Get a reference to T at offset (static).
Definition mem_cast.h:528
etl::mem_cast_ptr::size
ETL_NODISCARD size_t size() const
Get the size of the buffer.
Definition mem_cast.h:557
etl::mem_cast_ptr::data
ETL_NODISCARD char * data()
Get a pointer to the external buffer.
Definition mem_cast.h:588
etl::mem_cast_ptr::data
ETL_NODISCARD const char * data() const
Get const a pointer to the external buffer.
Definition mem_cast.h:597
etl::mem_cast_ptr::assign_at_offset
void assign_at_offset(size_t offset, const T &value)
Assign from value at offset.
Definition mem_cast.h:395
etl::mem_cast_ptr::ref_at_offset
ETL_NODISCARD const T & ref_at_offset(size_t offset) const
Get a const reference to T at offset (dynamic).
Definition mem_cast.h:513
etl::mem_cast_ptr::ref_at_offset
ETL_NODISCARD const T & ref_at_offset() const
Get a const reference to T at offset (static).
Definition mem_cast.h:543
etl::mem_cast_ptr::ref_at_offset
ETL_NODISCARD T & ref_at_offset(size_t offset)
Get a reference to T at offset (dynamic).
Definition mem_cast.h:498
etl::mem_cast_ptr::ref
ETL_NODISCARD const T & ref() const
Get a const reference to T.
Definition mem_cast.h:483
etl::mem_cast_ptr::operator=
mem_cast_ptr & operator=(const mem_cast_ptr &rhs)
Assignment operator.
Definition mem_cast.h:371
etl::mem_cast_ptr::ref
ETL_NODISCARD T & ref()
Get a reference to T.
Definition mem_cast.h:468
etl::mem_cast_ptr::mem_cast_ptr
mem_cast_ptr(const mem_cast_ptr &other)
Copy construct.
Definition mem_cast.h:362
etl::mem_cast_ptr::mem_cast_ptr
mem_cast_ptr(char *pbuffer_, size_t buffer_size_=Undefined_Size)
Construct with pointer to buffer and optional size.
Definition mem_cast.h:353
etl::mem_cast_ptr::assign
void assign(const T &value)
Assign from value.
Definition mem_cast.h:383
etl::mem_cast_ptr::alignment
ETL_NODISCARD size_t alignment() const
Get the alignment of the buffer.
Definition mem_cast.h:566
etl::mem_cast_ptr::data
void data(char *pbuffer_, size_t buffer_size_=Undefined_Size)
Set the pointer to the external buffer.
Definition mem_cast.h:578
etl::mem_cast_ptr::assign_at_offset
void assign_at_offset(const T &value)
Assign from value at offset.
Definition mem_cast.h:408
etl::mem_cast_ptr::mem_cast_ptr
mem_cast_ptr()
Default constructor.
Definition mem_cast.h:344
etl::mem_cast_size_exception
The exception thrown when the type size is too large.
Definition mem_cast.h:67
etl::mem_cast
mem_cast
Definition mem_cast.h:94
etl::mem_cast::assign_at_offset
void assign_at_offset(size_t offset, const T &value)
Assign from value at offset.
Definition mem_cast.h:149
etl::mem_cast::mem_cast
mem_cast(const mem_cast< Other_Size, Other_Alignment > &other)
Copy constructor.
Definition mem_cast.h:114
etl::mem_cast::assign_at_offset
void assign_at_offset(const T &value)
Assign from value at offset.
Definition mem_cast.h:161
etl::mem_cast::mem_cast
mem_cast()
Default constructor.
Definition mem_cast.h:105
etl::mem_cast::ref_at_offset
ETL_NODISCARD const T & ref_at_offset(size_t offset) const
Get a const reference to T at offset (dynamic).
Definition mem_cast.h:253
etl::mem_cast::assign
void assign(const T &value)
Assign from value.
Definition mem_cast.h:138
etl::mem_cast::data
ETL_NODISCARD char * data()
Get a pointer to the internal buffer.
Definition mem_cast.h:306
etl::mem_cast::ref_at_offset
ETL_NODISCARD T & ref_at_offset(size_t offset)
Get a reference to T at offset (dynamic).
Definition mem_cast.h:241
etl::mem_cast::ref
ETL_NODISCARD T & ref()
Get a reference to T.
Definition mem_cast.h:217
etl::mem_cast::alignment
static ETL_NODISCARD ETL_CONSTEXPR size_t alignment()
Get the alignment of the buffer.
Definition mem_cast.h:297
etl::mem_cast::ref_at_offset
ETL_NODISCARD T & ref_at_offset()
Get a reference to T at offset (static).
Definition mem_cast.h:265
etl::mem_cast::ref_at_offset
ETL_NODISCARD const T & ref_at_offset() const
Get a const reference to T at offset (static).
Definition mem_cast.h:277
etl::mem_cast::data
ETL_NODISCARD const char * data() const
Get a const pointer to the internal buffer.
Definition mem_cast.h:315
etl::mem_cast::size
static ETL_NODISCARD ETL_CONSTEXPR size_t size()
Get the size of the buffer.
Definition mem_cast.h:288
etl::mem_cast::operator=
mem_cast & operator=(const mem_cast< Other_Size, Other_Alignment > &rhs)
Assignment operator.
Definition mem_cast.h:125
etl::mem_cast::ref
ETL_NODISCARD const T & ref() const
Get a const reference to T.
Definition mem_cast.h:229
error_handler.h
exception.h
file_error_numbers.h
etl::uninitialized_buffer
Definition memory.h:3087
etl::count_trailing_zeros
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_unsigned< T >::value &&(etl::integral_limits< T >::bits==16U), uint_least8_t >::type count_trailing_zeros(T value)
Definition binary.h:1133
ETL_ASSERT
#define ETL_ASSERT(b, e)
Definition error_handler.h:511
etl::exception::exception
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
etl::exception
Definition exception.h:59
etl::integral_limits
Definition integral_limits.h:517
etl::largest
Definition largest.h:45
etl::smallest_uint_for_bits
Definition smallest.h:205
largest.h
memory.h
etl
Definition absolute.h:40
placement_new.h
platform.h
string.h
etl::mem_cast_types
Definition mem_cast.h:623
etl::type
utility.h