Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
type_traits.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 21.10.2013 21:22:36 CEST
3 // File: type_traits.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __TYPE_TRAITS_HEADER
12 #define __TYPE_TRAITS_HEADER
13 
14 namespace ustd {
15  //=================== ===================
17  template<typename T, T val>
19  static constexpr T value = val;
20  typedef T value_type;
21  //~ typedef integral_constant<T, val> type;
22  constexpr operator value_type() {
23  return value;
24  }
25  };
26  //=================== true_type ===================
29  //=================== conditional (meta if) ===================
30  template<bool cond, typename T, typename F> //default true
31  struct conditional {
32  typedef T type;
33  };
34  template<typename T, typename F>
35  struct conditional<false, T, F> {
36  typedef F type;
37  };
38  //=================== is_const ===================
39  template<typename T>
40  struct is_const {
41  enum{value = false};
42  };
43  template<typename T>
44  struct is_const<T const> {
45  enum{value = true};
46  };
47  //=================== remove_cv ===================
48  template<typename T>
49  struct remove_const {
50  typedef T type;
51  };
52  template<typename T>
53  struct remove_const<T const> {
54  typedef T type;
55  };
56  template<typename T>
57  struct remove_volatile {
58  typedef T type;
59  };
60  template<typename T>
61  struct remove_volatile<T volatile> {
62  typedef T type;
63  };
64  template<typename T>
65  struct remove_cv {
66  typedef typename remove_const<
68  >::type
70  };
71  //=================== is_unsigned ===================
72  template<typename T>
73  struct is_unsigned {
74  enum{value = (T(0) < T(-1))};
75  };
76  //=================== is_integral ===================
77  namespace detail {
78  template<typename T>
79  struct is_integral_helper {
80  enum{value = false};
81  };
82 
83  template<>
84  struct is_integral_helper<bool> {
85  enum{value = true};
86  };
87  template<>
88  struct is_integral_helper<char> {
89  enum{value = true};
90  };
91  template<>
92  struct is_integral_helper<char16_t> {
93  enum{value = true};
94  };
95  template<>
96  struct is_integral_helper<char32_t> {
97  enum{value = true};
98  };
99  template<>
100  struct is_integral_helper<wchar_t> {
101  enum{value = true};
102  };
103  template<>
104  struct is_integral_helper<signed char> {
105  enum{value = true};
106  };
107  template<>
108  struct is_integral_helper<short int> {
109  enum{value = true};
110  };
111  template<>
112  struct is_integral_helper<int> {
113  enum{value = true};
114  };
115  template<>
116  struct is_integral_helper<long int> {
117  enum{value = true};
118  };
119  template<>
120  struct is_integral_helper<long long int> {
121  enum{value = true};
122  };
123  template<>
124  struct is_integral_helper<unsigned char> {
125  enum{value = true};
126  };
127  template<>
128  struct is_integral_helper<unsigned short int> {
129  enum{value = true};
130  };
131  template<>
132  struct is_integral_helper<unsigned int> {
133  enum{value = true};
134  };
135  template<>
136  struct is_integral_helper<unsigned long int> {
137  enum{value = true};
138  };
139  template<>
140  struct is_integral_helper<unsigned long long int> {
141  enum{value = true};
142  };
143  }//end namespace detail
144  template<typename T>
145  struct is_integral {
146  enum {value = detail::is_integral_helper<typename remove_cv<T>::type>::value};
147  };
148  //=================== is_floating_point ===================
149  namespace detail {
150  template<typename T>
151  struct is_floating_point_helper {
152  enum{value = false};
153  };
154 
155  template<>
156  struct is_floating_point_helper<float> {
157  enum{value = true};
158  };
159  template<>
160  struct is_floating_point_helper<double> {
161  enum{value = true};
162  };
163  template<>
164  struct is_floating_point_helper<long double> {
165  enum{value = true};
166  };
167  }//end namespace detail
168  template<typename T>
170  enum {value = detail::is_floating_point_helper<typename remove_cv<T>::type>::value};
171  };
172  //=================== is_arithmetic ===================
173  template<typename T>
174  struct is_arithmetic {
176  };
177 
178  //=================== is_class ===================
179  template<typename T>
180  struct is_class {
181  template<typename U> static char check(void(U::*)(void));
182  template<typename U> static double check(...);
183 
184  enum { value = (sizeof(char) == sizeof(check<T>(0))) };
185  };
186  //=================== is_array ===================
187  template<typename>
188  struct is_array {
189  enum{value = false};
190  };
191  template<typename T, size_t N>
192  struct is_array<T[N]> {
193  enum{value = true};
194  };
195  template<typename T>
196  struct is_array<T[]> {
197  enum{value = true};
198  };
199  /*old but awesome implementation :-)
200  template<typename T>
201  struct is_array {
202  static T t;
203 
204  template<int N> struct wrap {typedef char type;};
205  template<typename U> static U * ptr(U u[]);
206  template<typename U> static U ptr(U u);
207 
208  template<typename U> static typename wrap<sizeof(t = ptr(t))>::type check(int);
209  template<typename U> static double check(...);
210 
211  enum{value = (sizeof(double) == sizeof(check<T>(int())))};
212  };
213  template<typename T> //remove constness before doing array checks since const i = j is also illegal
214  struct is_array<T const > {
215  enum{value = is_array<T>::value};
216  };*/
217  //=================== is_same ===================
218  template<typename T, typename U>
219  struct is_same {
220  enum {value = false};
221  };
222  template<typename T>
223  struct is_same<T, T> {
224  enum {value = true};
225  };
226  //=================== enable_if ===================
227  template<bool b, typename T>
228  struct enable_if {
229  };
230  template<typename T>
231  struct enable_if<true, T> {
232  typedef T type;
233  };
234  template<bool b, typename T> //not in std
235  struct disable_if {
236  };
237  template<typename T> //not in std
238  struct disable_if<false, T> {
239  typedef T type;
240  };
241 }//end namespace ustd
242 #endif //__TYPE_TRAITS_HEADER
Definition: type_traits.hpp:174
T type
Definition: type_traits.hpp:239
Definition: type_traits.hpp:228
integral_constant< bool, true > true_type
Definition: type_traits.hpp:27
Definition: type_traits.hpp:41
Definition: type_traits.hpp:74
Definition: type_traits.hpp:31
Definition: type_traits.hpp:65
T type
Definition: type_traits.hpp:62
T type
Definition: type_traits.hpp:58
T type
Definition: type_traits.hpp:50
T value_type
Definition: type_traits.hpp:20
T type
Definition: type_traits.hpp:232
remove_const< typename remove_volatile< T >::type >::type type
Definition: type_traits.hpp:69
F type
Definition: type_traits.hpp:36
Definition: type_traits.hpp:235
T type
Definition: type_traits.hpp:32
Definition: type_traits.hpp:188
static constexpr T value
Definition: type_traits.hpp:19
#define F(x)
Definition: ustd_generic.hpp:15
Definition: type_traits.hpp:49
integral_constant
Definition: type_traits.hpp:18
Definition: type_traits.hpp:169
Definition: type_traits.hpp:145
Definition: type_traits.hpp:57
Definition: type_traits.hpp:180
Definition: type_traits.hpp:73
Definition: type_traits.hpp:219
T type
Definition: type_traits.hpp:54
Definition: type_traits.hpp:40
integral_constant< bool, false > false_type
Definition: type_traits.hpp:28