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 __TYPE_TRAITS_H 16 #define __TYPE_TRAITS_H 17 18 #ifndef __STL_CONFIG_H 19 #include <stl_config.h> 20 #endif 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 T> void copy(T* source, T* destination, int n, __false_type); 44 //Copy an array of elements which have trivial copy constructors. Use memcpy. 45 template <class T> void copy(T* source, T* destination, int n, __true_type); 46 47 //Copy an array of any type by using the most efficient copy mechanism 48 template <class T> inline void copy(T* source,T* destination,int n) { 49 copy(source, destination, n, 50 typename __type_traits<T>::has_trivial_copy_constructor()); 51 } 52 */ 53 54 55 struct __true_type { 56 }; 57 58 struct __false_type { 59 }; 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 90 // Provide some specializations. This is harmless for compilers that 91 // have built-in __types_traits support, and essential for compilers 92 // that don't. 93 94 #ifndef __STL_NO_BOOL 95 96 __STL_TEMPLATE_NULL struct __type_traits<bool> { 97 typedef __true_type has_trivial_default_constructor; 98 typedef __true_type has_trivial_copy_constructor; 99 typedef __true_type has_trivial_assignment_operator; 100 typedef __true_type has_trivial_destructor; 101 typedef __true_type is_POD_type; 102 }; 103 104 #endif /* __STL_NO_BOOL */ 105 106 __STL_TEMPLATE_NULL struct __type_traits<char> { 107 typedef __true_type has_trivial_default_constructor; 108 typedef __true_type has_trivial_copy_constructor; 109 typedef __true_type has_trivial_assignment_operator; 110 typedef __true_type has_trivial_destructor; 111 typedef __true_type is_POD_type; 112 }; 113 114 __STL_TEMPLATE_NULL struct __type_traits<signed char> { 115 typedef __true_type has_trivial_default_constructor; 116 typedef __true_type has_trivial_copy_constructor; 117 typedef __true_type has_trivial_assignment_operator; 118 typedef __true_type has_trivial_destructor; 119 typedef __true_type is_POD_type; 120 }; 121 122 __STL_TEMPLATE_NULL struct __type_traits<unsigned char> { 123 typedef __true_type has_trivial_default_constructor; 124 typedef __true_type has_trivial_copy_constructor; 125 typedef __true_type has_trivial_assignment_operator; 126 typedef __true_type has_trivial_destructor; 127 typedef __true_type is_POD_type; 128 }; 129 130 #ifdef __STL_HAS_WCHAR_T 131 132 __STL_TEMPLATE_NULL struct __type_traits<wchar_t> { 133 typedef __true_type has_trivial_default_constructor; 134 typedef __true_type has_trivial_copy_constructor; 135 typedef __true_type has_trivial_assignment_operator; 136 typedef __true_type has_trivial_destructor; 137 typedef __true_type is_POD_type; 138 }; 139 140 #endif /* __STL_HAS_WCHAR_T */ 141 142 __STL_TEMPLATE_NULL struct __type_traits<short> { 143 typedef __true_type has_trivial_default_constructor; 144 typedef __true_type has_trivial_copy_constructor; 145 typedef __true_type has_trivial_assignment_operator; 146 typedef __true_type has_trivial_destructor; 147 typedef __true_type is_POD_type; 148 }; 149 150 __STL_TEMPLATE_NULL struct __type_traits<unsigned short> { 151 typedef __true_type has_trivial_default_constructor; 152 typedef __true_type has_trivial_copy_constructor; 153 typedef __true_type has_trivial_assignment_operator; 154 typedef __true_type has_trivial_destructor; 155 typedef __true_type is_POD_type; 156 }; 157 158 __STL_TEMPLATE_NULL struct __type_traits<int> { 159 typedef __true_type has_trivial_default_constructor; 160 typedef __true_type has_trivial_copy_constructor; 161 typedef __true_type has_trivial_assignment_operator; 162 typedef __true_type has_trivial_destructor; 163 typedef __true_type is_POD_type; 164 }; 165 166 __STL_TEMPLATE_NULL struct __type_traits<unsigned int> { 167 typedef __true_type has_trivial_default_constructor; 168 typedef __true_type has_trivial_copy_constructor; 169 typedef __true_type has_trivial_assignment_operator; 170 typedef __true_type has_trivial_destructor; 171 typedef __true_type is_POD_type; 172 }; 173 174 __STL_TEMPLATE_NULL struct __type_traits<long> { 175 typedef __true_type has_trivial_default_constructor; 176 typedef __true_type has_trivial_copy_constructor; 177 typedef __true_type has_trivial_assignment_operator; 178 typedef __true_type has_trivial_destructor; 179 typedef __true_type is_POD_type; 180 }; 181 182 __STL_TEMPLATE_NULL struct __type_traits<unsigned long> { 183 typedef __true_type has_trivial_default_constructor; 184 typedef __true_type has_trivial_copy_constructor; 185 typedef __true_type has_trivial_assignment_operator; 186 typedef __true_type has_trivial_destructor; 187 typedef __true_type is_POD_type; 188 }; 189 190 #ifdef __STL_LONG_LONG 191 192 __STL_TEMPLATE_NULL struct __type_traits<long long> { 193 typedef __true_type has_trivial_default_constructor; 194 typedef __true_type has_trivial_copy_constructor; 195 typedef __true_type has_trivial_assignment_operator; 196 typedef __true_type has_trivial_destructor; 197 typedef __true_type is_POD_type; 198 }; 199 200 __STL_TEMPLATE_NULL struct __type_traits<unsigned long long> { 201 typedef __true_type has_trivial_default_constructor; 202 typedef __true_type has_trivial_copy_constructor; 203 typedef __true_type has_trivial_assignment_operator; 204 typedef __true_type has_trivial_destructor; 205 typedef __true_type is_POD_type; 206 }; 207 208 #endif /* __STL_LONG_LONG */ 209 210 __STL_TEMPLATE_NULL struct __type_traits<float> { 211 typedef __true_type has_trivial_default_constructor; 212 typedef __true_type has_trivial_copy_constructor; 213 typedef __true_type has_trivial_assignment_operator; 214 typedef __true_type has_trivial_destructor; 215 typedef __true_type is_POD_type; 216 }; 217 218 __STL_TEMPLATE_NULL struct __type_traits<double> { 219 typedef __true_type has_trivial_default_constructor; 220 typedef __true_type has_trivial_copy_constructor; 221 typedef __true_type has_trivial_assignment_operator; 222 typedef __true_type has_trivial_destructor; 223 typedef __true_type is_POD_type; 224 }; 225 226 __STL_TEMPLATE_NULL struct __type_traits<long double> { 227 typedef __true_type has_trivial_default_constructor; 228 typedef __true_type has_trivial_copy_constructor; 229 typedef __true_type has_trivial_assignment_operator; 230 typedef __true_type has_trivial_destructor; 231 typedef __true_type is_POD_type; 232 }; 233 234 #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION 235 236 template <class _Tp> 237 struct __type_traits<_Tp*> { 238 typedef __true_type has_trivial_default_constructor; 239 typedef __true_type has_trivial_copy_constructor; 240 typedef __true_type has_trivial_assignment_operator; 241 typedef __true_type has_trivial_destructor; 242 typedef __true_type is_POD_type; 243 }; 244 245 #else /* __STL_CLASS_PARTIAL_SPECIALIZATION */ 246 247 __STL_TEMPLATE_NULL struct __type_traits<char*> { 248 typedef __true_type has_trivial_default_constructor; 249 typedef __true_type has_trivial_copy_constructor; 250 typedef __true_type has_trivial_assignment_operator; 251 typedef __true_type has_trivial_destructor; 252 typedef __true_type is_POD_type; 253 }; 254 255 __STL_TEMPLATE_NULL struct __type_traits<signed char*> { 256 typedef __true_type has_trivial_default_constructor; 257 typedef __true_type has_trivial_copy_constructor; 258 typedef __true_type has_trivial_assignment_operator; 259 typedef __true_type has_trivial_destructor; 260 typedef __true_type is_POD_type; 261 }; 262 263 __STL_TEMPLATE_NULL struct __type_traits<unsigned char*> { 264 typedef __true_type has_trivial_default_constructor; 265 typedef __true_type has_trivial_copy_constructor; 266 typedef __true_type has_trivial_assignment_operator; 267 typedef __true_type has_trivial_destructor; 268 typedef __true_type is_POD_type; 269 }; 270 271 __STL_TEMPLATE_NULL struct __type_traits<const char*> { 272 typedef __true_type has_trivial_default_constructor; 273 typedef __true_type has_trivial_copy_constructor; 274 typedef __true_type has_trivial_assignment_operator; 275 typedef __true_type has_trivial_destructor; 276 typedef __true_type is_POD_type; 277 }; 278 279 __STL_TEMPLATE_NULL struct __type_traits<const signed char*> { 280 typedef __true_type has_trivial_default_constructor; 281 typedef __true_type has_trivial_copy_constructor; 282 typedef __true_type has_trivial_assignment_operator; 283 typedef __true_type has_trivial_destructor; 284 typedef __true_type is_POD_type; 285 }; 286 287 __STL_TEMPLATE_NULL struct __type_traits<const unsigned char*> { 288 typedef __true_type has_trivial_default_constructor; 289 typedef __true_type has_trivial_copy_constructor; 290 typedef __true_type has_trivial_assignment_operator; 291 typedef __true_type has_trivial_destructor; 292 typedef __true_type is_POD_type; 293 }; 294 295 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */ 296 297 298 // The following could be written in terms of numeric_limits. 299 // We're doing it separately to reduce the number of dependencies. 300 301 template <class _Tp> struct _Is_integer { 302 typedef __false_type _Integral; 303 }; 304 305 #ifndef __STL_NO_BOOL 306 307 __STL_TEMPLATE_NULL struct _Is_integer<bool> { 308 typedef __true_type _Integral; 309 }; 310 311 #endif /* __STL_NO_BOOL */ 312 313 __STL_TEMPLATE_NULL struct _Is_integer<char> { 314 typedef __true_type _Integral; 315 }; 316 317 __STL_TEMPLATE_NULL struct _Is_integer<signed char> { 318 typedef __true_type _Integral; 319 }; 320 321 __STL_TEMPLATE_NULL struct _Is_integer<unsigned char> { 322 typedef __true_type _Integral; 323 }; 324 325 #ifdef __STL_HAS_WCHAR_T 326 327 __STL_TEMPLATE_NULL struct _Is_integer<wchar_t> { 328 typedef __true_type _Integral; 329 }; 330 331 #endif /* __STL_HAS_WCHAR_T */ 332 333 __STL_TEMPLATE_NULL struct _Is_integer<short> { 334 typedef __true_type _Integral; 335 }; 336 337 __STL_TEMPLATE_NULL struct _Is_integer<unsigned short> { 338 typedef __true_type _Integral; 339 }; 340 341 __STL_TEMPLATE_NULL struct _Is_integer<int> { 342 typedef __true_type _Integral; 343 }; 344 345 __STL_TEMPLATE_NULL struct _Is_integer<unsigned int> { 346 typedef __true_type _Integral; 347 }; 348 349 __STL_TEMPLATE_NULL struct _Is_integer<long> { 350 typedef __true_type _Integral; 351 }; 352 353 __STL_TEMPLATE_NULL struct _Is_integer<unsigned long> { 354 typedef __true_type _Integral; 355 }; 356 357 #ifdef __STL_LONG_LONG 358 359 __STL_TEMPLATE_NULL struct _Is_integer<long long> { 360 typedef __true_type _Integral; 361 }; 362 363 __STL_TEMPLATE_NULL struct _Is_integer<unsigned long long> { 364 typedef __true_type _Integral; 365 }; 366 367 #endif /* __STL_LONG_LONG */ 368 369 #endif /* __TYPE_TRAITS_H */ 370 371 // Local Variables: 372 // mode:C++ 373 // End: 374