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