Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
type_traits.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 
8 Copyright(c) 2014 jwellbelove
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files(the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions :
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 ******************************************************************************/
28 
29 #ifndef __ETL_TYPE_TRAITS__
30 #define __ETL_TYPE_TRAITS__
31 
32 #include <stddef.h>
33 
34 #include "nullptr.h"
35 
39 
40 namespace etl
41 {
44  template <typename T, const T VALUE>
46  {
47  static const T value = VALUE;
48 
49  typedef T value_type;
51 
52  operator value_type() const
53  {
54  return value;
55  }
56  };
57 
62 
65  template <typename T> struct remove_reference { typedef T type; };
66  template <typename T> struct remove_reference<T&> { typedef T type; };
67 
70  template <typename T> struct add_reference { typedef T& type; };
71  template <typename T> struct add_reference<T&> { typedef T& type; };
72 
75  template <typename T> struct remove_pointer { typedef T type; };
76  template <typename T> struct remove_pointer<T*> { typedef T type; };
77 
80  template <typename T> struct add_pointer { typedef typename remove_reference<T>::type* type; };
81 
84  template <typename T> struct is_const : false_type {};
85  template <typename T> struct is_const<const T> : true_type {};
86  template <typename T> struct is_const<const volatile T> : true_type {};
87 
90  template <typename T> struct remove_const { typedef T type; };
91  template <typename T> struct remove_const<const T> { typedef T type; };
92 
95  template <typename T> struct add_const { typedef const T type; };
96  template <typename T> struct add_const<const T> { typedef const T type; };
97 
100  template <typename T> struct is_volatile : false_type {};
101  template <typename T> struct is_volatile<volatile T> : true_type {};
102  template <typename T> struct is_volatile<const volatile T> : true_type {};
103 
106  template <typename T> struct remove_volatile { typedef T type; };
107  template <typename T> struct remove_volatile<volatile T> { typedef T type; };
108 
111  template <typename T> struct add_volatile { typedef volatile T type; };
112  template <typename T> struct add_volatile<volatile T> { typedef volatile T type; };
113 
116  template <typename T> struct remove_cv
117  {
118  typedef typename remove_volatile<typename remove_const<T>::type>::type type;
119  };
120 
123  template <typename T> struct add_cv
124  {
125  typedef typename add_volatile<typename add_const<T>::type>::type type;
126  };
127 
130  template <typename T> struct is_integral : false_type {};
131  template <> struct is_integral<bool> : true_type {};
132  template <> struct is_integral<char> : true_type {};
133  template <> struct is_integral<unsigned char> : true_type {};
134  template <> struct is_integral<signed char> : true_type {};
135  template <> struct is_integral<wchar_t> : true_type {};
136  template <> struct is_integral<short> : true_type {};
137  template <> struct is_integral<unsigned short> : true_type {};
138  template <> struct is_integral<int> : true_type {};
139  template <> struct is_integral<unsigned int> : true_type {};
140  template <> struct is_integral<long> : true_type {};
141  template <> struct is_integral<unsigned long> : true_type {};
142  template <> struct is_integral<long long> : true_type {};
143  template <> struct is_integral<unsigned long long> : true_type {};
144  template <typename T> struct is_integral<const T> : is_integral<T> {};
145  template <typename T> struct is_integral<volatile T> : is_integral<T> {};
146  template <typename T> struct is_integral<const volatile T> : is_integral<T> {};
147 
150  template <typename T> struct is_signed : false_type {};
151  template <> struct is_signed<char> : true_type {};
152 #ifdef PLATFORM_LINUX
153  template <> struct is_signed<wchar_t> : true_type {};
154 #endif
155  template <> struct is_signed<signed char> : true_type {};
156  template <> struct is_signed<short> : true_type {};
157  template <> struct is_signed<int> : true_type {};
158  template <> struct is_signed<long> : true_type {};
159  template <> struct is_signed<long long> : true_type {};
160  template <> struct is_signed<float> : true_type{};
161  template <> struct is_signed<double> : true_type{};
162  template <> struct is_signed<long double> : true_type{};
163  template <typename T> struct is_signed<const T> : is_signed<T> {};
164  template <typename T> struct is_signed<volatile T> : is_signed<T> {};
165  template <typename T> struct is_signed<const volatile T> : is_signed<T> {};
166 
169  template <typename T> struct is_unsigned : false_type {};
170  template <> struct is_unsigned<bool> : true_type {};
171  template <> struct is_unsigned<unsigned char> : true_type {};
172 #ifndef PLATFORM_LINUX
173  template <> struct is_unsigned<wchar_t> : true_type {};
174 #endif
175  template <> struct is_unsigned<unsigned short> : true_type {};
176  template <> struct is_unsigned<unsigned int> : true_type {};
177  template <> struct is_unsigned<unsigned long> : true_type {};
178  template <> struct is_unsigned<unsigned long long> : true_type {};
179  template <typename T> struct is_unsigned<const T> : is_unsigned<T> {};
180  template <typename T> struct is_unsigned<volatile T> : is_unsigned<T> {};
181  template <typename T> struct is_unsigned<const volatile T> : is_unsigned<T> {};
182 
185  template <typename T> struct is_floating_point : false_type {};
186  template <> struct is_floating_point<float> : true_type {};
187  template <> struct is_floating_point<double> : true_type {};
188  template <> struct is_floating_point<long double> : true_type {};
189  template <typename T> struct is_floating_point<const T> : is_floating_point<T> {};
190  template <typename T> struct is_floating_point<volatile T> : is_floating_point<T> {};
191  template <typename T> struct is_floating_point<const volatile T> : is_floating_point<T> {};
192 
195  template <typename T1, typename T2> struct is_same : public false_type {};
196  template <typename T> struct is_same<T, T> : public true_type {};
197 
200  template<typename T> struct is_void : false_type {};
201  template<> struct is_void<void> : true_type {};
202 
205  template<typename T> struct is_arithmetic : integral_constant<bool, is_integral<T>::value || is_floating_point<T>::value> {};
206 
209  template <typename T> struct is_fundamental : integral_constant<bool, is_arithmetic<T>::value ||
210  is_void<T>::value ||
211  is_same<nullptr_t,
212  typename remove_cv<T>::type>::value> {};
213 
216  template <typename T> struct is_compound : integral_constant<bool, !is_fundamental<T>::value> {};
217 
220  template <typename T> struct is_array : false_type {};
221  template <typename T> struct is_array<T[]> : true_type {};
222  template <typename T, size_t N> struct is_array<T[N]> : true_type {};
223 
226  template <typename T> struct is_pointer : false_type {};
227  template <typename T> struct is_pointer<T*> : true_type {};
228 
231  template <typename T> struct is_reference : false_type {};
232  template <typename T> struct is_reference<T&> : true_type {};
233 
236  template <typename T> struct make_signed { typedef T type; };
237  template <> struct make_signed<char> { typedef signed char type; };
238  template <> struct make_signed<unsigned char> { typedef signed char type; };
239 #ifdef COMPILER_MICROSOFT
240  template <> struct make_signed<wchar_t> { typedef short type; };
241 #endif
242  template <> struct make_signed<unsigned short> { typedef short type; };
243  template <> struct make_signed<unsigned int> { typedef int type; };
244  template <> struct make_signed<unsigned long> { typedef long type; };
245  template <> struct make_signed<unsigned long long> { typedef long long type; };
246  template <typename T> struct make_signed<const T> : add_const<typename make_signed<T>::type> {};
247  template <typename T> struct make_signed<volatile T> : add_volatile<typename make_signed<T>::type> {};
248  template <typename T> struct make_signed<const volatile T> : add_const<typename add_volatile<typename make_signed<T>::type>::type> {};
249 
252  template <typename T> struct make_unsigned { typedef T type; };
253  template <> struct make_unsigned<char> { typedef unsigned char type; };
254  template <> struct make_unsigned<signed char> { typedef unsigned char type; };
255  template <> struct make_unsigned<short> { typedef unsigned short type; };
256 #ifdef COMPILER_MICROSOFT
257  template <> struct make_unsigned<wchar_t> { typedef unsigned short type; };
258 #endif
259  template <> struct make_unsigned<int> { typedef unsigned int type; };
260  template <> struct make_unsigned<long> { typedef unsigned long type; };
261  template <> struct make_unsigned<long long> { typedef unsigned long long type; };
262  template <typename T> struct make_unsigned<const T> : add_const<typename make_unsigned<T>::type> {};
263  template <typename T> struct make_unsigned<volatile T> : add_volatile<typename make_unsigned<T>::type> {};
264  template <typename T> struct make_unsigned<const volatile T> : add_const<typename add_volatile<typename make_unsigned<T>::type>::type> {};
265 
268  template <bool B, typename T = void> struct enable_if {};
269  template <typename T> struct enable_if<true, T> { typedef T type; };
270 
273  template <bool B, typename T, typename F> struct conditional { typedef T type; };
274  template <typename T, typename F> struct conditional<false, T, F> { typedef F type; };
275 
278  template <typename T, size_t N = 0>
279  struct extent : integral_constant<size_t, 0> {};
280 
281  template <typename T>
282  struct extent<T[], 0> : integral_constant<size_t, 0> {};
283 
284  template <typename T, size_t N>
285  struct extent<T[], N> : integral_constant<size_t, extent<T, N - 1>::value> {};
286 
287  template <typename T, size_t N>
288  struct extent<T[N], 0> : integral_constant<size_t, N> {};
289 
290  template <typename T, size_t I, size_t N>
291  struct extent<T[I], N> : integral_constant<size_t, extent<T, N - 1>::value> {};
292 
295  template <typename T> struct remove_extent { typedef T type; };
296  template <typename T> struct remove_extent<T[]> { typedef T type; };
297  template <typename T, size_t N> struct remove_extent<T[N]> { typedef T type;};
298 
301  template <typename T> struct remove_all_extents { typedef T type;};
302  template <typename T> struct remove_all_extents<T[]> { typedef typename remove_all_extents<T>::type type; };
303  template <typename T, size_t N> struct remove_all_extents<T[N]> { typedef typename remove_all_extents<T>::type type; };
304 
307  template <typename T>struct rank : integral_constant<size_t, 0> {};
308  template <typename T> struct rank<T[]> : public integral_constant<size_t, rank<T>::value + 1> {};
309  template <typename T, size_t N> struct rank<T[N]> : public integral_constant<size_t, rank<T>::value + 1> {};
310 
314 #ifdef COMPILER_MICROSOFT
315  template <typename T> struct alignment_of : integral_constant<size_t, size_t(__alignof(T))> {};
316 #endif
317 
318 #ifdef COMPILER_GCC
319  template <typename T> struct alignment_of : integral_constant<size_t, size_t(__alignof__(T))> {};
320 #endif
321 
322 #ifdef COMPILER_KEIL
323  template <typename T> struct alignment_of : integral_constant<size_t, size_t(__alignof__(T))> {};
324 #endif
325 
326 #ifdef COMPILER_IAR
327  template <typename T> struct alignment_of : integral_constant<size_t, size_t(__ALIGNOF__(T))> {};
328 #endif
329 
332  template <> struct alignment_of<void> : integral_constant <size_t, 0>{};
333 }
334 
335 #endif
Definition: type_traits.h:216
Definition: type_traits.h:273
Definition: type_traits.h:84
Definition: type_traits.h:95
Definition: type_traits.h:130
Definition: type_traits.h:80
Definition: type_traits.h:268
Definition: type_traits.h:209
integral_constant< bool, false > false_type
Definition: type_traits.h:60
Definition: type_traits.h:169
Definition: type_traits.h:90
Definition: algorithm.h:43
Definition: type_traits.h:150
Definition: type_traits.h:236
Definition: type_traits.h:279
Definition: type_traits.h:70
Definition: type_traits.h:307
Definition: type_traits.h:200
Definition: type_traits.h:106
Definition: type_traits.h:195
Definition: type_traits.h:65
Definition: type_traits.h:226
Definition: type_traits.h:45
Definition: type_traits.h:220
Definition: type_traits.h:295
Definition: type_traits.h:205
Definition: type_traits.h:100
Definition: type_traits.h:252
Definition: type_traits.h:123
Definition: type_traits.h:116
Definition: type_traits.h:75
Definition: type_traits.h:111
Definition: type_traits.h:231
Definition: type_traits.h:301
Definition: type_traits.h:185