type_traits.h revision a1eb50fe3d8213156c77ef2f7ae5c4ad629dbb95
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 "llvm/Support/DataTypes.h" 21#include <cstddef> 22#include <utility> 23 24// This is actually the conforming implementation which works with abstract 25// classes. However, enough compilers have trouble with it that most will use 26// the one in boost/type_traits/object_traits.hpp. This implementation actually 27// works with VC7.0, but other interactions seem to fail when we use it. 28 29namespace llvm { 30 31namespace dont_use 32{ 33 // These two functions should never be used. They are helpers to 34 // the is_class template below. They cannot be located inside 35 // is_class because doing so causes at least GCC to think that 36 // the value of the "value" enumerator is not constant. Placing 37 // them out here (for some strange reason) allows the sizeof 38 // operator against them to magically be constant. This is 39 // important to make the is_class<T>::value idiom zero cost. it 40 // evaluates to a constant 1 or 0 depending on whether the 41 // parameter T is a class or not (respectively). 42 template<typename T> char is_class_helper(void(T::*)()); 43 template<typename T> double is_class_helper(...); 44} 45 46template <typename T> 47struct is_class 48{ 49 // is_class<> metafunction due to Paul Mensonides (leavings@attbi.com). For 50 // more details: 51 // http://groups.google.com/groups?hl=en&selm=000001c1cc83%24e154d5e0%247772e50c%40c161550a&rnum=1 52 public: 53 enum { value = sizeof(char) == sizeof(dont_use::is_class_helper<T>(0)) }; 54}; 55 56 57/// isPodLike - This is a type trait that is used to determine whether a given 58/// type can be copied around with memcpy instead of running ctors etc. 59template <typename T> 60struct isPodLike { 61 // If we don't know anything else, we can (at least) assume that all non-class 62 // types are PODs. 63 static const bool value = !is_class<T>::value; 64}; 65 66// std::pair's are pod-like if their elements are. 67template<typename T, typename U> 68struct isPodLike<std::pair<T, U> > { 69 static const bool value = isPodLike<T>::value & isPodLike<U>::value; 70}; 71 72 73template <class T, T v> 74struct integral_constant { 75 typedef T value_type; 76 static const value_type value = v; 77 typedef integral_constant<T,v> type; 78 operator value_type() { return value; } 79}; 80 81typedef integral_constant<bool, true> true_type; 82typedef integral_constant<bool, false> false_type; 83 84/// \brief Metafunction that determines whether the two given types are 85/// equivalent. 86template<typename T, typename U> struct is_same : public false_type {}; 87template<typename T> struct is_same<T, T> : public true_type {}; 88 89/// \brief Metafunction that removes const qualification from a type. 90template <typename T> struct remove_const { typedef T type; }; 91template <typename T> struct remove_const<const T> { typedef T type; }; 92 93/// \brief Metafunction that removes volatile qualification from a type. 94template <typename T> struct remove_volatile { typedef T type; }; 95template <typename T> struct remove_volatile<volatile T> { typedef T type; }; 96 97/// \brief Metafunction that removes both const and volatile qualification from 98/// a type. 99template <typename T> struct remove_cv { 100 typedef typename remove_const<typename remove_volatile<T>::type>::type type; 101}; 102 103/// \brief Helper to implement is_integral metafunction. 104template <typename T> struct is_integral_impl : false_type {}; 105template <> struct is_integral_impl< bool> : true_type {}; 106template <> struct is_integral_impl< char> : true_type {}; 107template <> struct is_integral_impl< signed char> : true_type {}; 108template <> struct is_integral_impl<unsigned char> : true_type {}; 109template <> struct is_integral_impl< wchar_t> : true_type {}; 110template <> struct is_integral_impl< short> : true_type {}; 111template <> struct is_integral_impl<unsigned short> : true_type {}; 112template <> struct is_integral_impl< int> : true_type {}; 113template <> struct is_integral_impl<unsigned int> : true_type {}; 114template <> struct is_integral_impl< long> : true_type {}; 115template <> struct is_integral_impl<unsigned long> : true_type {}; 116template <> struct is_integral_impl< long long> : true_type {}; 117template <> struct is_integral_impl<unsigned long long> : true_type {}; 118 119/// \brief Metafunction that determines whether the given type is an integral 120/// type. 121template <typename T> 122struct is_integral : is_integral_impl<T> {}; 123 124/// \brief Metafunction that determines whether the given type is either an 125/// integral type or an enumeration type. 126/// 127/// Note that this accepts potentially more integral types than we whitelist 128/// above for is_integral, it should accept essentially anything the compiler 129/// believes is an integral type. 130template <typename T> class is_integral_or_enum { 131 132 // Form a return type that can only be instantiated with an integral or enum 133 // types (or with nullptr_t in C++11). 134 template <typename U, U u = U()> struct check1_return_type { char c[2]; }; 135 template <typename U> static check1_return_type<U> checker1(U*); 136 template <typename U> static char checker1(...); 137 138 // Form a return type that can only be instantiated with nullptr_t in C++11 139 // mode. It's harmless in C++98 mode, but this allows us to filter nullptr_t 140 // when building in C++11 mode without having to detect that mode for each 141 // different compiler. 142 struct nonce {}; 143 template <typename U, nonce* u = U()> 144 struct check2_return_type { char c[2]; }; 145 template <typename U> static check2_return_type<U> checker2(U*); 146 template <typename U> static char checker2(...); 147 148public: 149 enum { 150 value = (sizeof(char) != sizeof(checker1<T>(0)) && 151 sizeof(char) == sizeof(checker2<T>(0))) 152 }; 153}; 154 155/// \brief Metafunction that determines whether the given type is a pointer 156/// type. 157template <typename T> struct is_pointer : false_type {}; 158template <typename T> struct is_pointer<T*> : true_type {}; 159 160// enable_if_c - Enable/disable a template based on a metafunction 161template<bool Cond, typename T = void> 162struct enable_if_c { 163 typedef T type; 164}; 165 166template<typename T> struct enable_if_c<false, T> { }; 167 168// enable_if - Enable/disable a template based on a metafunction 169template<typename Cond, typename T = void> 170struct enable_if : public enable_if_c<Cond::value, T> { }; 171 172namespace dont_use { 173 template<typename Base> char base_of_helper(const volatile Base*); 174 template<typename Base> double base_of_helper(...); 175} 176 177/// is_base_of - Metafunction to determine whether one type is a base class of 178/// (or identical to) another type. 179template<typename Base, typename Derived> 180struct is_base_of { 181 static const bool value 182 = is_class<Base>::value && is_class<Derived>::value && 183 sizeof(char) == sizeof(dont_use::base_of_helper<Base>((Derived*)0)); 184}; 185 186// remove_pointer - Metafunction to turn Foo* into Foo. Defined in 187// C++0x [meta.trans.ptr]. 188template <typename T> struct remove_pointer { typedef T type; }; 189template <typename T> struct remove_pointer<T*> { typedef T type; }; 190template <typename T> struct remove_pointer<T*const> { typedef T type; }; 191template <typename T> struct remove_pointer<T*volatile> { typedef T type; }; 192template <typename T> struct remove_pointer<T*const volatile> { 193 typedef T type; }; 194 195template <bool, typename T, typename F> 196struct conditional { typedef T type; }; 197 198template <typename T, typename F> 199struct conditional<false, T, F> { typedef F type; }; 200 201} 202 203#endif 204