type_traits.h revision 200581
1//===- llvm/Support/type_traits.h - Simplfied type traits -------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file provides a template class that determines if a type is a class or
11// not. The basic mechanism, based on using the pointer to member function of
12// a zero argument to a function was "boosted" from the boost type_traits
13// library. See http://www.boost.org/ for all the gory details.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_SUPPORT_TYPE_TRAITS_H
18#define LLVM_SUPPORT_TYPE_TRAITS_H
19
20#include <utility>
21
22// This is actually the conforming implementation which works with abstract
23// classes.  However, enough compilers have trouble with it that most will use
24// the one in boost/type_traits/object_traits.hpp. This implementation actually
25// works with VC7.0, but other interactions seem to fail when we use it.
26
27namespace llvm {
28
29namespace dont_use
30{
31    // These two functions should never be used. They are helpers to
32    // the is_class template below. They cannot be located inside
33    // is_class because doing so causes at least GCC to think that
34    // the value of the "value" enumerator is not constant. Placing
35    // them out here (for some strange reason) allows the sizeof
36    // operator against them to magically be constant. This is
37    // important to make the is_class<T>::value idiom zero cost. it
38    // evaluates to a constant 1 or 0 depending on whether the
39    // parameter T is a class or not (respectively).
40    template<typename T> char is_class_helper(void(T::*)());
41    template<typename T> double is_class_helper(...);
42}
43
44template <typename T>
45struct is_class
46{
47  // is_class<> metafunction due to Paul Mensonides (leavings@attbi.com). For
48  // more details:
49  // http://groups.google.com/groups?hl=en&selm=000001c1cc83%24e154d5e0%247772e50c%40c161550a&rnum=1
50 public:
51    enum { value = sizeof(char) == sizeof(dont_use::is_class_helper<T>(0)) };
52};
53
54
55/// isPodLike - This is a type trait that is used to determine whether a given
56/// type can be copied around with memcpy instead of running ctors etc.
57template <typename T>
58struct isPodLike {
59  // If we don't know anything else, we can (at least) assume that all non-class
60  // types are PODs.
61  static const bool value = !is_class<T>::value;
62};
63
64// std::pair's are pod-like if their elements are.
65template<typename T, typename U>
66struct isPodLike<std::pair<T, U> > {
67  static const bool value = isPodLike<T>::value & isPodLike<U>::value;
68};
69
70
71/// \brief Metafunction that determines whether the two given types are
72/// equivalent.
73template<typename T, typename U>
74struct is_same {
75  static const bool value = false;
76};
77
78template<typename T>
79struct is_same<T, T> {
80  static const bool value = true;
81};
82
83// enable_if_c - Enable/disable a template based on a metafunction
84template<bool Cond, typename T = void>
85struct enable_if_c {
86  typedef T type;
87};
88
89template<typename T> struct enable_if_c<false, T> { };
90
91// enable_if - Enable/disable a template based on a metafunction
92template<typename Cond, typename T = void>
93struct enable_if : public enable_if_c<Cond::value, T> { };
94
95namespace dont_use {
96  template<typename Base> char base_of_helper(const volatile Base*);
97  template<typename Base> double base_of_helper(...);
98}
99
100/// is_base_of - Metafunction to determine whether one type is a base class of
101/// (or identical to) another type.
102template<typename Base, typename Derived>
103struct is_base_of {
104  static const bool value
105    = is_class<Base>::value && is_class<Derived>::value &&
106      sizeof(char) == sizeof(dont_use::base_of_helper<Base>((Derived*)0));
107};
108
109// remove_pointer - Metafunction to turn Foo* into Foo.  Defined in
110// C++0x [meta.trans.ptr].
111template <typename T> struct remove_pointer { typedef T type; };
112template <typename T> struct remove_pointer<T*> { typedef T type; };
113template <typename T> struct remove_pointer<T*const> { typedef T type; };
114template <typename T> struct remove_pointer<T*volatile> { typedef T type; };
115template <typename T> struct remove_pointer<T*const volatile> {
116    typedef T type; };
117
118template <bool, typename T, typename F>
119struct conditional { typedef T type; };
120
121template <typename T, typename F>
122struct conditional<false, T, F> { typedef F type; };
123
124}
125
126#endif
127