31#ifndef ETL_ALIGNMENT_INCLUDED
32#define ETL_ALIGNMENT_INCLUDED
37#include "static_assert.h"
56 alignment_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
57 :
exception(reason_, file_name_, line_number_)
65 class alignment_error :
public alignment_exception
69 alignment_error(string_type file_name_, numeric_type line_number_)
70 : alignment_exception(ETL_ERROR_TEXT(
"alignment:error", ETL_ALIGNMENT_FILE_ID
"A"), file_name_, line_number_)
78 class typed_storage_error :
public alignment_exception
82 typed_storage_error(string_type file_name_, numeric_type line_number_)
83 : alignment_exception(ETL_ERROR_TEXT(
"typed_storage:error", ETL_ALIGNMENT_FILE_ID
"B"), file_name_, line_number_)
91 inline bool is_aligned(
const void* p,
size_t required_alignment)
93 uintptr_t address =
reinterpret_cast<uintptr_t
>(p);
94 return (address % required_alignment) == 0U;
100 template <
size_t Alignment>
103 uintptr_t address =
reinterpret_cast<uintptr_t
>(p);
104 return (address % Alignment) == 0U;
110 template <
typename T>
116 namespace private_alignment
122 template <
bool Is_Match,
size_t Alignment,
typename... TRest>
123 class type_with_alignment_matcher;
126 template <
size_t Alignment,
typename T1,
typename... TRest>
127 class type_with_alignment_matcher<true, Alignment, T1, TRest...>
135 template <
size_t Alignment,
typename T1,
typename T2,
typename... TRest>
140 typedef typename type_with_alignment_matcher< Alignment <= etl::alignment_of<T2>::value, Alignment, T2, TRest... >::type type;
144 template <
size_t Alignment,
typename T1>
155 template <
size_t Alignment,
typename T1,
typename... T>
160 typedef typename type_with_alignment_matcher<Alignment <= etl::alignment_of<T1>::value, Alignment, T1, T...>::type type;
166 template <
bool Is_Match,
const size_t Alignment,
typename T1 = void,
typename T2 = void,
typename T3 = void,
typename T4 = void,
167 typename T5 = void,
typename T6 = void,
typename T7 = void,
typename T8 =
void>
171 template <
size_t Alignment,
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6,
typename T7,
typename T8>
180 template <
size_t Alignment,
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6,
typename T7,
typename T8>
190 template <
size_t Alignment>
201 template <
size_t Alignment,
typename T1,
typename T2 = void,
typename T3 = void,
typename T4 = void,
typename T5 = void,
typename T6 = void,
202 typename T7 = void,
typename T8 =
void>
216 template <
size_t Alignment>
224 alignas(Alignment)
char dummy;
227 #if ETL_NOT_USING_64BIT_TYPES
228 typedef typename private_alignment::type_with_alignment_helper< Alignment, int_least8_t, int_least16_t, int32_t, float, double, void*>::type type;
231 typename private_alignment::type_with_alignment_helper< Alignment, int_least8_t, int_least16_t, int32_t, int64_t, float, double, void*>::type
236 ETL_STATIC_ASSERT(etl::alignment_of<type>::value == Alignment,
"Unable to create the type with the specified alignment");
240 template <
size_t Alignment>
241 using type_with_alignment_t =
typename type_with_alignment<Alignment>::type;
249 template <
size_t Length, const
size_t Alignment>
255 template <
typename T>
258 ETL_STATIC_ASSERT((etl::is_same<T*, void*>::value || ((Alignment % etl::alignment_of<T>::value) == 0)),
"Incompatible alignment");
264 template <
typename T>
265 operator const T&()
const
267 ETL_STATIC_ASSERT((etl::is_same<T*, void*>::value || ((Alignment % etl::alignment_of<T>::value) == 0)),
"Incompatible alignment");
273 template <
typename T>
276 ETL_STATIC_ASSERT((etl::is_same<T*, void*>::value || ((Alignment % etl::alignment_of<T>::value) == 0)),
"Incompatible alignment");
277 return reinterpret_cast<T*
>(data);
281 template <
typename T>
282 operator const T*()
const
284 ETL_STATIC_ASSERT((etl::is_same<T*, void*>::value || ((Alignment % etl::alignment_of<T>::value) == 0)),
"Incompatible alignment");
285 return reinterpret_cast<const T*
>(data);
289 template <
typename T>
292 ETL_STATIC_ASSERT((etl::is_same<T*, void*>::value || ((Alignment % etl::alignment_of<T>::value) == 0)),
"Incompatible alignment");
298 template <
typename T>
301 ETL_STATIC_ASSERT((etl::is_same<T*, void*>::value || ((Alignment % etl::alignment_of<T>::value) == 0)),
"Incompatible alignment");
307 template <
typename T>
310 ETL_STATIC_ASSERT((etl::is_same<T*, void*>::value || ((Alignment % etl::alignment_of<T>::value) == 0)),
"Incompatible alignment");
311 return reinterpret_cast<T*
>(data);
315 template <
typename T>
318 ETL_STATIC_ASSERT((etl::is_same<T*, void*>::value || ((Alignment % etl::alignment_of<T>::value) == 0)),
"Incompatible alignment");
319 return reinterpret_cast<const T*
>(data);
322#if ETL_USING_CPP11 && !defined(ETL_COMPILER_ARM5)
323 alignas(Alignment)
char data[Length];
328 typename etl::type_with_alignment<Alignment>::type etl_alignment_type;
336 template <
size_t Length, const
size_t Alignment>
344 template <
size_t Length,
typename T>
350 template <
size_t Length,
typename T>
351 using aligned_storage_as_t =
typename aligned_storage_as<Length, T>::type;
362 template <
typename T>
367 typedef T value_type;
368 typedef T& reference;
369 typedef const T& const_reference;
371 typedef const T* const_pointer;
376 ETL_CONSTEXPR typed_storage() ETL_NOEXCEPT
386 template <
typename... TArgs>
387 typed_storage(TArgs&&... args) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
390 create(etl::forward<TArgs>(args)...);
396 template <
typename T1>
406 template <
typename T1,
typename T2>
416 template <
typename T1,
typename T2,
typename T3>
426 template <
typename T1,
typename T2,
typename T3,
typename T4>
458 template <
typename... TArgs>
459 reference create(TArgs&&... args) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
472 template <
typename T1>
476 pointer p = ::new (&storage.value) value_type(t1);
486 template <
typename T1,
typename T2>
487 reference
create(
const T1& t1,
const T2& t2)
490 pointer p = ::new (&storage.value) value_type(t1, t2);
500 template <
typename T1,
typename T2,
typename T3>
501 reference
create(
const T1& t1,
const T2& t2,
const T3& t3)
504 pointer p = ::new (&storage.value) value_type(t1, t2, t3);
514 template <
typename T1,
typename T2,
typename T3,
typename T4>
515 reference
create(
const T1& t1,
const T2& t2,
const T3& t3,
const T4& t4)
518 pointer p = ::new (&storage.value) value_type(t1, t2, t3, t4);
539 pointer
operator->() ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
543 return &storage.value;
549 const_pointer
operator->() const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
553 return &storage.value;
559 reference
operator*() ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
567 const_reference
operator*() const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
584 ETL_CONSTEXPR union_type() ETL_NOEXCEPT
589 ~union_type() ETL_NOEXCEPT {}
606 template <
typename T>
611 typedef T value_type;
612 typedef T& reference;
613 typedef const T& const_reference;
615 typedef const T* const_pointer;
617 template <
typename U>
624 : pbuffer(reinterpret_cast<T*>(pbuffer_))
635 template <
typename... TArgs>
636 typed_storage_ext(
void* pbuffer_, TArgs&&... args) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
637 : pbuffer(reinterpret_cast<T*>(pbuffer_))
641 create(etl::forward<TArgs>(args)...);
648 typed_storage_ext(typed_storage_ext<T>&& other) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
649 : pbuffer(other.pbuffer)
652 other.pbuffer = ETL_NULLPTR;
659 template <
typename T1>
661 : pbuffer(reinterpret_cast<T*>(pbuffer_))
671 template <
typename T1,
typename T2>
673 : pbuffer(reinterpret_cast<T*>(pbuffer_))
683 template <
typename T1,
typename T2,
typename T3>
685 : pbuffer(reinterpret_cast<T*>(pbuffer_))
695 template <
typename T1,
typename T2,
typename T3,
typename T4>
697 : pbuffer(reinterpret_cast<T*>(pbuffer_))
729 template <
typename... TArgs>
730 reference create(TArgs&&... args) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
743 template <
typename T1>
747 pointer p = ::new (pbuffer) value_type(t1);
757 template <
typename T1,
typename T2>
758 reference
create(
const T1& t1,
const T2& t2)
761 pointer p = ::new (pbuffer) value_type(t1, t2);
771 template <
typename T1,
typename T2,
typename T3>
772 reference
create(
const T1& t1,
const T2& t2,
const T3& t3)
775 pointer p = ::new (pbuffer) value_type(t1, t2, t3);
785 template <
typename T1,
typename T2,
typename T3,
typename T4>
786 reference
create(
const T1& t1,
const T2& t2,
const T3& t3,
const T4& t4)
789 pointer p = ::new (pbuffer) value_type(t1, t2, t3, t4);
810 pointer
operator->() ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
820 const_pointer
operator->() const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
830 reference
operator*() ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
838 const_reference
operator*() const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
855 template <
typename T>
858 using ETL_OR_STD::swap;
860 swap(lhs.pbuffer, rhs.pbuffer);
861 swap(lhs.valid, rhs.valid);
void swap(etl::array_view< T > &lhs, etl::array_view< T > &rhs) ETL_NOEXCEPT
Swaps the values.
Definition array_view.h:692
Memory misalignment exception.
Definition alignment.h:66
Definition alignment.h:204
Definition alignment.h:168
Typed storage exception.
Definition alignment.h:79
Definition alignment.h:608
typed_storage_ext(void *pbuffer_) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
Constructor.
Definition alignment.h:623
typed_storage_ext(void *pbuffer_, const T1 &t1, const T2 &t2)
Constructs the instance of T with types T1, T2.
Definition alignment.h:672
const_reference operator*() const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
Definition alignment.h:838
reference create(const T1 &t1, const T2 &t2)
Definition alignment.h:758
pointer operator->() ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
Definition alignment.h:810
reference create(const T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4)
Definition alignment.h:786
reference create(const T1 &t1, const T2 &t2, const T3 &t3)
Definition alignment.h:772
typed_storage_ext(void *pbuffer_, const T1 &t1)
Constructs the instance of T with type T1.
Definition alignment.h:660
void destroy() ETL_NOEXCEPT
Calls the destructor of the stored object, if created.
Definition alignment.h:798
~typed_storage_ext() ETL_NOEXCEPT
Definition alignment.h:709
bool has_value() const ETL_NOEXCEPT
Definition alignment.h:718
typed_storage_ext(void *pbuffer_, const T1 &t1, const T2 &t2, const T3 &t3)
Constructs the instance of T with types T1, T2, T3.
Definition alignment.h:684
reference operator*() ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
Definition alignment.h:830
typed_storage_ext(void *pbuffer_, const T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4)
Constructs the instance of T with types T1, T2, T3, T4.
Definition alignment.h:696
reference create(const T1 &t1)
Definition alignment.h:744
const_pointer operator->() const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
Definition alignment.h:820
Definition alignment.h:364
reference create(const T1 &t1, const T2 &t2)
Definition alignment.h:487
reference create(const T1 &t1)
Definition alignment.h:473
reference operator*() ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
Definition alignment.h:559
void destroy() ETL_NOEXCEPT
Calls the destructor of the stored object, if created.
Definition alignment.h:527
bool has_value() const ETL_NOEXCEPT
Definition alignment.h:447
typed_storage(const T1 &t1)
Constructs the instance of T with type T1.
Definition alignment.h:397
const_reference operator*() const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
Definition alignment.h:567
~typed_storage() ETL_NOEXCEPT
Definition alignment.h:438
const_pointer operator->() const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
Definition alignment.h:549
reference create(const T1 &t1, const T2 &t2, const T3 &t3)
Definition alignment.h:501
pointer operator->() ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
Definition alignment.h:539
typed_storage(const T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4)
Constructs the instance of T with types T1, T2, T3, T4.
Definition alignment.h:427
typed_storage(const T1 &t1, const T2 &t2, const T3 &t3)
Constructs the instance of T with types T1, T2, T3.
Definition alignment.h:417
reference create(const T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4)
Definition alignment.h:515
typed_storage(const T1 &t1, const T2 &t2)
Constructs the instance of T with types T1, T2.
Definition alignment.h:407
Definition alignment.h:218
Definition alignment.h:251
Definition alignment.h:346
#define ETL_ASSERT(b, e)
Definition error_handler.h:511
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
Definition exception.h:59
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR14 void swap(etl::typed_storage_ext< T > &lhs, etl::typed_storage_ext< T > &rhs) ETL_NOEXCEPT
Swap two etl::typed_storage_ext.
Definition alignment.h:856
ETL_CONSTEXPR TContainer::pointer data(TContainer &container)
Definition iterator.h:1228
bool is_aligned(const void *p, size_t required_alignment)
Check that 'p' has 'required_alignment'.
Definition alignment.h:91
Definition alignment.h:253
const T * get_address() const
Get address as const T pointer.
Definition alignment.h:316
T * get_address()
Get address as T pointer.
Definition alignment.h:308
const T & get_reference() const
Get address as const T reference.
Definition alignment.h:299
T & get_reference()
Get address as T reference.
Definition alignment.h:290