| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. | |
| 3 * Copyright (C) 2009, 2010 Google Inc. All rights reserved. | |
| 4 * | |
| 5 * This library is free software; you can redistribute it and/or | |
| 6 * modify it under the terms of the GNU Library General Public | |
| 7 * License as published by the Free Software Foundation; either | |
| 8 * version 2 of the License, or (at your option) any later version. | |
| 9 * | |
| 10 * This library is distributed in the hope that it will be useful, | |
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 * Library General Public License for more details. | |
| 14 * | |
| 15 * You should have received a copy of the GNU Library General Public License | |
| 16 * along with this library; see the file COPYING.LIB. If not, write to | |
| 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
| 18 * Boston, MA 02110-1301, USA. | |
| 19 * | |
| 20 */ | |
| 21 | |
| 22 #ifndef TypeTraits_h | |
| 23 #define TypeTraits_h | |
| 24 | |
| 25 #include <wtf/Platform.h> | |
| 26 | |
| 27 #if (defined(__GLIBCXX__) && (__GLIBCXX__ >= 20070724) && defined(__GXX_EXPERIME
NTAL_CXX0X__)) || (defined(_MSC_VER) && (_MSC_VER >= 1600)) | |
| 28 #include <type_traits> | |
| 29 #if defined(__GLIBCXX__) && (__GLIBCXX__ >= 20070724) && defined(__GXX_EXPERIMEN
TAL_CXX0X__) | |
| 30 #include <tr1/memory> | |
| 31 #endif | |
| 32 #endif | |
| 33 | |
| 34 namespace WTF { | |
| 35 | |
| 36 // The following are provided in this file: | |
| 37 // | |
| 38 // Conditional<Predicate, If, Then>::Type | |
| 39 // | |
| 40 // IsInteger<T>::value | |
| 41 // IsPod<T>::value, see the definition for a note about its limitations | |
| 42 // IsConvertibleToInteger<T>::value | |
| 43 // | |
| 44 // IsArray<T>::value | |
| 45 // | |
| 46 // IsSameType<T, U>::value | |
| 47 // | |
| 48 // RemovePointer<T>::Type | |
| 49 // RemoveReference<T>::Type | |
| 50 // RemoveConst<T>::Type | |
| 51 // RemoveVolatile<T>::Type | |
| 52 // RemoveConstVolatile<T>::Type | |
| 53 // RemoveExtent<T>::Type | |
| 54 // | |
| 55 // DecayArray<T>::Type | |
| 56 // | |
| 57 // COMPILE_ASSERT's in TypeTraits.cpp illustrate their usage and what they
do. | |
| 58 | |
| 59 template <bool Predicate, class If, class Then> struct Conditional { typede
f If Type; }; | |
| 60 template <class If, class Then> struct Conditional<false, If, Then> { typede
f Then Type; }; | |
| 61 | |
| 62 template<typename T> struct IsInteger { static const bool value =
false; }; | |
| 63 template<> struct IsInteger<bool> { static const bool value =
true; }; | |
| 64 template<> struct IsInteger<char> { static const bool value =
true; }; | |
| 65 template<> struct IsInteger<signed char> { static const bool value =
true; }; | |
| 66 template<> struct IsInteger<unsigned char> { static const bool value =
true; }; | |
| 67 template<> struct IsInteger<short> { static const bool value =
true; }; | |
| 68 template<> struct IsInteger<unsigned short> { static const bool value =
true; }; | |
| 69 template<> struct IsInteger<int> { static const bool value =
true; }; | |
| 70 template<> struct IsInteger<unsigned int> { static const bool value =
true; }; | |
| 71 template<> struct IsInteger<long> { static const bool value =
true; }; | |
| 72 template<> struct IsInteger<unsigned long> { static const bool value =
true; }; | |
| 73 template<> struct IsInteger<long long> { static const bool value =
true; }; | |
| 74 template<> struct IsInteger<unsigned long long> { static const bool value =
true; }; | |
| 75 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED) | |
| 76 template<> struct IsInteger<wchar_t> { static const bool value =
true; }; | |
| 77 #endif | |
| 78 | |
| 79 template<typename T> struct IsFloatingPoint { static const bool value =
false; }; | |
| 80 template<> struct IsFloatingPoint<float> { static const bool value =
true; }; | |
| 81 template<> struct IsFloatingPoint<double> { static const bool value =
true; }; | |
| 82 template<> struct IsFloatingPoint<long double> { static const bool value =
true; }; | |
| 83 | |
| 84 template<typename T> struct IsArithmetic { static const bool value = IsI
nteger<T>::value || IsFloatingPoint<T>::value; }; | |
| 85 | |
| 86 // IsPod is misnamed as it doesn't cover all plain old data (pod) types. | |
| 87 // Specifically, it doesn't allow for enums or for structs. | |
| 88 template <typename T> struct IsPod { static const bool value = IsA
rithmetic<T>::value; }; | |
| 89 template <typename P> struct IsPod<P*> { static const bool value = tru
e; }; | |
| 90 | |
| 91 template<typename T> class IsConvertibleToInteger { | |
| 92 // Avoid "possible loss of data" warning when using Microsoft's C++ comp
iler | |
| 93 // by not converting int's to doubles. | |
| 94 template<bool performCheck, typename U> class IsConvertibleToDouble; | |
| 95 template<typename U> class IsConvertibleToDouble<false, U> { | |
| 96 public: | |
| 97 static const bool value = false; | |
| 98 }; | |
| 99 | |
| 100 template<typename U> class IsConvertibleToDouble<true, U> { | |
| 101 typedef char YesType; | |
| 102 struct NoType { | |
| 103 char padding[8]; | |
| 104 }; | |
| 105 | |
| 106 static YesType floatCheck(long double); | |
| 107 static NoType floatCheck(...); | |
| 108 static T& t; | |
| 109 public: | |
| 110 static const bool value = sizeof(floatCheck(t)) == sizeof(YesType); | |
| 111 }; | |
| 112 | |
| 113 public: | |
| 114 static const bool value = IsInteger<T>::value || IsConvertibleToDouble<!
IsInteger<T>::value, T>::value; | |
| 115 }; | |
| 116 | |
| 117 | |
| 118 template <class T> struct IsArray { | |
| 119 static const bool value = false; | |
| 120 }; | |
| 121 | |
| 122 template <class T> struct IsArray<T[]> { | |
| 123 static const bool value = true; | |
| 124 }; | |
| 125 | |
| 126 template <class T, size_t N> struct IsArray<T[N]> { | |
| 127 static const bool value = true; | |
| 128 }; | |
| 129 | |
| 130 | |
| 131 template <typename T, typename U> struct IsSameType { | |
| 132 static const bool value = false; | |
| 133 }; | |
| 134 | |
| 135 template <typename T> struct IsSameType<T, T> { | |
| 136 static const bool value = true; | |
| 137 }; | |
| 138 | |
| 139 template <typename T, typename U> class IsSubclass { | |
| 140 typedef char YesType; | |
| 141 struct NoType { | |
| 142 char padding[8]; | |
| 143 }; | |
| 144 | |
| 145 static YesType subclassCheck(U*); | |
| 146 static NoType subclassCheck(...); | |
| 147 static T* t; | |
| 148 public: | |
| 149 static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType); | |
| 150 }; | |
| 151 | |
| 152 template <typename T, template<class V> class U> class IsSubclassOfTemplate
{ | |
| 153 typedef char YesType; | |
| 154 struct NoType { | |
| 155 char padding[8]; | |
| 156 }; | |
| 157 | |
| 158 template<typename W> static YesType subclassCheck(U<W>*); | |
| 159 static NoType subclassCheck(...); | |
| 160 static T* t; | |
| 161 public: | |
| 162 static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType); | |
| 163 }; | |
| 164 | |
| 165 template <typename T, template <class V> class OuterTemplate> struct RemoveT
emplate { | |
| 166 typedef T Type; | |
| 167 }; | |
| 168 | |
| 169 template <typename T, template <class V> class OuterTemplate> struct RemoveT
emplate<OuterTemplate<T>, OuterTemplate> { | |
| 170 typedef T Type; | |
| 171 }; | |
| 172 | |
| 173 template <typename T> struct RemoveConst { | |
| 174 typedef T Type; | |
| 175 }; | |
| 176 | |
| 177 template <typename T> struct RemoveConst<const T> { | |
| 178 typedef T Type; | |
| 179 }; | |
| 180 | |
| 181 template <typename T> struct RemoveVolatile { | |
| 182 typedef T Type; | |
| 183 }; | |
| 184 | |
| 185 template <typename T> struct RemoveVolatile<volatile T> { | |
| 186 typedef T Type; | |
| 187 }; | |
| 188 | |
| 189 template <typename T> struct RemoveConstVolatile { | |
| 190 typedef typename RemoveVolatile<typename RemoveConst<T>::Type>::Type Typ
e; | |
| 191 }; | |
| 192 | |
| 193 template <typename T> struct RemovePointer { | |
| 194 typedef T Type; | |
| 195 }; | |
| 196 | |
| 197 template <typename T> struct RemovePointer<T*> { | |
| 198 typedef T Type; | |
| 199 }; | |
| 200 | |
| 201 template <typename T> struct RemoveReference { | |
| 202 typedef T Type; | |
| 203 }; | |
| 204 | |
| 205 template <typename T> struct RemoveReference<T&> { | |
| 206 typedef T Type; | |
| 207 }; | |
| 208 | |
| 209 template <typename T> struct RemoveExtent { | |
| 210 typedef T Type; | |
| 211 }; | |
| 212 | |
| 213 template <typename T> struct RemoveExtent<T[]> { | |
| 214 typedef T Type; | |
| 215 }; | |
| 216 | |
| 217 template <typename T, size_t N> struct RemoveExtent<T[N]> { | |
| 218 typedef T Type; | |
| 219 }; | |
| 220 | |
| 221 template <class T> struct DecayArray { | |
| 222 typedef typename RemoveReference<T>::Type U; | |
| 223 public: | |
| 224 typedef typename Conditional< | |
| 225 IsArray<U>::value, | |
| 226 typename RemoveExtent<U>::Type*, | |
| 227 typename RemoveConstVolatile<U>::Type | |
| 228 >::Type Type; | |
| 229 }; | |
| 230 | |
| 231 #if COMPILER(CLANG) || GCC_VERSION_AT_LEAST(4, 6, 0) || (defined(_MSC_VER) && (_
MSC_VER >= 1400) && (_MSC_VER < 1600) && !defined(__INTEL_COMPILER)) | |
| 232 // VC8 (VS2005) and later has __has_trivial_constructor and __has_trivial_de
structor, | |
| 233 // but the implementation returns false for built-in types. We add the extra
IsPod condition to | |
| 234 // work around this. | |
| 235 template <typename T> struct HasTrivialConstructor { | |
| 236 static const bool value = __has_trivial_constructor(T) || IsPod<RemoveCo
nstVolatile<T> >::value; | |
| 237 }; | |
| 238 template <typename T> struct HasTrivialDestructor { | |
| 239 static const bool value = __has_trivial_destructor(T) || IsPod<RemoveCon
stVolatile<T> >::value; | |
| 240 }; | |
| 241 #elif (defined(__GLIBCXX__) && (__GLIBCXX__ >= 20070724) && defined(__GXX_EXPERI
MENTAL_CXX0X__)) || (defined(_MSC_VER) && (_MSC_VER >= 1600)) | |
| 242 // GCC's libstdc++ 20070724 and later supports C++ TR1 type_traits in the st
d namespace. | |
| 243 // VC10 (VS2010) and later support C++ TR1 type_traits in the std::tr1 names
pace. | |
| 244 template<typename T> struct HasTrivialConstructor : public std::tr1::has_tri
vial_constructor<T> { }; | |
| 245 template<typename T> struct HasTrivialDestructor : public std::tr1::has_triv
ial_destructor<T> { }; | |
| 246 #else | |
| 247 // For compilers that don't support detection of trivial constructors and de
structors in classes, | |
| 248 // we use a template that returns true for any POD type that IsPod can detec
t (see IsPod caveats above), | |
| 249 // but false for all other types (which includes all classes). This will giv
e false negatives, which can hurt | |
| 250 // performance, but avoids false positives, which would result in incorrect
behavior. | |
| 251 template <typename T> struct HasTrivialConstructor { | |
| 252 static const bool value = IsPod<RemoveConstVolatile<T> >::value; | |
| 253 }; | |
| 254 template <typename T> struct HasTrivialDestructor { | |
| 255 static const bool value = IsPod<RemoveConstVolatile<T> >::value; | |
| 256 }; | |
| 257 #endif | |
| 258 | |
| 259 } // namespace WTF | |
| 260 | |
| 261 #endif // TypeTraits_h | |
| OLD | NEW |