/* * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. * Copyright (C) 2009, 2010 Google Inc. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * */ #ifndef TypeTraits_h #define TypeTraits_h #include "Platform.h" #if (defined(__GLIBCXX__) && (__GLIBCXX__ >= 20070724) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || (defined(_MSC_VER) && (_MSC_VER >= 1600)) #include #if defined(__GLIBCXX__) && (__GLIBCXX__ >= 20070724) && defined(__GXX_EXPERIMENTAL_CXX0X__) #include #endif #endif namespace WTF { // The following are provided in this file: // // IsInteger::value // IsPod::value, see the definition for a note about its limitations // IsConvertibleToInteger::value // // IsSameType::value // // RemovePointer::Type // RemoveConst::Type // RemoveVolatile::Type // RemoveConstVolatile::Type // // COMPILE_ASSERT's in TypeTraits.cpp illustrate their usage and what they do. template struct IsInteger { static const bool value = false; }; template<> struct IsInteger { static const bool value = true; }; template<> struct IsInteger { static const bool value = true; }; template<> struct IsInteger { static const bool value = true; }; template<> struct IsInteger { static const bool value = true; }; template<> struct IsInteger { static const bool value = true; }; template<> struct IsInteger { static const bool value = true; }; template<> struct IsInteger { static const bool value = true; }; template<> struct IsInteger { static const bool value = true; }; template<> struct IsInteger { static const bool value = true; }; template<> struct IsInteger { static const bool value = true; }; template<> struct IsInteger { static const bool value = true; }; template<> struct IsInteger { static const bool value = true; }; #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED) template<> struct IsInteger { static const bool value = true; }; #endif // IsPod is misnamed as it doesn't cover all plain old data (pod) types. // Specifically, it doesn't allow for enums or for structs. template struct IsPod { static const bool value = IsInteger::value; }; template <> struct IsPod { static const bool value = true; }; template <> struct IsPod { static const bool value = true; }; template <> struct IsPod { static const bool value = true; }; template struct IsPod { static const bool value = true; }; // Avoid "possible loss of data" warning when using Microsoft's C++ compiler // by not converting int's to doubles. template class CheckedIsConvertibleToDouble; template class CheckedIsConvertibleToDouble { public: static const bool value = false; }; template class CheckedIsConvertibleToDouble { typedef char YesType; struct NoType { char padding[8]; }; static YesType floatCheck(long double); static NoType floatCheck(...); static U& t; public: static const bool value = sizeof(floatCheck(t)) == sizeof(YesType); }; template class IsConvertibleToInteger { public: static const bool value = IsInteger::value || CheckedIsConvertibleToDouble::value, T>::value; }; template struct IsSameType { static const bool value = false; }; template struct IsSameType { static const bool value = true; }; template class IsSubclass { typedef char YesType; struct NoType { char padding[8]; }; static YesType subclassCheck(U*); static NoType subclassCheck(...); static T* t; public: static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType); }; template class U> class IsSubclassOfTemplate { typedef char YesType; struct NoType { char padding[8]; }; template static YesType subclassCheck(U*); static NoType subclassCheck(...); static T* t; public: static const bool value = sizeof(subclassCheck(t)) == sizeof(YesType); }; template class OuterTemplate> struct RemoveTemplate { typedef T Type; }; template class OuterTemplate> struct RemoveTemplate, OuterTemplate> { typedef T Type; }; template struct RemoveConst { typedef T Type; }; template struct RemoveConst { typedef T Type; }; template struct RemoveVolatile { typedef T Type; }; template struct RemoveVolatile { typedef T Type; }; template struct RemoveConstVolatile { typedef typename RemoveVolatile::Type>::Type Type; }; template struct RemovePointer { typedef T Type; }; template struct RemovePointer { typedef T Type; }; #if COMPILER(CLANG) || GCC_VERSION_AT_LEAST(4, 6, 0) || (defined(_MSC_VER) && (_MSC_VER >= 1400) && (_MSC_VER < 1600) && !defined(__INTEL_COMPILER)) // VC8 (VS2005) and later has __has_trivial_constructor and __has_trivial_destructor, // but the implementation returns false for built-in types. We add the extra IsPod condition to // work around this. template struct HasTrivialConstructor { static const bool value = __has_trivial_constructor(T) || IsPod >::value; }; template struct HasTrivialDestructor { static const bool value = __has_trivial_destructor(T) || IsPod >::value; }; #elif (defined(__GLIBCXX__) && (__GLIBCXX__ >= 20070724) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || (defined(_MSC_VER) && (_MSC_VER >= 1600 && _MSC_VER < 1900)) // GCC's libstdc++ 20070724 and later supports C++ TR1 type_traits in the std namespace. // VC10 (VS2010) and later support C++ TR1 type_traits in the std::tr1 namespace. template struct HasTrivialConstructor : public std::tr1::has_trivial_constructor { }; template struct HasTrivialDestructor : public std::tr1::has_trivial_destructor { }; #else // This compiler doesn't provide type traits, so we provide basic HasTrivialConstructor // and HasTrivialDestructor definitions. The definitions here include most built-in // scalar types but do not include POD structs and classes. For the intended purposes of // type_traits this results correct but potentially less efficient code. template struct IntegralConstant { static const T value = v; typedef T value_type; typedef IntegralConstant type; }; typedef IntegralConstant true_type; typedef IntegralConstant false_type; template struct HasTrivialConstructor : public false_type{ }; template struct HasTrivialDestructor : public false_type{ }; template struct HasTrivialConstructor : public true_type{ }; template struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; template <> struct HasTrivialConstructor : public true_type{ }; #endif template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; template <> struct HasTrivialDestructor : public true_type{ }; #endif #endif // __GLIBCXX__, etc. } // namespace WTF #endif // TypeTraits_h