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