]>
Commit | Line | Data |
---|---|---|
493bc460 PC |
1 | // TR1 type_traits -*- C++ -*- |
2 | ||
8fc81078 PC |
3 | // Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
4 | // Free Software Foundation, Inc. | |
493bc460 PC |
5 | // |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
748086b7 | 9 | // Free Software Foundation; either version 3, or (at your option) |
493bc460 PC |
10 | // any later version. |
11 | ||
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | ||
748086b7 JJ |
17 | // Under Section 7 of GPL version 3, you are granted additional |
18 | // permissions described in the GCC Runtime Library Exception, version | |
19 | // 3.1, as published by the Free Software Foundation. | |
493bc460 | 20 | |
748086b7 JJ |
21 | // You should have received a copy of the GNU General Public License and |
22 | // a copy of the GCC Runtime Library Exception along with this program; | |
23 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
24 | // <http://www.gnu.org/licenses/>. | |
909a9d44 | 25 | |
143c27b0 | 26 | /** @file tr1/type_traits |
493bc460 PC |
27 | * This is a TR1 C++ Library header. |
28 | */ | |
29 | ||
e133ace8 PC |
30 | #ifndef _GLIBCXX_TR1_TYPE_TRAITS |
31 | #define _GLIBCXX_TR1_TYPE_TRAITS 1 | |
493bc460 | 32 | |
05beb8e7 PC |
33 | #pragma GCC system_header |
34 | ||
e133ace8 PC |
35 | #if defined(_GLIBCXX_INCLUDE_AS_CXX0X) |
36 | # error TR1 header cannot be included from C++0x header | |
37 | #endif | |
38 | ||
8fc81078 | 39 | #include <bits/c++config.h> |
e133ace8 PC |
40 | |
41 | #if defined(_GLIBCXX_INCLUDE_AS_TR1) | |
42 | # include <tr1_impl/type_traits> | |
43 | #else | |
44 | # define _GLIBCXX_INCLUDE_AS_TR1 | |
45 | # define _GLIBCXX_BEGIN_NAMESPACE_TR1 namespace tr1 { | |
46 | # define _GLIBCXX_END_NAMESPACE_TR1 } | |
47 | # define _GLIBCXX_TR1 tr1:: | |
48 | # include <tr1_impl/type_traits> | |
49 | # undef _GLIBCXX_TR1 | |
50 | # undef _GLIBCXX_END_NAMESPACE_TR1 | |
51 | # undef _GLIBCXX_BEGIN_NAMESPACE_TR1 | |
52 | # undef _GLIBCXX_INCLUDE_AS_TR1 | |
53 | #endif | |
493bc460 | 54 | |
493bc460 PC |
55 | namespace std |
56 | { | |
e133ace8 PC |
57 | namespace tr1 |
58 | { | |
77d45108 PC |
59 | #define _DEFINE_SPEC(_Trait, _Type) \ |
60 | template<> \ | |
61 | struct _Trait<_Type> \ | |
3c279553 | 62 | : public true_type { }; |
51687431 | 63 | |
493bc460 | 64 | template<typename> |
4a27a739 | 65 | struct is_reference |
0f910b4f PC |
66 | : public false_type { }; |
67 | ||
4a27a739 PC |
68 | template<typename _Tp> |
69 | struct is_reference<_Tp&> | |
70 | : public true_type { }; | |
493bc460 | 71 | |
1933b74f PC |
72 | template<typename _Tp> |
73 | struct is_pod | |
6a7508ec | 74 | : public integral_constant<bool, __is_pod(_Tp) || is_void<_Tp>::value> |
1933b74f PC |
75 | { }; |
76 | ||
77 | template<typename _Tp> | |
78 | struct has_trivial_constructor | |
e133ace8 PC |
79 | : public integral_constant<bool, is_pod<_Tp>::value> |
80 | { }; | |
1933b74f | 81 | |
36651afe PC |
82 | template<typename _Tp> |
83 | struct has_trivial_copy | |
e133ace8 PC |
84 | : public integral_constant<bool, is_pod<_Tp>::value> |
85 | { }; | |
36651afe PC |
86 | |
87 | template<typename _Tp> | |
88 | struct has_trivial_assign | |
e133ace8 PC |
89 | : public integral_constant<bool, is_pod<_Tp>::value> |
90 | { }; | |
36651afe | 91 | |
1933b74f PC |
92 | template<typename _Tp> |
93 | struct has_trivial_destructor | |
e133ace8 PC |
94 | : public integral_constant<bool, is_pod<_Tp>::value> |
95 | { }; | |
1933b74f | 96 | |
5249b4b0 PC |
97 | template<typename _Tp> |
98 | struct has_nothrow_constructor | |
e133ace8 PC |
99 | : public integral_constant<bool, is_pod<_Tp>::value> |
100 | { }; | |
5249b4b0 | 101 | |
36651afe PC |
102 | template<typename _Tp> |
103 | struct has_nothrow_copy | |
e133ace8 | 104 | : public integral_constant<bool, is_pod<_Tp>::value> |
22931aa4 | 105 | { }; |
464b277b PC |
106 | |
107 | template<typename _Tp> | |
e133ace8 PC |
108 | struct has_nothrow_assign |
109 | : public integral_constant<bool, is_pod<_Tp>::value> | |
973cb10b | 110 | { }; |
493bc460 | 111 | |
4a27a739 | 112 | template<typename> |
77d45108 | 113 | struct __is_signed_helper |
4a27a739 | 114 | : public false_type { }; |
77d45108 PC |
115 | _DEFINE_SPEC(__is_signed_helper, signed char) |
116 | _DEFINE_SPEC(__is_signed_helper, short) | |
117 | _DEFINE_SPEC(__is_signed_helper, int) | |
118 | _DEFINE_SPEC(__is_signed_helper, long) | |
119 | _DEFINE_SPEC(__is_signed_helper, long long) | |
120 | ||
121 | template<typename _Tp> | |
122 | struct is_signed | |
123 | : public integral_constant<bool, (__is_signed_helper<typename | |
124 | remove_cv<_Tp>::type>::value)> | |
125 | { }; | |
4a27a739 PC |
126 | |
127 | template<typename> | |
77d45108 | 128 | struct __is_unsigned_helper |
4a27a739 | 129 | : public false_type { }; |
77d45108 PC |
130 | _DEFINE_SPEC(__is_unsigned_helper, unsigned char) |
131 | _DEFINE_SPEC(__is_unsigned_helper, unsigned short) | |
132 | _DEFINE_SPEC(__is_unsigned_helper, unsigned int) | |
133 | _DEFINE_SPEC(__is_unsigned_helper, unsigned long) | |
134 | _DEFINE_SPEC(__is_unsigned_helper, unsigned long long) | |
135 | ||
136 | template<typename _Tp> | |
137 | struct is_unsigned | |
138 | : public integral_constant<bool, (__is_unsigned_helper<typename | |
139 | remove_cv<_Tp>::type>::value)> | |
140 | { }; | |
4a27a739 | 141 | |
22931aa4 | 142 | template<typename _Base, typename _Derived> |
f4e4284d | 143 | struct __is_base_of_helper |
f4e4284d | 144 | { |
22931aa4 PC |
145 | typedef typename remove_cv<_Base>::type _NoCv_Base; |
146 | typedef typename remove_cv<_Derived>::type _NoCv_Derived; | |
147 | static const bool __value = (is_same<_Base, _Derived>::value | |
148 | || (__is_base_of(_Base, _Derived) | |
149 | && !is_same<_NoCv_Base, | |
150 | _NoCv_Derived>::value)); | |
f4e4284d | 151 | }; |
22931aa4 | 152 | |
f4e4284d PC |
153 | template<typename _Base, typename _Derived> |
154 | struct is_base_of | |
155 | : public integral_constant<bool, | |
156 | __is_base_of_helper<_Base, _Derived>::__value> | |
157 | { }; | |
158 | ||
516ebd44 PC |
159 | template<typename _From, typename _To> |
160 | struct __is_convertible_simple | |
161 | : public __sfinae_types | |
162 | { | |
163 | private: | |
164 | static __one __test(_To); | |
165 | static __two __test(...); | |
166 | static _From __makeFrom(); | |
167 | ||
168 | public: | |
169 | static const bool __value = sizeof(__test(__makeFrom())) == 1; | |
170 | }; | |
171 | ||
beb1b475 PC |
172 | template<typename _Tp> |
173 | struct add_reference; | |
174 | ||
c150a271 PC |
175 | template<typename _Tp> |
176 | struct __is_int_or_cref | |
177 | { | |
178 | typedef typename remove_reference<_Tp>::type __rr_Tp; | |
179 | static const bool __value = (is_integral<_Tp>::value | |
180 | || (is_integral<__rr_Tp>::value | |
181 | && is_const<__rr_Tp>::value | |
182 | && !is_volatile<__rr_Tp>::value)); | |
183 | }; | |
184 | ||
185 | template<typename _From, typename _To, | |
152d9676 PC |
186 | bool = (is_void<_From>::value || is_void<_To>::value |
187 | || is_function<_To>::value || is_array<_To>::value | |
939759fc | 188 | // This special case is here only to avoid warnings. |
c150a271 PC |
189 | || (is_floating_point<typename |
190 | remove_reference<_From>::type>::value | |
191 | && __is_int_or_cref<_To>::__value))> | |
192 | struct __is_convertible_helper | |
193 | { | |
194 | // "An imaginary lvalue of type From...". | |
516ebd44 | 195 | static const bool __value = (__is_convertible_simple<typename |
c150a271 PC |
196 | add_reference<_From>::type, _To>::__value); |
197 | }; | |
198 | ||
199 | template<typename _From, typename _To> | |
200 | struct __is_convertible_helper<_From, _To, true> | |
152d9676 PC |
201 | { static const bool __value = (is_void<_To>::value |
202 | || (__is_int_or_cref<_To>::__value | |
203 | && !is_void<_From>::value)); }; | |
c150a271 PC |
204 | |
205 | template<typename _From, typename _To> | |
206 | struct is_convertible | |
207 | : public integral_constant<bool, | |
208 | __is_convertible_helper<_From, _To>::__value> | |
209 | { }; | |
210 | ||
939759fc | 211 | // reference modifications [4.7.2]. |
4a27a739 PC |
212 | template<typename _Tp> |
213 | struct remove_reference | |
214 | { typedef _Tp type; }; | |
215 | ||
216 | template<typename _Tp> | |
217 | struct remove_reference<_Tp&> | |
218 | { typedef _Tp type; }; | |
219 | ||
220 | // NB: Careful with reference to void. | |
221 | template<typename _Tp, bool = (is_void<_Tp>::value | |
222 | || is_reference<_Tp>::value)> | |
223 | struct __add_reference_helper | |
224 | { typedef _Tp& type; }; | |
225 | ||
226 | template<typename _Tp> | |
227 | struct __add_reference_helper<_Tp, true> | |
228 | { typedef _Tp type; }; | |
229 | ||
230 | template<typename _Tp> | |
231 | struct add_reference | |
232 | : public __add_reference_helper<_Tp> | |
233 | { }; | |
234 | ||
939759fc | 235 | // other transformations [4.8]. |
fd735b6a PC |
236 | template<std::size_t _Len, std::size_t _Align> |
237 | struct aligned_storage | |
238 | { | |
239 | union type | |
240 | { | |
241 | unsigned char __data[_Len]; | |
242 | struct __attribute__((__aligned__((_Align)))) { } __align; | |
243 | }; | |
244 | }; | |
245 | ||
e2f5c678 | 246 | #undef _DEFINE_SPEC |
e133ace8 | 247 | } |
493bc460 PC |
248 | } |
249 | ||
e133ace8 | 250 | #endif // _GLIBCXX_TR1_TYPE_TRAITS |