Upstream version 7.36.149.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 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     // 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; };
79
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> {
85         public:
86             static const bool value = false;
87         };
88
89         template<typename U> class IsConvertibleToDouble<true, U> {
90             typedef char YesType;
91             struct NoType {
92                 char padding[8];
93             };
94
95             static YesType floatCheck(long double);
96             static NoType floatCheck(...);
97             static T& t;
98         public:
99             static const bool value = sizeof(floatCheck(t)) == sizeof(YesType);
100         };
101
102     public:
103         static const bool value = IsInteger<T>::value || IsConvertibleToDouble<!IsInteger<T>::value, T>::value;
104     };
105
106     template<typename From, typename To> class IsPointerConvertible {
107         typedef char YesType;
108         struct NoType {
109             char padding[8];
110         };
111
112         static YesType convertCheck(To* x);
113         static NoType convertCheck(...);
114     public:
115         enum {
116             Value = (sizeof(YesType) == sizeof(convertCheck(static_cast<From*>(0))))
117         };
118     };
119
120     template <class T> struct IsArray {
121         static const bool value = false;
122     };
123
124     template <class T> struct IsArray<T[]> {
125         static const bool value = true;
126     };
127
128     template <class T, size_t N> struct IsArray<T[N]> {
129         static const bool value = true;
130     };
131
132
133     template <typename T, typename U> struct IsSameType {
134         static const bool value = false;
135     };
136
137     template <typename T> struct IsSameType<T, T> {
138         static const bool value = true;
139     };
140
141     template <typename T, typename U> class IsSubclass {
142         typedef char YesType;
143         struct NoType {
144             char padding[8];
145         };
146
147         static YesType subclassCheck(U*);
148         static NoType subclassCheck(...);
149         static T* t;
150     public:
151         static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
152     };
153
154     template <typename T, template<class V> class U> class IsSubclassOfTemplate {
155         typedef char YesType;
156         struct NoType {
157             char padding[8];
158         };
159
160         template<typename W> static YesType subclassCheck(U<W>*);
161         static NoType subclassCheck(...);
162         static T* t;
163     public:
164         static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
165     };
166
167     template <typename T, template<typename V, size_t W> class U> class IsSubclassOfTemplateTypenameSize {
168         typedef char YesType;
169         struct NoType {
170             char padding[8];
171         };
172
173         template<typename X, size_t Y> static YesType subclassCheck(U<X, Y>*);
174         static NoType subclassCheck(...);
175         static T* t;
176     public:
177         static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
178     };
179
180     template <typename T, template<typename V, size_t W, typename X> class U> class IsSubclassOfTemplateTypenameSizeTypename {
181         typedef char YesType;
182         struct NoType {
183             char padding[8];
184         };
185
186         template<typename Y, size_t Z, typename A> static YesType subclassCheck(U<Y, Z, A>*);
187         static NoType subclassCheck(...);
188         static T* t;
189     public:
190         static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
191     };
192
193     template <typename T, template<class A, class B, class C> class U> class IsSubclassOfTemplate3 {
194         typedef char YesType;
195         struct NoType {
196             char padding[8];
197         };
198
199         template<typename D, typename E, typename F> static YesType subclassCheck(U<D, E, F>*);
200         static NoType subclassCheck(...);
201         static T* t;
202     public:
203         static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
204     };
205
206     template <typename T, template<class A, class B, class C, class D, class E> class U> class IsSubclassOfTemplate5 {
207         typedef char YesType;
208         struct NoType {
209             char padding[8];
210         };
211
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(...);
214         static T* t;
215     public:
216         static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
217     };
218
219     template <typename T, template <class V> class OuterTemplate> struct RemoveTemplate {
220         typedef T Type;
221     };
222
223     template <typename T, template <class V> class OuterTemplate> struct RemoveTemplate<OuterTemplate<T>, OuterTemplate> {
224         typedef T Type;
225     };
226
227     template <typename T> struct RemoveConst {
228         typedef T Type;
229     };
230
231     template <typename T> struct RemoveConst<const T> {
232         typedef T Type;
233     };
234
235     template <typename T> struct RemoveVolatile {
236         typedef T Type;
237     };
238
239     template <typename T> struct RemoveVolatile<volatile T> {
240         typedef T Type;
241     };
242
243     template <typename T> struct RemoveConstVolatile {
244         typedef typename RemoveVolatile<typename RemoveConst<T>::Type>::Type Type;
245     };
246
247     template <typename T> struct RemovePointer {
248         typedef T Type;
249     };
250
251     template <typename T> struct RemovePointer<T*> {
252         typedef T Type;
253     };
254
255     template <typename T> struct RemoveReference {
256         typedef T Type;
257     };
258
259     template <typename T> struct RemoveReference<T&> {
260         typedef T Type;
261     };
262
263     template <typename T> struct RemoveExtent {
264         typedef T Type;
265     };
266
267     template <typename T> struct RemoveExtent<T[]> {
268         typedef T Type;
269     };
270
271     template <typename T, size_t N> struct RemoveExtent<T[N]> {
272         typedef T Type;
273     };
274
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
279
280 } // namespace WTF
281
282 namespace WebCore {
283
284 class JSONValue;
285
286 } // namespace WebCore
287
288 namespace WTF {
289
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> {
293     public:
294         enum {
295             Value = true
296         };
297     };
298
299 template<typename T>
300 class NeedsTracing {
301     typedef char YesType;
302     typedef struct NoType {
303         char padding[8];
304     } NoType;
305
306 #if COMPILER(MSVC)
307     template<typename V> static YesType checkHasTraceMethod(char[&V::trace != 0]);
308 #else
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(...);
313 public:
314     static const bool value = sizeof(YesType) == sizeof(checkHasTraceMethod<T>(0));
315 };
316
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 {
321 public:
322     static const bool value = Traits::template NeedsTracingLazily<>::value;
323 };
324
325 } // namespace WTF
326
327 #endif // TypeTraits_h