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.
27 // The following are provided in this file:
29 // IsInteger<T>::value
30 // IsPod<T>::value, see the definition for a note about its limitations
31 // IsConvertibleToInteger<T>::value
35 // IsSameType<T, U>::value
37 // RemovePointer<T>::Type
38 // RemoveReference<T>::Type
39 // RemoveConst<T>::Type
40 // RemoveVolatile<T>::Type
41 // RemoveConstVolatile<T>::Type
42 // RemoveExtent<T>::Type
44 // COMPILE_ASSERT's in TypeTraits.cpp illustrate their usage and what they do.
46 template<bool Predicate, class T = void> struct EnableIf;
47 template<class T> struct EnableIf<true, T> { typedef T Type; };
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; };
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; };
71 template<typename T> struct IsArithmetic { static const bool value = IsInteger<T>::value || IsFloatingPoint<T>::value; };
73 template<typename T> struct IsWeak { static const bool value = false; };
75 // IsPod is misnamed as it doesn't cover all plain old data (pod) types.
76 // Specifically, it doesn't allow for enums or for structs.
77 template <typename T> struct IsPod { static const bool value = IsArithmetic<T>::value; };
78 template <typename P> struct IsPod<P*> { static const bool value = true; };
80 template<typename T> class IsConvertibleToInteger {
81 // Avoid "possible loss of data" warning when using Microsoft's C++ compiler
82 // by not converting int's to doubles.
83 template<bool performCheck, typename U> class IsConvertibleToDouble;
84 template<typename U> class IsConvertibleToDouble<false, U> {
86 static const bool value = false;
89 template<typename U> class IsConvertibleToDouble<true, U> {
95 static YesType floatCheck(long double);
96 static NoType floatCheck(...);
99 static const bool value = sizeof(floatCheck(t)) == sizeof(YesType);
103 static const bool value = IsInteger<T>::value || IsConvertibleToDouble<!IsInteger<T>::value, T>::value;
106 template<typename From, typename To> class IsPointerConvertible {
107 typedef char YesType;
112 static YesType convertCheck(To* x);
113 static NoType convertCheck(...);
116 Value = (sizeof(YesType) == sizeof(convertCheck(static_cast<From*>(0))))
120 template <class T> struct IsArray {
121 static const bool value = false;
124 template <class T> struct IsArray<T[]> {
125 static const bool value = true;
128 template <class T, size_t N> struct IsArray<T[N]> {
129 static const bool value = true;
133 template <typename T, typename U> struct IsSameType {
134 static const bool value = false;
137 template <typename T> struct IsSameType<T, T> {
138 static const bool value = true;
141 template <typename T, typename U> class IsSubclass {
142 typedef char YesType;
147 static YesType subclassCheck(U*);
148 static NoType subclassCheck(...);
151 static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
154 template <typename T, template<class V> class U> class IsSubclassOfTemplate {
155 typedef char YesType;
160 template<typename W> static YesType subclassCheck(U<W>*);
161 static NoType subclassCheck(...);
164 static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
167 template <typename T, template<typename V, size_t W> class U> class IsSubclassOfTemplateTypenameSize {
168 typedef char YesType;
173 template<typename X, size_t Y> static YesType subclassCheck(U<X, Y>*);
174 static NoType subclassCheck(...);
177 static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
180 template <typename T, template<typename V, size_t W, typename X> class U> class IsSubclassOfTemplateTypenameSizeTypename {
181 typedef char YesType;
186 template<typename Y, size_t Z, typename A> static YesType subclassCheck(U<Y, Z, A>*);
187 static NoType subclassCheck(...);
190 static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
193 template <typename T, template<class A, class B, class C> class U> class IsSubclassOfTemplate3 {
194 typedef char YesType;
199 template<typename D, typename E, typename F> static YesType subclassCheck(U<D, E, F>*);
200 static NoType subclassCheck(...);
203 static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
206 template <typename T, template<class A, class B, class C, class D, class E> class U> class IsSubclassOfTemplate5 {
207 typedef char YesType;
212 template<typename F, typename G, typename H, typename I, typename J> static YesType subclassCheck(U<F, G, H, I, J>*);
213 static NoType subclassCheck(...);
216 static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
219 template <typename T, template <class V> class OuterTemplate> struct RemoveTemplate {
223 template <typename T, template <class V> class OuterTemplate> struct RemoveTemplate<OuterTemplate<T>, OuterTemplate> {
227 template <typename T> struct RemoveConst {
231 template <typename T> struct RemoveConst<const T> {
235 template <typename T> struct RemoveVolatile {
239 template <typename T> struct RemoveVolatile<volatile T> {
243 template <typename T> struct RemoveConstVolatile {
244 typedef typename RemoveVolatile<typename RemoveConst<T>::Type>::Type Type;
247 template <typename T> struct RemovePointer {
251 template <typename T> struct RemovePointer<T*> {
255 template <typename T> struct RemoveReference {
259 template <typename T> struct RemoveReference<T&> {
263 template <typename T> struct RemoveExtent {
267 template <typename T> struct RemoveExtent<T[]> {
271 template <typename T, size_t N> struct RemoveExtent<T[N]> {
275 #define EnsurePtrConvertibleArgDecl(From, To) \
276 typename WTF::EnableIf<WTF::IsPointerConvertible<From, To>::Value, bool>::Type = true
277 #define EnsurePtrConvertibleArgDefn(From, To) \
278 typename WTF::EnableIf<WTF::IsPointerConvertible<From, To>::Value, bool>::Type
286 } // namespace WebCore
290 // FIXME: Disable pointer conversion checking against JSONValue.
291 // The current CodeGeneratorInspector.py generates code which upcasts to JSONValue from undefined types.
292 template<typename From> class IsPointerConvertible<From, WebCore::JSONValue> {
301 typedef char YesType;
302 typedef struct NoType {
307 template<typename V> static YesType checkHasTraceMethod(char[&V::trace != 0]);
309 template<size_t> struct HasMethod;
310 template<typename V> static YesType checkHasTraceMethod(HasMethod<sizeof(&V::trace)>*);
311 #endif // COMPILER(MSVC)
312 template<typename V> static NoType checkHasTraceMethod(...);
314 static const bool value = sizeof(YesType) == sizeof(checkHasTraceMethod<T>(0));
317 // Convenience template wrapping the NeedsTracingLazily template in
318 // Collection Traits. It helps make the code more readable.
319 template<typename Traits>
320 class ShouldBeTraced {
322 static const bool value = Traits::template NeedsTracingLazily<>::value;
327 #endif // TypeTraits_h