Embedded Template Library  1.0
alignment.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 
10 Copyright(c) 2014 jwellbelove
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files(the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions :
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29 ******************************************************************************/
30 
31 #ifndef ETL_ALIGNEMENT_INCLUDED
32 #define ETL_ALIGNEMENT_INCLUDED
33 
34 #include <stdint.h>
35 
36 #include "platform.h"
37 #include "type_traits.h"
38 #include "static_assert.h"
39 
43 
44 namespace etl
45 {
46  namespace private_alignment
47  {
48  //***************************************************************************
49  // Matcher.
50  //***************************************************************************
51  template <const bool IS_MATCH, const size_t ALIGNMENT, typename T1 = void, typename T2 = void, typename T3 = void, typename T4 = void, typename T5 = void, typename T6 = void, typename T7 = void, typename T8 = void>
53 
54  // Matching alignment.
55  template <const size_t ALIGNMENT, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
56  class type_with_alignment_matcher <true, ALIGNMENT, T1, T2, T3, T4, T5, T6, T7, T8>
57  {
58  public:
59 
60  typedef T1 type;
61  };
62 
63  // Non-matching alignment.
64  template <const size_t ALIGNMENT, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
65  class type_with_alignment_matcher <false, ALIGNMENT, T1, T2, T3, T4, T5, T6, T7, T8>
66  {
67  public:
68 
69  typedef typename type_with_alignment_matcher<ALIGNMENT <= etl::alignment_of<T2>::value, ALIGNMENT, T2, T3, T4, T5, T6, T7, T8, void>::type type;
70  };
71 
72  // Non-matching alignment, none left.
73  template <const size_t ALIGNMENT>
74  class type_with_alignment_matcher <false, ALIGNMENT, void, void, void, void, void, void, void, void>
75  {
76  public:
77 
78  typedef char type;
79  };
80 
81  //***************************************************************************
82  // Helper.
83  //***************************************************************************
84  template <const size_t ALIGNMENT, typename T1, typename T2 = void, typename T3 = void, typename T4 = void, typename T5 = void, typename T6 = void, typename T7 = void, typename T8 = void>
86  {
87  public:
88 
89  typedef typename type_with_alignment_matcher<ALIGNMENT <= etl::alignment_of<T1>::value, ALIGNMENT, T1, T2, T3, T4, T5, T6, T7, T8>::type type;
90  };
91  }
92 
93  //***************************************************************************
96  //***************************************************************************
97  template <const size_t ALIGNMENT>
99  {
100  public:
101 
102 #if ETL_NOT_USING_64BIT_TYPES
104 #else
106 #endif
107  };
108 
109  //***************************************************************************
113  //***************************************************************************
114  template <const size_t LENGTH, const size_t ALIGNMENT>
116  {
117  struct type
118  {
120  template <typename T>
121  operator T& ()
122  {
123  ETL_STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((ALIGNMENT % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
124  T* t = *this;
125  return *t;
126  }
127 
129  template <typename T>
130  operator const T& () const
131  {
132  ETL_STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((ALIGNMENT % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
133  const T* t = *this;
134  return *t;
135  }
136 
138  template <typename T>
139  operator T* ()
140  {
141  ETL_STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((ALIGNMENT % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
142  return reinterpret_cast<T*>(data);
143  }
144 
146  template <typename T>
147  operator const T* () const
148  {
149  ETL_STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((ALIGNMENT % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
150  return reinterpret_cast<const T*>(data);
151  }
152 
154  template <typename T>
156  {
157  ETL_STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((ALIGNMENT % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
158  T* t = *this;
159  return *t;
160  }
161 
163  template <typename T>
164  const T& get_reference() const
165  {
166  ETL_STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((ALIGNMENT % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
167  const T* t = *this;
168  return *t;
169  }
170 
172  template <typename T>
174  {
175  ETL_STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((ALIGNMENT % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
176  return reinterpret_cast<T*>(data);
177  }
178 
180  template <typename T>
181  const T* get_address() const
182  {
183  ETL_STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((ALIGNMENT % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
184  return reinterpret_cast<const T*>(data);
185  }
186 
187 #if ETL_CPP11_SUPPORTED && !defined(ETL_COMPILER_ARM5)
188  alignas(ALIGNMENT) char data[LENGTH];
189 #else
190  union
191  {
192  char data[LENGTH];
193  typename etl::type_with_alignment<ALIGNMENT>::type etl_alignment_type; // A POD type that has the same alignment as ALIGNMENT.
194  };
195 #endif
196  };
197  };
198 
199 #if ETL_CPP14_SUPPORTED
200  template <const size_t LENGTH, const size_t ALIGNMENT>
201  using aligned_storage_t = typename aligned_storage<LENGTH, ALIGNMENT>::type;
202 #endif
203 
204  //***************************************************************************
207  //***************************************************************************
208  template <size_t LENGTH, typename T>
209  struct aligned_storage_as : public etl::aligned_storage<LENGTH, etl::alignment_of<T>::value>
210  {
211  };
212 
213 #if ETL_CPP14_SUPPORTED
214  template <size_t LENGTH, typename T>
215  using aligned_storage_as_t = typename aligned_storage_as<LENGTH, T>::type;
216 #endif
217 }
218 
219 #endif
Definition: alignment.h:99
Definition: alignment.h:116
Definition: alignment.h:210
add_rvalue_reference
Definition: type_traits_generator.h:1348
is_same
Definition: type_traits_generator.h:981
Definition: absolute.h:37
Definition: alignment.h:118
const T & get_reference() const
Get address as const T reference.
Definition: alignment.h:164
T & get_reference()
Get address as T reference.
Definition: alignment.h:155
const T * get_address() const
Get address as const T pointer.
Definition: alignment.h:181
T * get_address()
Get address as T pointer.
Definition: alignment.h:173