1 // (C) Copyright Steve Cleary, Beman Dawes, Howard Hinnant & John Maddock 2000.
2 // Permission to copy, use, modify, sell and
3 // distribute this software is granted provided this copyright notice appears
4 // in all copies. This software is provided "as is" without express or implied
5 // warranty, and with no claim as to its suitability for any purpose.
7 // See http://www.boost.org for most recent version including documentation.
9 // defines object traits classes:
10 // is_object, is_scalar, is_class, is_compound, is_POD,
11 // has_trivial_constructor, has_trivial_copy, has_trivial_assign,
12 // has_trivial_destructor, is_empty.
15 #ifndef BOOST_OBJECT_TYPE_TRAITS_HPP
16 #define BOOST_OBJECT_TYPE_TRAITS_HPP
18 #ifndef BOOST_ICE_TYPE_TRAITS_HPP
19 #include <boost/type_traits/ice.hpp>
21 #ifndef BOOST_FWD_TYPE_TRAITS_HPP
22 #include <boost/type_traits/fwd.hpp>
24 #ifndef BOOST_COMPOSITE_TYPE_TRAITS_HPP
25 #include <boost/type_traits/composite_traits.hpp>
27 #ifndef BOOST_ARITHMETIC_TYPE_TRAITS_HPP
28 #include <boost/type_traits/arithmetic_traits.hpp>
30 #ifndef BOOST_FUNCTION_TYPE_TRAITS_HPP
31 #include <boost/type_traits/function_traits.hpp>
33 #ifndef BOOST_TYPE_TRAITS_IS_CLASS_HPP
34 # include <boost/type_traits/is_class.hpp>
37 #ifdef BOOST_HAS_SGI_TYPE_TRAITS
38 # include <type_traits.h>
39 # include <boost/type_traits/same_traits.hpp>
44 /**********************************************
48 **********************************************/
52 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
53 BOOST_STATIC_CONSTANT(bool, value =
54 (::boost::type_traits::ice_and<
55 ::boost::type_traits::ice_not< ::boost::is_reference<T>::value>::value,
56 ::boost::type_traits::ice_not< ::boost::is_void<T>::value>::value,
57 ::boost::type_traits::ice_not< ::boost::is_function<T>::value>::value
60 BOOST_STATIC_CONSTANT(bool, value =
61 (::boost::type_traits::ice_and<
62 ::boost::type_traits::ice_not< ::boost::is_reference<T>::value>::value,
63 ::boost::type_traits::ice_not< ::boost::is_void<T>::value>::value
68 /**********************************************
72 **********************************************/
76 BOOST_STATIC_CONSTANT(bool, value =
77 (::boost::type_traits::ice_or<
78 ::boost::is_arithmetic<T>::value,
79 ::boost::is_enum<T>::value,
80 ::boost::is_pointer<T>::value,
81 ::boost::is_member_pointer<T>::value
85 # ifndef BOOST_TYPE_TRAITS_IS_CLASS_DEFINED
86 // conforming compilers use the implementation in <boost/type_traits/is_class.hpp>
87 /**********************************************
91 **********************************************/
95 # ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
97 BOOST_STATIC_CONSTANT(bool, value =
98 (::boost::type_traits::ice_and<
99 ::boost::type_traits::ice_not< ::boost::is_union<T>::value >::value,
100 ::boost::type_traits::ice_not< ::boost::is_scalar<T>::value >::value,
101 ::boost::type_traits::ice_not< ::boost::is_array<T>::value >::value,
102 ::boost::type_traits::ice_not< ::boost::is_reference<T>::value>::value,
103 ::boost::type_traits::ice_not< ::boost::is_void<T>::value >::value,
104 ::boost::type_traits::ice_not< ::boost::is_function<T>::value >::value
107 BOOST_STATIC_CONSTANT(bool, value =
108 (::boost::type_traits::ice_and<
109 ::boost::type_traits::ice_not< ::boost::is_union<T>::value >::value,
110 ::boost::type_traits::ice_not< ::boost::is_scalar<T>::value >::value,
111 ::boost::type_traits::ice_not< ::boost::is_array<T>::value >::value,
112 ::boost::type_traits::ice_not< ::boost::is_reference<T>::value>::value,
113 ::boost::type_traits::ice_not< ::boost::is_void<T>::value >::value
117 # endif // nonconforming implementations
118 /**********************************************
122 **********************************************/
123 template <typename T> struct is_compound
125 BOOST_STATIC_CONSTANT(bool, value =
126 (::boost::type_traits::ice_or<
127 ::boost::is_array<T>::value,
128 ::boost::is_pointer<T>::value,
129 ::boost::is_reference<T>::value,
130 ::boost::is_class<T>::value,
131 ::boost::is_union<T>::value,
132 ::boost::is_enum<T>::value,
133 ::boost::is_member_pointer<T>::value
137 /**********************************************
141 **********************************************/
143 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
144 template <typename T> struct is_POD
146 BOOST_STATIC_CONSTANT(
148 (::boost::type_traits::ice_or<
149 ::boost::is_scalar<T>::value,
150 ::boost::is_void<T>::value,
155 template <typename T, std::size_t sz>
158 BOOST_STATIC_CONSTANT(bool, value = ::boost::is_POD<T>::value);
163 template <bool is_array = false> struct is_POD_helper;
166 template <typename T> struct is_POD
168 BOOST_STATIC_CONSTANT(
170 ::boost::detail::is_POD_helper<
171 ::boost::is_array<T>::value
172 >::template apply<T>::value
179 template <bool is_array>
182 template <typename T> struct apply
184 BOOST_STATIC_CONSTANT(
186 (::boost::type_traits::ice_or<
187 ::boost::is_scalar<T>::value,
188 ::boost::is_void<T>::value,
197 typedef ::boost::type_traits::no_type type;
201 struct bool_to_type<true>
203 typedef ::boost::type_traits::yes_type type;
206 template <class ArrayType>
207 struct is_POD_array_helper
210 #if !defined(__BORLANDC__) || __BORLANDC__ > 0x551
213 ::boost::detail::bool_to_type<(::boost::is_POD<ArrayType>::value)>::type type;
215 type instance() const;
219 is_POD_array_helper<T> is_POD_array(T*);
222 struct is_POD_helper<true>
224 template <typename T> struct apply
228 BOOST_STATIC_CONSTANT(
230 sizeof(is_POD_array(help()).instance()) == sizeof(::boost::type_traits::yes_type));
236 /**********************************************
238 * has_trivial_constructor
240 **********************************************/
241 template <typename T>
242 struct has_trivial_constructor
244 BOOST_STATIC_CONSTANT(bool, value =
245 (::boost::type_traits::ice_or<
246 ::boost::is_POD<T>::value,
247 BOOST_HAS_TRIVIAL_CONSTRUCTOR(T)
251 /**********************************************
255 **********************************************/
256 template <typename T>
257 struct has_trivial_copy
259 BOOST_STATIC_CONSTANT(bool, value =
260 (::boost::type_traits::ice_and<
261 ::boost::type_traits::ice_or<
262 ::boost::is_POD<T>::value,
263 BOOST_HAS_TRIVIAL_COPY(T)
265 ::boost::type_traits::ice_not< ::boost::is_volatile<T>::value >::value
269 /**********************************************
273 **********************************************/
274 template <typename T>
275 struct has_trivial_assign
277 BOOST_STATIC_CONSTANT(bool, value =
278 (::boost::type_traits::ice_and<
279 ::boost::type_traits::ice_or<
280 ::boost::is_POD<T>::value,
281 BOOST_HAS_TRIVIAL_ASSIGN(T)
283 ::boost::type_traits::ice_not< ::boost::is_const<T>::value >::value,
284 ::boost::type_traits::ice_not< ::boost::is_volatile<T>::value >::value
288 /**********************************************
290 * has_trivial_destructor
292 **********************************************/
293 template <typename T>
294 struct has_trivial_destructor
296 BOOST_STATIC_CONSTANT(bool, value =
297 (::boost::type_traits::ice_or<
298 ::boost::is_POD<T>::value,
299 BOOST_HAS_TRIVIAL_DESTRUCTOR(T)
303 /**********************************************
305 * has_nothrow_constructor
307 **********************************************/
308 template <typename T>
309 struct has_nothrow_constructor
311 BOOST_STATIC_CONSTANT(bool, value =
312 (::boost::has_trivial_constructor<T>::value));
315 /**********************************************
319 **********************************************/
320 template <typename T>
321 struct has_nothrow_copy
323 BOOST_STATIC_CONSTANT(bool, value =
324 (::boost::has_trivial_copy<T>::value));
327 /**********************************************
331 **********************************************/
332 template <typename T>
333 struct has_nothrow_assign
335 BOOST_STATIC_CONSTANT(bool, value =
336 (::boost::has_trivial_assign<T>::value));
339 /**********************************************
343 **********************************************/
344 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
347 template <typename T>
348 struct empty_helper_t1 : public T
351 empty_helper_t1(); // hh compiler bug workaround
355 struct empty_helper_t2 { int i[256]; };
358 # ifndef __BORLANDC__
361 template <typename T, bool is_a_class = false>
362 struct empty_helper{ BOOST_STATIC_CONSTANT(bool, value = false); };
364 template <typename T>
365 struct empty_helper<T, true>
367 BOOST_STATIC_CONSTANT(
368 bool, value = (sizeof(empty_helper_t1<T>) == sizeof(empty_helper_t2)));
372 template <typename T>
376 typedef typename remove_cv<T>::type cvt;
379 BOOST_STATIC_CONSTANT(
381 ::boost::type_traits::ice_or<
382 ::boost::detail::empty_helper<T,::boost::is_class<T>::value>::value
383 , BOOST_IS_EMPTY(cvt)
388 # else // __BORLANDC__
392 template <typename T, bool is_a_class, bool convertible_to_int>
393 struct empty_helper{ BOOST_STATIC_CONSTANT(bool, value = false); };
395 template <typename T>
396 struct empty_helper<T, true, false>
398 BOOST_STATIC_CONSTANT(bool, value =
399 (sizeof(empty_helper_t1<T>) == sizeof(empty_helper_t2)));
403 template <typename T>
407 typedef typename remove_cv<T>::type cvt;
408 typedef typename add_reference<T>::type r_type;
410 BOOST_STATIC_CONSTANT(
412 ::boost::type_traits::ice_or<
413 ::boost::detail::empty_helper<
415 , ::boost::is_class<T>::value
416 , ::boost::is_convertible< r_type,int>::value
418 , BOOST_IS_EMPTY(cvt)
421 # endif // __BORLANDC__
423 #else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
424 #ifdef BOOST_MSVC6_MEMBER_TEMPLATES
428 template <typename T>
429 struct empty_helper_t1 : public T
434 struct empty_helper_t2 { int i[256]; };
436 template <typename T>
437 struct empty_helper_base
439 enum{ value = (sizeof(empty_helper_t1<T>) == sizeof(empty_helper_t2)) };
442 template <typename T>
443 struct empty_helper_nonbase
445 enum{ value = false };
449 struct empty_helper_chooser
454 typedef empty_helper_nonbase<T> type;
459 struct empty_helper_chooser<true>
464 typedef empty_helper_base<T> type;
468 } // namespace detail
470 template <typename T>
474 typedef ::boost::detail::empty_helper_chooser<
475 ::boost::type_traits::ice_and<
476 ::boost::type_traits::ice_not<
477 ::boost::is_reference<T>::value>::value,
478 ::boost::type_traits::ice_not<
479 ::boost::is_convertible<T,double>::value>::value,
480 ::boost::type_traits::ice_not<
481 ::boost::is_pointer<T>::value>::value,
482 ::boost::type_traits::ice_not<
483 ::boost::is_member_pointer<T>::value>::value,
484 ::boost::type_traits::ice_not<
485 ::boost::is_array<T>::value>::value,
486 ::boost::type_traits::ice_not<
487 ::boost::is_void<T>::value>::value,
488 ::boost::type_traits::ice_not<
489 ::boost::is_convertible<T, const volatile void*>::value>::value
491 typedef typename chooser::template rebind<T> bound_type;
492 typedef typename bound_type::type eh_type;
494 BOOST_STATIC_CONSTANT(bool, value =
495 (::boost::type_traits::ice_or<eh_type::value, BOOST_IS_EMPTY(T)>::value));
499 template <typename T> struct is_empty
500 { enum{ value = BOOST_IS_EMPTY(T) }; };
501 #endif // BOOST_MSVC6_MEMBER_TEMPLATES
503 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
505 /**********************************************
509 **********************************************/
510 template <typename T>
513 BOOST_STATIC_CONSTANT(bool, value =
514 (::boost::type_traits::ice_and<
515 ::boost::has_trivial_constructor<T>::value,
516 ::boost::has_trivial_copy<T>::value,
517 ::boost::has_trivial_destructor<T>::value,
518 ::boost::is_class<T>::value,
519 ::boost::is_empty<T>::value
523 template <class Base, class Derived>
524 struct is_base_and_derived
526 BOOST_STATIC_CONSTANT(bool, value =
527 (::boost::type_traits::ice_and<
528 ::boost::is_convertible<Derived*,Base*>::value,
529 ::boost::is_class<Derived>::value,
530 ::boost::is_class<Base>::value
535 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
536 template <class Base, class Derived>
537 struct is_base_and_derived<Base&, Derived>
539 BOOST_STATIC_CONSTANT(bool, value = false);
541 template <class Base, class Derived>
542 struct is_base_and_derived<Base, Derived&>
544 BOOST_STATIC_CONSTANT(bool, value = false);
546 template <class Base, class Derived>
547 struct is_base_and_derived<Base&, Derived&>
549 BOOST_STATIC_CONSTANT(bool, value = false);
555 #endif // BOOST_OBJECT_TYPE_TRAITS_HPP