OSDN Git Service

2001-03-04 Phil Edwards <pme@sources.redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / type_traits.h
1 /*
2  *
3  * Copyright (c) 1997
4  * Silicon Graphics Computer Systems, Inc.
5  *
6  * Permission to use, copy, modify, distribute and sell this software
7  * and its documentation for any purpose is hereby granted without fee,
8  * provided that the above copyright notice appear in all copies and
9  * that both that copyright notice and this permission notice appear
10  * in supporting documentation.  Silicon Graphics makes no
11  * representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  */
14
15 #ifndef _CPP_BITS_TYPE_TRAITS_H
16 #define _CPP_BITS_TYPE_TRAITS_H 1
17
18 #pragma GCC system_header
19
20 #include <bits/c++config.h>
21
22 /*
23 This header file provides a framework for allowing compile time dispatch
24 based on type attributes. This is useful when writing template code.
25 For example, when making a copy of an array of an unknown type, it helps
26 to know if the type has a trivial copy constructor or not, to help decide
27 if a memcpy can be used.
28
29 The class template __type_traits provides a series of typedefs each of
30 which is either __true_type or __false_type. The argument to
31 __type_traits can be any type. The typedefs within this template will
32 attain their correct values by one of these means:
33     1. The general instantiation contain conservative values which work
34        for all types.
35     2. Specializations may be declared to make distinctions between types.
36     3. Some compilers (such as the Silicon Graphics N32 and N64 compilers)
37        will automatically provide the appropriate specializations for all
38        types.
39
40 EXAMPLE:
41
42 //Copy an array of elements which have non-trivial copy constructors
43 template <class _Tp> void 
44   copy(_Tp* __source,_Tp* __destination,int __n,__false_type);
45 //Copy an array of elements which have trivial copy constructors. Use memcpy.
46 template <class _Tp> void 
47   copy(_Tp* __source,_Tp* __destination,int __n,__true_type);
48
49 //Copy an array of any type by using the most efficient copy mechanism
50 template <class _Tp> inline void copy(_Tp* __source,_Tp* __destination,int __n) {
51    copy(__source,__destination,__n,
52         typename __type_traits<_Tp>::has_trivial_copy_constructor());
53 }
54 */
55
56
57 template <bool _Truth> struct _Bool {};
58 typedef _Bool<true>  __true_type;
59 typedef _Bool<false> __false_type;
60
61 template <class _Tp>
62 struct __type_traits { 
63    typedef __true_type     this_dummy_member_must_be_first;
64                    /* Do not remove this member. It informs a compiler which
65                       automatically specializes __type_traits that this
66                       __type_traits template is special. It just makes sure that
67                       things work if an implementation is using a template
68                       called __type_traits for something unrelated. */
69
70    /* The following restrictions should be observed for the sake of
71       compilers which automatically produce type specific specializations 
72       of this class:
73           - You may reorder the members below if you wish
74           - You may remove any of the members below if you wish
75           - You must not rename members without making the corresponding
76             name change in the compiler
77           - Members you add will be treated like regular members unless
78             you add the appropriate support in the compiler. */
79  
80
81    typedef __false_type    has_trivial_default_constructor;
82    typedef __false_type    has_trivial_copy_constructor;
83    typedef __false_type    has_trivial_assignment_operator;
84    typedef __false_type    has_trivial_destructor;
85    typedef __false_type    is_POD_type;
86 };
87
88
89 // Provide some specializations.
90
91 template<> struct __type_traits<bool> {
92    typedef __true_type    has_trivial_default_constructor;
93    typedef __true_type    has_trivial_copy_constructor;
94    typedef __true_type    has_trivial_assignment_operator;
95    typedef __true_type    has_trivial_destructor;
96    typedef __true_type    is_POD_type;
97 };
98
99 template<> struct __type_traits<char> {
100    typedef __true_type    has_trivial_default_constructor;
101    typedef __true_type    has_trivial_copy_constructor;
102    typedef __true_type    has_trivial_assignment_operator;
103    typedef __true_type    has_trivial_destructor;
104    typedef __true_type    is_POD_type;
105 };
106
107 template<> struct __type_traits<signed char> {
108    typedef __true_type    has_trivial_default_constructor;
109    typedef __true_type    has_trivial_copy_constructor;
110    typedef __true_type    has_trivial_assignment_operator;
111    typedef __true_type    has_trivial_destructor;
112    typedef __true_type    is_POD_type;
113 };
114
115 template<> struct __type_traits<unsigned char> {
116    typedef __true_type    has_trivial_default_constructor;
117    typedef __true_type    has_trivial_copy_constructor;
118    typedef __true_type    has_trivial_assignment_operator;
119    typedef __true_type    has_trivial_destructor;
120    typedef __true_type    is_POD_type;
121 };
122
123 template<> struct __type_traits<wchar_t> {
124    typedef __true_type    has_trivial_default_constructor;
125    typedef __true_type    has_trivial_copy_constructor;
126    typedef __true_type    has_trivial_assignment_operator;
127    typedef __true_type    has_trivial_destructor;
128    typedef __true_type    is_POD_type;
129 };
130
131 template<> struct __type_traits<short> {
132    typedef __true_type    has_trivial_default_constructor;
133    typedef __true_type    has_trivial_copy_constructor;
134    typedef __true_type    has_trivial_assignment_operator;
135    typedef __true_type    has_trivial_destructor;
136    typedef __true_type    is_POD_type;
137 };
138
139 template<> struct __type_traits<unsigned short> {
140    typedef __true_type    has_trivial_default_constructor;
141    typedef __true_type    has_trivial_copy_constructor;
142    typedef __true_type    has_trivial_assignment_operator;
143    typedef __true_type    has_trivial_destructor;
144    typedef __true_type    is_POD_type;
145 };
146
147 template<> struct __type_traits<int> {
148    typedef __true_type    has_trivial_default_constructor;
149    typedef __true_type    has_trivial_copy_constructor;
150    typedef __true_type    has_trivial_assignment_operator;
151    typedef __true_type    has_trivial_destructor;
152    typedef __true_type    is_POD_type;
153 };
154
155 template<> struct __type_traits<unsigned int> {
156    typedef __true_type    has_trivial_default_constructor;
157    typedef __true_type    has_trivial_copy_constructor;
158    typedef __true_type    has_trivial_assignment_operator;
159    typedef __true_type    has_trivial_destructor;
160    typedef __true_type    is_POD_type;
161 };
162
163 template<> struct __type_traits<long> {
164    typedef __true_type    has_trivial_default_constructor;
165    typedef __true_type    has_trivial_copy_constructor;
166    typedef __true_type    has_trivial_assignment_operator;
167    typedef __true_type    has_trivial_destructor;
168    typedef __true_type    is_POD_type;
169 };
170
171 template<> struct __type_traits<unsigned long> {
172    typedef __true_type    has_trivial_default_constructor;
173    typedef __true_type    has_trivial_copy_constructor;
174    typedef __true_type    has_trivial_assignment_operator;
175    typedef __true_type    has_trivial_destructor;
176    typedef __true_type    is_POD_type;
177 };
178
179 #ifdef _GLIBCPP_USE_LONG_LONG
180
181 template<> struct __type_traits<long long> {
182    typedef __true_type    has_trivial_default_constructor;
183    typedef __true_type    has_trivial_copy_constructor;
184    typedef __true_type    has_trivial_assignment_operator;
185    typedef __true_type    has_trivial_destructor;
186    typedef __true_type    is_POD_type;
187 };
188
189 template<> struct __type_traits<unsigned long long> {
190    typedef __true_type    has_trivial_default_constructor;
191    typedef __true_type    has_trivial_copy_constructor;
192    typedef __true_type    has_trivial_assignment_operator;
193    typedef __true_type    has_trivial_destructor;
194    typedef __true_type    is_POD_type;
195 };
196
197 #endif /* _GLIBCPP_USE_LONG_LONG */
198
199 template<> struct __type_traits<float> {
200    typedef __true_type    has_trivial_default_constructor;
201    typedef __true_type    has_trivial_copy_constructor;
202    typedef __true_type    has_trivial_assignment_operator;
203    typedef __true_type    has_trivial_destructor;
204    typedef __true_type    is_POD_type;
205 };
206
207 template<> struct __type_traits<double> {
208    typedef __true_type    has_trivial_default_constructor;
209    typedef __true_type    has_trivial_copy_constructor;
210    typedef __true_type    has_trivial_assignment_operator;
211    typedef __true_type    has_trivial_destructor;
212    typedef __true_type    is_POD_type;
213 };
214
215 template<> struct __type_traits<long double> {
216    typedef __true_type    has_trivial_default_constructor;
217    typedef __true_type    has_trivial_copy_constructor;
218    typedef __true_type    has_trivial_assignment_operator;
219    typedef __true_type    has_trivial_destructor;
220    typedef __true_type    is_POD_type;
221 };
222
223 template <class _Tp>
224 struct __type_traits<_Tp*> {
225    typedef __true_type    has_trivial_default_constructor;
226    typedef __true_type    has_trivial_copy_constructor;
227    typedef __true_type    has_trivial_assignment_operator;
228    typedef __true_type    has_trivial_destructor;
229    typedef __true_type    is_POD_type;
230 };
231
232
233 // The following could be written in terms of numeric_limits.  
234 // We're doing it separately to reduce the number of dependencies.
235
236 template <class _Tp> struct _Is_integer {
237   typedef __false_type _Integral;
238 };
239
240 template<> struct _Is_integer<bool> {
241   typedef __true_type _Integral;
242 };
243
244 template<> struct _Is_integer<char> {
245   typedef __true_type _Integral;
246 };
247
248 template<> struct _Is_integer<signed char> {
249   typedef __true_type _Integral;
250 };
251
252 template<> struct _Is_integer<unsigned char> {
253   typedef __true_type _Integral;
254 };
255
256 template<> struct _Is_integer<wchar_t> {
257   typedef __true_type _Integral;
258 };
259
260 template<> struct _Is_integer<short> {
261   typedef __true_type _Integral;
262 };
263
264 template<> struct _Is_integer<unsigned short> {
265   typedef __true_type _Integral;
266 };
267
268 template<> struct _Is_integer<int> {
269   typedef __true_type _Integral;
270 };
271
272 template<> struct _Is_integer<unsigned int> {
273   typedef __true_type _Integral;
274 };
275
276 template<> struct _Is_integer<long> {
277   typedef __true_type _Integral;
278 };
279
280 template<> struct _Is_integer<unsigned long> {
281   typedef __true_type _Integral;
282 };
283
284 #ifdef _GLIBCPP_USE_LONG_LONG
285
286 template<> struct _Is_integer<long long> {
287   typedef __true_type _Integral;
288 };
289
290 template<> struct _Is_integer<unsigned long long> {
291   typedef __true_type _Integral;
292 };
293
294 #endif /* _GLIBCPP_USE_LONG_LONG */
295
296 template<typename _Tp> struct _Is_normal_iterator {
297    typedef __false_type _Normal;
298 };
299
300 // Forward declaration hack, should really include this from somewhere.
301 namespace std {
302    template<typename _Iterator, typename _Container> class __normal_iterator;
303 };
304
305 template<typename _Iterator, typename _Container>
306 struct _Is_normal_iterator< std::__normal_iterator<_Iterator, _Container> > {
307    typedef __true_type _Normal;
308 };
309
310 #endif /* _CPP_BITS_TYPE_TRAITS_H */
311
312 // Local Variables:
313 // mode:C++
314 // End: