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 namespace WTF { 26 27 // The following are provided in this file: 28 // 29 // IsInteger<T>::value 30 // IsPod<T>::value, see the definition for a note about its limitations 31 // IsConvertibleToInteger<T>::value 32 // 33 // IsArray<T>::value 34 // 35 // IsSameType<T, U>::value 36 // 37 // RemovePointer<T>::Type 38 // RemoveReference<T>::Type 39 // RemoveConst<T>::Type 40 // RemoveVolatile<T>::Type 41 // RemoveConstVolatile<T>::Type 42 // RemoveExtent<T>::Type 43 // 44 // COMPILE_ASSERT's in TypeTraits.cpp illustrate their usage and what they do. 45 46 template<bool Predicate, class T = void> struct EnableIf; 47 template<class T> struct EnableIf<true, T> { typedef T Type; }; 48 49 template<typename T> struct IsInteger { static const bool value = false; }; 50 template<> struct IsInteger<bool> { static const bool value = true; }; 51 template<> struct IsInteger<char> { static const bool value = true; }; 52 template<> struct IsInteger<signed char> { static const bool value = true; }; 53 template<> struct IsInteger<unsigned char> { static const bool value = true; }; 54 template<> struct IsInteger<short> { static const bool value = true; }; 55 template<> struct IsInteger<unsigned short> { static const bool value = true; }; 56 template<> struct IsInteger<int> { static const bool value = true; }; 57 template<> struct IsInteger<unsigned int> { static const bool value = true; }; 58 template<> struct IsInteger<long> { static const bool value = true; }; 59 template<> struct IsInteger<unsigned long> { static const bool value = true; }; 60 template<> struct IsInteger<long long> { static const bool value = true; }; 61 template<> struct IsInteger<unsigned long long> { static const bool value = true; }; 62 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED) 63 template<> struct IsInteger<wchar_t> { static const bool value = true; }; 64 #endif 65 66 template<typename T> struct IsFloatingPoint { static const bool value = false; }; 67 template<> struct IsFloatingPoint<float> { static const bool value = true; }; 68 template<> struct IsFloatingPoint<double> { static const bool value = true; }; 69 template<> struct IsFloatingPoint<long double> { static const bool value = true; }; 70 71 template<typename T> struct IsArithmetic { static const bool value = IsInteger<T>::value || IsFloatingPoint<T>::value; }; 72 73 template<typename T> struct IsWeak { static const bool value = false; }; 74 75 enum WeakHandlingFlag { 76 NoWeakHandlingInCollections, 77 WeakHandlingInCollections 78 }; 79 80 // IsPod is misnamed as it doesn't cover all plain old data (pod) types. 81 // Specifically, it doesn't allow for enums or for structs. 82 template <typename T> struct IsPod { static const bool value = IsArithmetic<T>::value; }; 83 template <typename P> struct IsPod<P*> { static const bool value = true; }; 84 85 template<typename T> class IsConvertibleToInteger { 86 // Avoid "possible loss of data" warning when using Microsoft's C++ compiler 87 // by not converting int's to doubles. 88 template<bool performCheck, typename U> class IsConvertibleToDouble; 89 template<typename U> class IsConvertibleToDouble<false, U> { 90 public: 91 static const bool value = false; 92 }; 93 94 template<typename U> class IsConvertibleToDouble<true, U> { 95 typedef char YesType; 96 struct NoType { 97 char padding[8]; 98 }; 99 100 static YesType floatCheck(long double); 101 static NoType floatCheck(...); 102 static T& t; 103 public: 104 static const bool value = sizeof(floatCheck(t)) == sizeof(YesType); 105 }; 106 107 public: 108 static const bool value = IsInteger<T>::value || IsConvertibleToDouble<!IsInteger<T>::value, T>::value; 109 }; 110 111 template<typename From, typename To> class IsPointerConvertible { 112 typedef char YesType; 113 struct NoType { 114 char padding[8]; 115 }; 116 117 static YesType convertCheck(To* x); 118 static NoType convertCheck(...); 119 public: 120 enum { 121 Value = (sizeof(YesType) == sizeof(convertCheck(static_cast<From*>(0)))) 122 }; 123 }; 124 125 template <class T> struct IsArray { 126 static const bool value = false; 127 }; 128 129 template <class T> struct IsArray<T[]> { 130 static const bool value = true; 131 }; 132 133 template <class T, size_t N> struct IsArray<T[N]> { 134 static const bool value = true; 135 }; 136 137 138 template <typename T, typename U> struct IsSameType { 139 static const bool value = false; 140 }; 141 142 template <typename T> struct IsSameType<T, T> { 143 static const bool value = true; 144 }; 145 146 template <typename T, typename U> class IsSubclass { 147 typedef char YesType; 148 struct NoType { 149 char padding[8]; 150 }; 151 152 static YesType subclassCheck(U*); 153 static NoType subclassCheck(...); 154 static T* t; 155 public: 156 static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType); 157 }; 158 159 template <typename T, template<class V> class U> class IsSubclassOfTemplate { 160 typedef char YesType; 161 struct NoType { 162 char padding[8]; 163 }; 164 165 template<typename W> static YesType subclassCheck(U<W>*); 166 static NoType subclassCheck(...); 167 static T* t; 168 public: 169 static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType); 170 }; 171 172 template <typename T, template<typename V, size_t W> class U> class IsSubclassOfTemplateTypenameSize { 173 typedef char YesType; 174 struct NoType { 175 char padding[8]; 176 }; 177 178 template<typename X, size_t Y> static YesType subclassCheck(U<X, Y>*); 179 static NoType subclassCheck(...); 180 static T* t; 181 public: 182 static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType); 183 }; 184 185 template <typename T, template<typename V, size_t W, typename X> class U> class IsSubclassOfTemplateTypenameSizeTypename { 186 typedef char YesType; 187 struct NoType { 188 char padding[8]; 189 }; 190 191 template<typename Y, size_t Z, typename A> static YesType subclassCheck(U<Y, Z, A>*); 192 static NoType subclassCheck(...); 193 static T* t; 194 public: 195 static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType); 196 }; 197 198 template <typename T, template<class A, class B, class C> class U> class IsSubclassOfTemplate3 { 199 typedef char YesType; 200 struct NoType { 201 char padding[8]; 202 }; 203 204 template<typename D, typename E, typename F> static YesType subclassCheck(U<D, E, F>*); 205 static NoType subclassCheck(...); 206 static T* t; 207 public: 208 static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType); 209 }; 210 211 template <typename T, template<class A, class B, class C, class D, class E> class U> class IsSubclassOfTemplate5 { 212 typedef char YesType; 213 struct NoType { 214 char padding[8]; 215 }; 216 217 template<typename F, typename G, typename H, typename I, typename J> static YesType subclassCheck(U<F, G, H, I, J>*); 218 static NoType subclassCheck(...); 219 static T* t; 220 public: 221 static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType); 222 }; 223 224 template <typename T, template <class V> class OuterTemplate> struct RemoveTemplate { 225 typedef T Type; 226 }; 227 228 template <typename T, template <class V> class OuterTemplate> struct RemoveTemplate<OuterTemplate<T>, OuterTemplate> { 229 typedef T Type; 230 }; 231 232 template <typename T> struct RemoveConst { 233 typedef T Type; 234 }; 235 236 template <typename T> struct RemoveConst<const T> { 237 typedef T Type; 238 }; 239 240 template <typename T> struct RemoveVolatile { 241 typedef T Type; 242 }; 243 244 template <typename T> struct RemoveVolatile<volatile T> { 245 typedef T Type; 246 }; 247 248 template <typename T> struct RemoveConstVolatile { 249 typedef typename RemoveVolatile<typename RemoveConst<T>::Type>::Type Type; 250 }; 251 252 template <typename T> struct RemovePointer { 253 typedef T Type; 254 }; 255 256 template <typename T> struct RemovePointer<T*> { 257 typedef T Type; 258 }; 259 260 template <typename T> struct RemoveReference { 261 typedef T Type; 262 }; 263 264 template <typename T> struct RemoveReference<T&> { 265 typedef T Type; 266 }; 267 268 template <typename T> struct RemoveExtent { 269 typedef T Type; 270 }; 271 272 template <typename T> struct RemoveExtent<T[]> { 273 typedef T Type; 274 }; 275 276 template <typename T, size_t N> struct RemoveExtent<T[N]> { 277 typedef T Type; 278 }; 279 280 // Determines whether this type has a vtable. 281 template <typename T> struct IsPolymorphic { 282 static const bool value = __is_polymorphic(T); 283 }; 284 285 #define EnsurePtrConvertibleArgDecl(From, To) \ 286 typename WTF::EnableIf<WTF::IsPointerConvertible<From, To>::Value, bool>::Type = true 287 #define EnsurePtrConvertibleArgDefn(From, To) \ 288 typename WTF::EnableIf<WTF::IsPointerConvertible<From, To>::Value, bool>::Type 289 290 } // namespace WTF 291 292 namespace WebCore { 293 294 class JSONValue; 295 296 } // namespace WebCore 297 298 namespace WTF { 299 300 // FIXME: Disable pointer conversion checking against JSONValue. 301 // The current CodeGeneratorInspector.py generates code which upcasts to JSONValue from undefined types. 302 template<typename From> class IsPointerConvertible<From, WebCore::JSONValue> { 303 public: 304 enum { 305 Value = true 306 }; 307 }; 308 309 template<typename T> 310 class NeedsTracing { 311 typedef char YesType; 312 typedef struct NoType { 313 char padding[8]; 314 } NoType; 315 316 #if COMPILER(MSVC) 317 template<typename V> static YesType checkHasTraceMethod(char[&V::trace != 0]); 318 #else 319 template<size_t> struct HasMethod; 320 template<typename V> static YesType checkHasTraceMethod(HasMethod<sizeof(&V::trace)>*); 321 #endif // COMPILER(MSVC) 322 template<typename V> static NoType checkHasTraceMethod(...); 323 public: 324 static const bool value = sizeof(YesType) == sizeof(checkHasTraceMethod<T>(0)); 325 }; 326 327 // Convenience template wrapping the NeedsTracingLazily template in 328 // Collection Traits. It helps make the code more readable. 329 template<typename Traits> 330 class ShouldBeTraced { 331 public: 332 static const bool value = Traits::template NeedsTracingLazily<>::value; 333 }; 334 335 } // namespace WTF 336 337 #endif // TypeTraits_h 338