1f3f1e4f8936c2449d1743e43f00e6c4fa79f3eb
[oota-llvm.git] / include / llvm / Support / type_traits.h
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
27 namespace llvm {
28
29 /// isPodLike - This is a type trait that is used to determine whether a given
30 /// type can be copied around with memcpy instead of running ctors etc.
31 template <typename T>
32 struct isPodLike {
33   static const bool value = false;  
34 };
35   
36 // pointers are all pod-like.
37 template <typename T>
38 struct isPodLike<T*> { static const bool value = true; };
39
40 // builtin types are pod-like as well.
41 // There is probably a much better way to do this.
42 template <> struct isPodLike<char> { static const bool value = true; };
43 template <> struct isPodLike<unsigned> { static const bool value = true; };
44 template <> struct isPodLike<unsigned long> { static const bool value = true; };
45 template <> struct isPodLike<unsigned long long> {
46   static const bool value = true;
47 };
48   
49   
50 // pairs are pod-like if their elements are.
51 template<typename T, typename U>
52 struct isPodLike<std::pair<T, U> > {
53   static const bool value = isPodLike<T>::value & isPodLike<U>::value;
54 };
55   
56 namespace dont_use
57 {
58     // These two functions should never be used. They are helpers to
59     // the is_class template below. They cannot be located inside
60     // is_class because doing so causes at least GCC to think that
61     // the value of the "value" enumerator is not constant. Placing
62     // them out here (for some strange reason) allows the sizeof
63     // operator against them to magically be constant. This is
64     // important to make the is_class<T>::value idiom zero cost. it
65     // evaluates to a constant 1 or 0 depending on whether the
66     // parameter T is a class or not (respectively).
67     template<typename T> char is_class_helper(void(T::*)());
68     template<typename T> double is_class_helper(...);
69 }
70
71 template <typename T>
72 struct is_class
73 {
74   // is_class<> metafunction due to Paul Mensonides (leavings@attbi.com). For
75   // more details:
76   // http://groups.google.com/groups?hl=en&selm=000001c1cc83%24e154d5e0%247772e50c%40c161550a&rnum=1
77  public:
78     enum { value = sizeof(char) == sizeof(dont_use::is_class_helper<T>(0)) };
79 };
80
81 /// \brief Metafunction that determines whether the two given types are 
82 /// equivalent.
83 template<typename T, typename U>
84 struct is_same {
85   static const bool value = false;
86 };
87
88 template<typename T>
89 struct is_same<T, T> {
90   static const bool value = true;
91 };
92   
93 // enable_if_c - Enable/disable a template based on a metafunction
94 template<bool Cond, typename T = void>
95 struct enable_if_c {
96   typedef T type;
97 };
98
99 template<typename T> struct enable_if_c<false, T> { };
100   
101 // enable_if - Enable/disable a template based on a metafunction
102 template<typename Cond, typename T = void>
103 struct enable_if : public enable_if_c<Cond::value, T> { };
104
105 namespace dont_use {
106   template<typename Base> char base_of_helper(const volatile Base*);
107   template<typename Base> double base_of_helper(...);
108 }
109
110 /// is_base_of - Metafunction to determine whether one type is a base class of
111 /// (or identical to) another type.
112 template<typename Base, typename Derived>
113 struct is_base_of {
114   static const bool value 
115     = is_class<Base>::value && is_class<Derived>::value &&
116       sizeof(char) == sizeof(dont_use::base_of_helper<Base>((Derived*)0));
117 };
118
119 // remove_pointer - Metafunction to turn Foo* into Foo.  Defined in
120 // C++0x [meta.trans.ptr].
121 template <typename T> struct remove_pointer { typedef T type; };
122 template <typename T> struct remove_pointer<T*> { typedef T type; };
123 template <typename T> struct remove_pointer<T*const> { typedef T type; };
124 template <typename T> struct remove_pointer<T*volatile> { typedef T type; };
125 template <typename T> struct remove_pointer<T*const volatile> {
126     typedef T type; };
127
128 template <bool, typename T, typename F>
129 struct conditional { typedef T type; };
130
131 template <typename T, typename F>
132 struct conditional<false, T, F> { typedef F type; };
133
134 }
135
136 #endif