]>
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 | { | |
3c279553 PC |
63 | #define _DEFINE_SPEC_HELPER(_Spec) \ |
64 | template<> \ | |
65 | struct _Spec \ | |
66 | : public true_type { }; | |
51687431 | 67 | |
3c279553 PC |
68 | #define _DEFINE_SPEC(_Trait, _Type) \ |
69 | _DEFINE_SPEC_HELPER(_Trait<_Type>) \ | |
70 | _DEFINE_SPEC_HELPER(_Trait<_Type const>) \ | |
71 | _DEFINE_SPEC_HELPER(_Trait<_Type volatile>) \ | |
72 | _DEFINE_SPEC_HELPER(_Trait<_Type const volatile>) | |
e192ab01 | 73 | |
493bc460 | 74 | template<typename> |
4a27a739 | 75 | struct is_reference |
0f910b4f PC |
76 | : public false_type { }; |
77 | ||
4a27a739 PC |
78 | template<typename _Tp> |
79 | struct is_reference<_Tp&> | |
80 | : public true_type { }; | |
493bc460 | 81 | |
1933b74f PC |
82 | template<typename _Tp> |
83 | struct is_pod | |
6a7508ec | 84 | : public integral_constant<bool, __is_pod(_Tp) || is_void<_Tp>::value> |
1933b74f PC |
85 | { }; |
86 | ||
87 | template<typename _Tp> | |
88 | struct has_trivial_constructor | |
e133ace8 PC |
89 | : public integral_constant<bool, is_pod<_Tp>::value> |
90 | { }; | |
1933b74f | 91 | |
36651afe PC |
92 | template<typename _Tp> |
93 | struct has_trivial_copy | |
e133ace8 PC |
94 | : public integral_constant<bool, is_pod<_Tp>::value> |
95 | { }; | |
36651afe PC |
96 | |
97 | template<typename _Tp> | |
98 | struct has_trivial_assign | |
e133ace8 PC |
99 | : public integral_constant<bool, is_pod<_Tp>::value> |
100 | { }; | |
36651afe | 101 | |
1933b74f PC |
102 | template<typename _Tp> |
103 | struct has_trivial_destructor | |
e133ace8 PC |
104 | : public integral_constant<bool, is_pod<_Tp>::value> |
105 | { }; | |
1933b74f | 106 | |
5249b4b0 PC |
107 | template<typename _Tp> |
108 | struct has_nothrow_constructor | |
e133ace8 PC |
109 | : public integral_constant<bool, is_pod<_Tp>::value> |
110 | { }; | |
5249b4b0 | 111 | |
36651afe PC |
112 | template<typename _Tp> |
113 | struct has_nothrow_copy | |
e133ace8 | 114 | : public integral_constant<bool, is_pod<_Tp>::value> |
22931aa4 | 115 | { }; |
464b277b PC |
116 | |
117 | template<typename _Tp> | |
e133ace8 PC |
118 | struct has_nothrow_assign |
119 | : public integral_constant<bool, is_pod<_Tp>::value> | |
973cb10b | 120 | { }; |
493bc460 | 121 | |
4a27a739 PC |
122 | template<typename> |
123 | struct is_signed | |
124 | : public false_type { }; | |
3c279553 PC |
125 | _DEFINE_SPEC(is_signed, signed char) |
126 | _DEFINE_SPEC(is_signed, short) | |
127 | _DEFINE_SPEC(is_signed, int) | |
128 | _DEFINE_SPEC(is_signed, long) | |
129 | _DEFINE_SPEC(is_signed, long long) | |
4a27a739 PC |
130 | |
131 | template<typename> | |
132 | struct is_unsigned | |
133 | : public false_type { }; | |
3c279553 PC |
134 | _DEFINE_SPEC(is_unsigned, unsigned char) |
135 | _DEFINE_SPEC(is_unsigned, unsigned short) | |
136 | _DEFINE_SPEC(is_unsigned, unsigned int) | |
137 | _DEFINE_SPEC(is_unsigned, unsigned long) | |
138 | _DEFINE_SPEC(is_unsigned, unsigned long long) | |
4a27a739 | 139 | |
22931aa4 | 140 | template<typename _Base, typename _Derived> |
f4e4284d | 141 | struct __is_base_of_helper |
f4e4284d | 142 | { |
22931aa4 PC |
143 | typedef typename remove_cv<_Base>::type _NoCv_Base; |
144 | typedef typename remove_cv<_Derived>::type _NoCv_Derived; | |
145 | static const bool __value = (is_same<_Base, _Derived>::value | |
146 | || (__is_base_of(_Base, _Derived) | |
147 | && !is_same<_NoCv_Base, | |
148 | _NoCv_Derived>::value)); | |
f4e4284d | 149 | }; |
22931aa4 | 150 | |
f4e4284d PC |
151 | template<typename _Base, typename _Derived> |
152 | struct is_base_of | |
153 | : public integral_constant<bool, | |
154 | __is_base_of_helper<_Base, _Derived>::__value> | |
155 | { }; | |
156 | ||
516ebd44 PC |
157 | template<typename _From, typename _To> |
158 | struct __is_convertible_simple | |
159 | : public __sfinae_types | |
160 | { | |
161 | private: | |
162 | static __one __test(_To); | |
163 | static __two __test(...); | |
164 | static _From __makeFrom(); | |
165 | ||
166 | public: | |
167 | static const bool __value = sizeof(__test(__makeFrom())) == 1; | |
168 | }; | |
169 | ||
beb1b475 PC |
170 | template<typename _Tp> |
171 | struct add_reference; | |
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 | ||
3c279553 | 244 | #undef _DEFINE_SPEC_HELPER |
e2f5c678 | 245 | #undef _DEFINE_SPEC |
e133ace8 | 246 | } |
493bc460 PC |
247 | } |
248 | ||
e133ace8 | 249 | #endif // _GLIBCXX_TR1_TYPE_TRAITS |