Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / wtf / TypeTraits.h
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 <utility>
26
27 namespace WTF {
28
29     // The following are provided in this file:
30     //
31     //   IsInteger<T>::value
32     //   IsPod<T>::value
33     //   IsConvertibleToInteger<T>::value
34     //
35     //   IsArray<T>::value
36     //
37     //   IsSameType<T, U>::value
38     //
39     //   RemovePointer<T>::Type
40     //   RemoveReference<T>::Type
41     //   RemoveConst<T>::Type
42     //   RemoveVolatile<T>::Type
43     //   RemoveConstVolatile<T>::Type
44     //   RemoveExtent<T>::Type
45     //
46     //   COMPILE_ASSERT's in TypeTraits.cpp illustrate their usage and what they do.
47
48     template<bool Predicate, class T = void> struct EnableIf;
49     template<class T> struct EnableIf<true, T> { typedef T Type; };
50
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; };
66 #endif
67
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; };
72
73     template<typename T> struct IsArithmetic        { static const bool value = IsInteger<T>::value || IsFloatingPoint<T>::value; };
74
75     template<typename T> struct IsPointer {
76         static const bool value = false;
77     };
78
79     template<typename P> struct IsPointer<const P*> {
80         static const bool value = true;
81     };
82
83     template<typename P> struct IsPointer<P*> {
84         static const bool value = true;
85     };
86
87     template<typename T> struct IsEnum {
88         static const bool value = __is_enum(T);
89     };
90
91     template<typename T> struct IsScalar {
92         static const bool value = IsEnum<T>::value || IsArithmetic<T>::value || IsPointer<T>::value;
93     };
94
95     template<typename T> struct IsWeak              { static const bool value = false; };
96
97     enum WeakHandlingFlag {
98         NoWeakHandlingInCollections,
99         WeakHandlingInCollections
100     };
101
102     template <typename T> struct IsPod {
103         static const bool value = __is_pod(T);
104     };
105
106     template <typename T> struct IsTriviallyCopyAssignable {
107         static const bool value = __has_trivial_assign(T);
108     };
109
110     template <typename T> struct IsTriviallyMoveAssignable {
111         static const bool value = __has_trivial_assign(T);
112     };
113
114     template <typename T> struct IsTriviallyDefaultConstructible {
115         static const bool value = __has_trivial_constructor(T);
116     };
117
118     template <typename T> struct IsTriviallyDestructible {
119         static const bool value = __has_trivial_destructor(T);
120     };
121
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> {
127         public:
128             static const bool value = false;
129         };
130
131         template<typename U> class IsConvertibleToDouble<true, U> {
132             typedef char YesType;
133             struct NoType {
134                 char padding[8];
135             };
136
137             static YesType floatCheck(long double);
138             static NoType floatCheck(...);
139             static T& t;
140         public:
141             static const bool value = sizeof(floatCheck(t)) == sizeof(YesType);
142         };
143
144     public:
145         static const bool value = IsInteger<T>::value || IsConvertibleToDouble<!IsInteger<T>::value, T>::value;
146     };
147
148     template<typename From, typename To> class IsPointerConvertible {
149         typedef char YesType;
150         struct NoType {
151             char padding[8];
152         };
153
154         static YesType convertCheck(To* x);
155         static NoType convertCheck(...);
156     public:
157         enum {
158             Value = (sizeof(YesType) == sizeof(convertCheck(static_cast<From*>(0))))
159         };
160     };
161
162     template <class T> struct IsArray {
163         static const bool value = false;
164     };
165
166     template <class T> struct IsArray<T[]> {
167         static const bool value = true;
168     };
169
170     template <class T, size_t N> struct IsArray<T[N]> {
171         static const bool value = true;
172     };
173
174
175     template <typename T, typename U> struct IsSameType {
176         static const bool value = false;
177     };
178
179     template <typename T> struct IsSameType<T, T> {
180         static const bool value = true;
181     };
182
183     template <typename T, typename U> class IsSubclass {
184         typedef char YesType;
185         struct NoType {
186             char padding[8];
187         };
188
189         static YesType subclassCheck(U*);
190         static NoType subclassCheck(...);
191         static T* t;
192     public:
193         static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
194     };
195
196     template <typename T, template<typename... V> class U> class IsSubclassOfTemplate {
197         typedef char YesType;
198         struct NoType {
199             char padding[8];
200         };
201
202         template<typename... W> static YesType subclassCheck(U<W...>*);
203         static NoType subclassCheck(...);
204         static T* t;
205     public:
206         static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
207     };
208
209     template <typename T, template<typename V, size_t W> class U> class IsSubclassOfTemplateTypenameSize {
210         typedef char YesType;
211         struct NoType {
212             char padding[8];
213         };
214
215         template<typename X, size_t Y> static YesType subclassCheck(U<X, Y>*);
216         static NoType subclassCheck(...);
217         static T* t;
218     public:
219         static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
220     };
221
222     template <typename T, template<typename V, size_t W, typename X> class U> class IsSubclassOfTemplateTypenameSizeTypename {
223         typedef char YesType;
224         struct NoType {
225             char padding[8];
226         };
227
228         template<typename Y, size_t Z, typename A> static YesType subclassCheck(U<Y, Z, A>*);
229         static NoType subclassCheck(...);
230         static T* t;
231     public:
232         static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
233     };
234
235     template <typename T, template <class V> class OuterTemplate> struct RemoveTemplate {
236         typedef T Type;
237     };
238
239     template <typename T, template <class V> class OuterTemplate> struct RemoveTemplate<OuterTemplate<T>, OuterTemplate> {
240         typedef T Type;
241     };
242
243     template <typename T> struct RemoveConst {
244         typedef T Type;
245     };
246
247     template <typename T> struct RemoveConst<const T> {
248         typedef T Type;
249     };
250
251     template <typename T> struct RemoveVolatile {
252         typedef T Type;
253     };
254
255     template <typename T> struct RemoveVolatile<volatile T> {
256         typedef T Type;
257     };
258
259     template <typename T> struct RemoveConstVolatile {
260         typedef typename RemoveVolatile<typename RemoveConst<T>::Type>::Type Type;
261     };
262
263     template <typename T> struct RemovePointer {
264         typedef T Type;
265     };
266
267     template <typename T> struct RemovePointer<T*> {
268         typedef T Type;
269     };
270
271     template <typename T> struct RemoveReference {
272         typedef T Type;
273     };
274
275     template <typename T> struct RemoveReference<T&> {
276         typedef T Type;
277     };
278
279     template <typename T> struct RemoveExtent {
280         typedef T Type;
281     };
282
283     template <typename T> struct RemoveExtent<T[]> {
284         typedef T Type;
285     };
286
287     template <typename T, size_t N> struct RemoveExtent<T[N]> {
288         typedef T Type;
289     };
290
291     // Determines whether this type has a vtable.
292     template <typename T> struct IsPolymorphic {
293         static const bool value = __is_polymorphic(T);
294     };
295
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
300
301 } // namespace WTF
302
303 namespace blink {
304
305 class JSONValue;
306
307 } // namespace blink
308
309 namespace WTF {
310
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> {
314     public:
315         enum {
316             Value = true
317         };
318     };
319
320 template<typename T>
321 class NeedsTracing {
322     typedef char YesType;
323     typedef struct NoType {
324         char padding[8];
325     } NoType;
326 #if COMPILER(MSVC)
327     template<typename V> static YesType checkHasTraceMethod(char[&V::trace != 0]);
328 #else
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(...);
333 public:
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);
338 };
339
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 {
344 public:
345     static const bool value = Traits::template NeedsTracingLazily<>::value;
346 };
347
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;
351 };
352
353 } // namespace WTF
354
355 #endif // TypeTraits_h