2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009, 2010 Google Inc. All rights reserved.
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.
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.
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.
29 // The following are provided in this file:
31 // IsInteger<T>::value
33 // IsConvertibleToInteger<T>::value
37 // IsSameType<T, U>::value
39 // RemovePointer<T>::Type
40 // RemoveReference<T>::Type
41 // RemoveConst<T>::Type
42 // RemoveVolatile<T>::Type
43 // RemoveConstVolatile<T>::Type
44 // RemoveExtent<T>::Type
46 // COMPILE_ASSERT's in TypeTraits.cpp illustrate their usage and what they do.
48 template<bool Predicate, class T = void> struct EnableIf;
49 template<class T> struct EnableIf<true, T> { typedef T Type; };
51 template<typename T> struct IsInteger { static const bool value = false; };
52 template<> struct IsInteger<bool> { static const bool value = true; };
53 template<> struct IsInteger<char> { static const bool value = true; };
54 template<> struct IsInteger<signed char> { static const bool value = true; };
55 template<> struct IsInteger<unsigned char> { static const bool value = true; };
56 template<> struct IsInteger<short> { static const bool value = true; };
57 template<> struct IsInteger<unsigned short> { static const bool value = true; };
58 template<> struct IsInteger<int> { static const bool value = true; };
59 template<> struct IsInteger<unsigned int> { static const bool value = true; };
60 template<> struct IsInteger<long> { static const bool value = true; };
61 template<> struct IsInteger<unsigned long> { static const bool value = true; };
62 template<> struct IsInteger<long long> { static const bool value = true; };
63 template<> struct IsInteger<unsigned long long> { static const bool value = true; };
64 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
65 template<> struct IsInteger<wchar_t> { static const bool value = true; };
68 template<typename T> struct IsFloatingPoint { static const bool value = false; };
69 template<> struct IsFloatingPoint<float> { static const bool value = true; };
70 template<> struct IsFloatingPoint<double> { static const bool value = true; };
71 template<> struct IsFloatingPoint<long double> { static const bool value = true; };
73 template<typename T> struct IsArithmetic { static const bool value = IsInteger<T>::value || IsFloatingPoint<T>::value; };
75 template<typename T> struct IsPointer {
76 static const bool value = false;
79 template<typename P> struct IsPointer<const P*> {
80 static const bool value = true;
83 template<typename P> struct IsPointer<P*> {
84 static const bool value = true;
87 template<typename T> struct IsEnum {
88 static const bool value = __is_enum(T);
91 template<typename T> struct IsScalar {
92 static const bool value = IsEnum<T>::value || IsArithmetic<T>::value || IsPointer<T>::value;
95 template<typename T> struct IsWeak { static const bool value = false; };
97 enum WeakHandlingFlag {
98 NoWeakHandlingInCollections,
99 WeakHandlingInCollections
102 template <typename T> struct IsPod {
103 static const bool value = __is_pod(T);
106 template <typename T> struct IsTriviallyCopyAssignable {
107 static const bool value = __has_trivial_assign(T);
110 template <typename T> struct IsTriviallyMoveAssignable {
111 static const bool value = __has_trivial_assign(T);
114 template <typename T> struct IsTriviallyDefaultConstructible {
115 static const bool value = __has_trivial_constructor(T);
118 template <typename T> struct IsTriviallyDestructible {
119 static const bool value = __has_trivial_destructor(T);
122 template<typename T> class IsConvertibleToInteger {
123 // Avoid "possible loss of data" warning when using Microsoft's C++ compiler
124 // by not converting int's to doubles.
125 template<bool performCheck, typename U> class IsConvertibleToDouble;
126 template<typename U> class IsConvertibleToDouble<false, U> {
128 static const bool value = false;
131 template<typename U> class IsConvertibleToDouble<true, U> {
132 typedef char YesType;
137 static YesType floatCheck(long double);
138 static NoType floatCheck(...);
141 static const bool value = sizeof(floatCheck(t)) == sizeof(YesType);
145 static const bool value = IsInteger<T>::value || IsConvertibleToDouble<!IsInteger<T>::value, T>::value;
148 template<typename From, typename To> class IsPointerConvertible {
149 typedef char YesType;
154 static YesType convertCheck(To* x);
155 static NoType convertCheck(...);
158 Value = (sizeof(YesType) == sizeof(convertCheck(static_cast<From*>(0))))
162 template <class T> struct IsArray {
163 static const bool value = false;
166 template <class T> struct IsArray<T[]> {
167 static const bool value = true;
170 template <class T, size_t N> struct IsArray<T[N]> {
171 static const bool value = true;
175 template <typename T, typename U> struct IsSameType {
176 static const bool value = false;
179 template <typename T> struct IsSameType<T, T> {
180 static const bool value = true;
183 template <typename T, typename U> class IsSubclass {
184 typedef char YesType;
189 static YesType subclassCheck(U*);
190 static NoType subclassCheck(...);
193 static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
196 template <typename T, template<typename... V> class U> class IsSubclassOfTemplate {
197 typedef char YesType;
202 template<typename... W> static YesType subclassCheck(U<W...>*);
203 static NoType subclassCheck(...);
206 static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
209 template <typename T, template<typename V, size_t W> class U> class IsSubclassOfTemplateTypenameSize {
210 typedef char YesType;
215 template<typename X, size_t Y> static YesType subclassCheck(U<X, Y>*);
216 static NoType subclassCheck(...);
219 static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
222 template <typename T, template<typename V, size_t W, typename X> class U> class IsSubclassOfTemplateTypenameSizeTypename {
223 typedef char YesType;
228 template<typename Y, size_t Z, typename A> static YesType subclassCheck(U<Y, Z, A>*);
229 static NoType subclassCheck(...);
232 static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
235 template <typename T, template <class V> class OuterTemplate> struct RemoveTemplate {
239 template <typename T, template <class V> class OuterTemplate> struct RemoveTemplate<OuterTemplate<T>, OuterTemplate> {
243 template <typename T> struct RemoveConst {
247 template <typename T> struct RemoveConst<const T> {
251 template <typename T> struct RemoveVolatile {
255 template <typename T> struct RemoveVolatile<volatile T> {
259 template <typename T> struct RemoveConstVolatile {
260 typedef typename RemoveVolatile<typename RemoveConst<T>::Type>::Type Type;
263 template <typename T> struct RemovePointer {
267 template <typename T> struct RemovePointer<T*> {
271 template <typename T> struct RemoveReference {
275 template <typename T> struct RemoveReference<T&> {
279 template <typename T> struct RemoveExtent {
283 template <typename T> struct RemoveExtent<T[]> {
287 template <typename T, size_t N> struct RemoveExtent<T[N]> {
291 // Determines whether this type has a vtable.
292 template <typename T> struct IsPolymorphic {
293 static const bool value = __is_polymorphic(T);
296 #define EnsurePtrConvertibleArgDecl(From, To) \
297 typename WTF::EnableIf<WTF::IsPointerConvertible<From, To>::Value, bool>::Type = true
298 #define EnsurePtrConvertibleArgDefn(From, To) \
299 typename WTF::EnableIf<WTF::IsPointerConvertible<From, To>::Value, bool>::Type
311 // FIXME: Disable pointer conversion checking against JSONValue.
312 // The current CodeGeneratorInspector.py generates code which upcasts to JSONValue from undefined types.
313 template<typename From> class IsPointerConvertible<From, blink::JSONValue> {
322 typedef char YesType;
323 typedef struct NoType {
327 template<typename V> static YesType checkHasTraceMethod(char[&V::trace != 0]);
329 template<size_t> struct HasMethod;
330 template<typename V> static YesType checkHasTraceMethod(HasMethod<sizeof(&V::trace)>*);
331 #endif // COMPILER(MSVC)
332 template<typename V> static NoType checkHasTraceMethod(...);
334 // We add sizeof(T) to both sides here, because we want it to fail for
335 // incomplete types. Otherwise it just assumes that incomplete types do not
336 // have a trace method, which may not be true.
337 static const bool value = sizeof(YesType) + sizeof(T) == sizeof(checkHasTraceMethod<T>(0)) + sizeof(T);
340 // Convenience template wrapping the NeedsTracingLazily template in
341 // Collection Traits. It helps make the code more readable.
342 template<typename Traits>
343 class ShouldBeTraced {
345 static const bool value = Traits::template NeedsTracingLazily<>::value;
348 template<typename T, typename U>
349 struct NeedsTracing<std::pair<T, U> > {
350 static const bool value = NeedsTracing<T>::value || NeedsTracing<U>::value || IsWeak<T>::value || IsWeak<U>::value;
355 #endif // TypeTraits_h