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.
25
#include <wtf/Platform.h>
27
#if (defined(__GLIBCXX__) && (__GLIBCXX__ >= 20070724) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || (defined(_MSC_VER) && (_MSC_VER >= 1600))
28
#include <type_traits>
29
#if defined(__GLIBCXX__) && (__GLIBCXX__ >= 20070724) && defined(__GXX_EXPERIMENTAL_CXX0X__)
36
// The following are provided in this file:
38
// Conditional<Predicate, If, Then>::Type
40
// IsInteger<T>::value
41
// IsPod<T>::value, see the definition for a note about its limitations
42
// IsConvertibleToInteger<T>::value
46
// IsSameType<T, U>::value
48
// RemovePointer<T>::Type
49
// RemoveReference<T>::Type
50
// RemoveConst<T>::Type
51
// RemoveVolatile<T>::Type
52
// RemoveConstVolatile<T>::Type
53
// RemoveExtent<T>::Type
55
// DecayArray<T>::Type
57
// COMPILE_ASSERT's in TypeTraits.cpp illustrate their usage and what they do.
59
template <bool Predicate, class If, class Then> struct Conditional { typedef If Type; };
60
template <class If, class Then> struct Conditional<false, If, Then> { typedef Then Type; };
62
template<typename T> struct IsInteger { static const bool value = false; };
63
template<> struct IsInteger<bool> { static const bool value = true; };
64
template<> struct IsInteger<char> { static const bool value = true; };
65
template<> struct IsInteger<signed char> { static const bool value = true; };
66
template<> struct IsInteger<unsigned char> { static const bool value = true; };
67
template<> struct IsInteger<short> { static const bool value = true; };
68
template<> struct IsInteger<unsigned short> { static const bool value = true; };
69
template<> struct IsInteger<int> { static const bool value = true; };
70
template<> struct IsInteger<unsigned int> { static const bool value = true; };
71
template<> struct IsInteger<long> { static const bool value = true; };
72
template<> struct IsInteger<unsigned long> { static const bool value = true; };
73
template<> struct IsInteger<long long> { static const bool value = true; };
74
template<> struct IsInteger<unsigned long long> { static const bool value = true; };
75
#if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
76
template<> struct IsInteger<wchar_t> { static const bool value = true; };
79
template<typename T> struct IsFloatingPoint { static const bool value = false; };
80
template<> struct IsFloatingPoint<float> { static const bool value = true; };
81
template<> struct IsFloatingPoint<double> { static const bool value = true; };
82
template<> struct IsFloatingPoint<long double> { static const bool value = true; };
84
template<typename T> struct IsArithmetic { static const bool value = IsInteger<T>::value || IsFloatingPoint<T>::value; };
86
// IsPod is misnamed as it doesn't cover all plain old data (pod) types.
87
// Specifically, it doesn't allow for enums or for structs.
88
template <typename T> struct IsPod { static const bool value = IsArithmetic<T>::value; };
89
template <typename P> struct IsPod<P*> { static const bool value = true; };
91
template<typename T> class IsConvertibleToInteger {
92
// Avoid "possible loss of data" warning when using Microsoft's C++ compiler
93
// by not converting int's to doubles.
94
template<bool performCheck, typename U> class IsConvertibleToDouble;
95
template<typename U> class IsConvertibleToDouble<false, U> {
97
static const bool value = false;
100
template<typename U> class IsConvertibleToDouble<true, U> {
101
typedef char YesType;
106
static YesType floatCheck(long double);
107
static NoType floatCheck(...);
110
static const bool value = sizeof(floatCheck(t)) == sizeof(YesType);
114
static const bool value = IsInteger<T>::value || IsConvertibleToDouble<!IsInteger<T>::value, T>::value;
118
template <class T> struct IsArray {
119
static const bool value = false;
122
template <class T> struct IsArray<T[]> {
123
static const bool value = true;
126
template <class T, size_t N> struct IsArray<T[N]> {
127
static const bool value = true;
131
template <typename T, typename U> struct IsSameType {
132
static const bool value = false;
135
template <typename T> struct IsSameType<T, T> {
136
static const bool value = true;
139
template <typename T, typename U> class IsSubclass {
140
typedef char YesType;
145
static YesType subclassCheck(U*);
146
static NoType subclassCheck(...);
149
static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
152
template <typename T, template<class V> class U> class IsSubclassOfTemplate {
153
typedef char YesType;
158
template<typename W> static YesType subclassCheck(U<W>*);
159
static NoType subclassCheck(...);
162
static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType);
165
template <typename T, template <class V> class OuterTemplate> struct RemoveTemplate {
169
template <typename T, template <class V> class OuterTemplate> struct RemoveTemplate<OuterTemplate<T>, OuterTemplate> {
173
template <typename T> struct RemoveConst {
177
template <typename T> struct RemoveConst<const T> {
181
template <typename T> struct RemoveVolatile {
185
template <typename T> struct RemoveVolatile<volatile T> {
189
template <typename T> struct RemoveConstVolatile {
190
typedef typename RemoveVolatile<typename RemoveConst<T>::Type>::Type Type;
193
template <typename T> struct RemovePointer {
197
template <typename T> struct RemovePointer<T*> {
201
template <typename T> struct RemoveReference {
205
template <typename T> struct RemoveReference<T&> {
209
template <typename T> struct RemoveExtent {
213
template <typename T> struct RemoveExtent<T[]> {
217
template <typename T, size_t N> struct RemoveExtent<T[N]> {
221
template <class T> struct DecayArray {
222
typedef typename RemoveReference<T>::Type U;
224
typedef typename Conditional<
226
typename RemoveExtent<U>::Type*,
227
typename RemoveConstVolatile<U>::Type
231
#if COMPILER(CLANG) || GCC_VERSION_AT_LEAST(4, 6, 0) || (defined(_MSC_VER) && (_MSC_VER >= 1400) && (_MSC_VER < 1600) && !defined(__INTEL_COMPILER))
232
// VC8 (VS2005) and later has __has_trivial_constructor and __has_trivial_destructor,
233
// but the implementation returns false for built-in types. We add the extra IsPod condition to
235
template <typename T> struct HasTrivialConstructor {
236
static const bool value = __has_trivial_constructor(T) || IsPod<RemoveConstVolatile<T> >::value;
238
template <typename T> struct HasTrivialDestructor {
239
static const bool value = __has_trivial_destructor(T) || IsPod<RemoveConstVolatile<T> >::value;
241
#elif (defined(__GLIBCXX__) && (__GLIBCXX__ >= 20070724) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || (defined(_MSC_VER) && (_MSC_VER >= 1600))
242
// GCC's libstdc++ 20070724 and later supports C++ TR1 type_traits in the std namespace.
243
// VC10 (VS2010) and later support C++ TR1 type_traits in the std::tr1 namespace.
244
template<typename T> struct HasTrivialConstructor : public std::tr1::has_trivial_constructor<T> { };
245
template<typename T> struct HasTrivialDestructor : public std::tr1::has_trivial_destructor<T> { };
247
// For compilers that don't support detection of trivial constructors and destructors in classes,
248
// we use a template that returns true for any POD type that IsPod can detect (see IsPod caveats above),
249
// but false for all other types (which includes all classes). This will give false negatives, which can hurt
250
// performance, but avoids false positives, which would result in incorrect behavior.
251
template <typename T> struct HasTrivialConstructor {
252
static const bool value = IsPod<RemoveConstVolatile<T> >::value;
254
template <typename T> struct HasTrivialDestructor {
255
static const bool value = IsPod<RemoveConstVolatile<T> >::value;
261
#endif // TypeTraits_h