Commit b87c01c8 authored by weinig@apple.com's avatar weinig@apple.com

Add a DecayArray type trait as a first step towards merging OwnPtr and OwnArrayPtr

https://bugs.webkit.org/show_bug.cgi?id=75737

Reviewed by Anders Carlsson.

* wtf/TypeTraits.cpp:
* wtf/TypeTraits.h:
Added a DecayArray trait, that can convert T[] and T[3] -> T*. DecayArray
is composed of some helpers which are also exposed, Conditional<>, which
can provide one type or another based on a boolean predicate, IsArray<>
which can deduce array types, and RemoveExtent<>, which removes the extent
from an array type. 


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@104333 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent a41f4b6b
2012-01-06 Sam Weinig <sam@webkit.org>
Add a DecayArray type trait as a first step towards merging OwnPtr and OwnArrayPtr
https://bugs.webkit.org/show_bug.cgi?id=75737
Reviewed by Anders Carlsson.
* wtf/TypeTraits.cpp:
* wtf/TypeTraits.h:
Added a DecayArray trait, that can convert T[] and T[3] -> T*. DecayArray
is composed of some helpers which are also exposed, Conditional<>, which
can provide one type or another based on a boolean predicate, IsArray<>
which can deduce array types, and RemoveExtent<>, which removes the extent
from an array type.
2012-01-06 Oliver Hunt <oliver@apple.com>
GetByteArrayLength is incorrect
......@@ -139,4 +139,20 @@ COMPILE_ASSERT((!IsSameType<int, RemovePointer<int**>::Type>::value), WTF_Test_R
COMPILE_ASSERT((IsSameType<int, RemoveReference<int>::Type>::value), WTF_Test_RemoveReference_int);
COMPILE_ASSERT((IsSameType<int, RemoveReference<int&>::Type>::value), WTF_Test_RemoveReference_int_reference);
typedef int IntArray[];
typedef int IntArraySized[4];
COMPILE_ASSERT((IsArray<IntArray>::value), WTF_Test_IsArray_int_array);
COMPILE_ASSERT((IsArray<IntArraySized>::value), WTF_Test_IsArray_int_sized_array);
COMPILE_ASSERT((IsSameType<int, RemoveExtent<IntArray>::Type>::value), WTF_Test_RemoveExtent_int_array);
COMPILE_ASSERT((IsSameType<int, RemoveExtent<IntArraySized>::Type>::value), WTF_Test_RemoveReference_int_sized_array);
COMPILE_ASSERT((IsSameType<int*, DecayArray<IntArray>::Type>::value), WTF_Test_DecayArray_int_array);
COMPILE_ASSERT((IsSameType<int*, DecayArray<IntArraySized>::Type>::value), WTF_Test_DecayArray_int_sized_array);
COMPILE_ASSERT((IsSameType<int*, DecayArray<IntArray&>::Type>::value), WTF_Test_DecayArray_int_array_reference);
COMPILE_ASSERT((IsSameType<int*, DecayArray<IntArraySized&>::Type>::value), WTF_Test_DecayArray_int_sized_array_reference);
} // namespace WTF
......@@ -35,10 +35,14 @@ namespace WTF {
// The following are provided in this file:
//
// Conditional<Predicate, If, Then>::Type
//
// IsInteger<T>::value
// IsPod<T>::value, see the definition for a note about its limitations
// IsConvertibleToInteger<T>::value
//
// IsArray<T>::value
//
// IsSameType<T, U>::value
//
// RemovePointer<T>::Type
......@@ -46,9 +50,15 @@ namespace WTF {
// RemoveConst<T>::Type
// RemoveVolatile<T>::Type
// RemoveConstVolatile<T>::Type
// RemoveExtent<T>::Type
//
// DecayArray<T>::Type
//
// COMPILE_ASSERT's in TypeTraits.cpp illustrate their usage and what they do.
template <bool Predicate, class If, class Then> struct Conditional { typedef If Type; };
template <class If, class Then> struct Conditional<false, If, Then> { typedef Then Type; };
template<typename T> struct IsInteger { static const bool value = false; };
template<> struct IsInteger<bool> { static const bool value = true; };
template<> struct IsInteger<char> { static const bool value = true; };
......@@ -104,6 +114,20 @@ namespace WTF {
static const bool value = IsInteger<T>::value || IsConvertibleToDouble<!IsInteger<T>::value, T>::value;
};
template <class T> struct IsArray {
static const bool value = false;
};
template <class T> struct IsArray<T[]> {
static const bool value = true;
};
template <class T, size_t N> struct IsArray<T[N]> {
static const bool value = true;
};
template <typename T, typename U> struct IsSameType {
static const bool value = false;
};
......@@ -182,6 +206,28 @@ namespace WTF {
typedef T Type;
};
template <typename T> struct RemoveExtent {
typedef T Type;
};
template <typename T> struct RemoveExtent<T[]> {
typedef T Type;
};
template <typename T, size_t N> struct RemoveExtent<T[N]> {
typedef T Type;
};
template <class T> struct DecayArray {
typedef typename RemoveReference<T>::Type U;
public:
typedef typename Conditional<
IsArray<U>::value,
typename RemoveExtent<U>::Type*,
typename RemoveConstVolatile<U>::Type
>::Type Type;
};
#if (defined(__GLIBCXX__) && (__GLIBCXX__ >= 20070724) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || (defined(_MSC_VER) && (_MSC_VER >= 1600))
// GCC's libstdc++ 20070724 and later supports C++ TR1 type_traits in the std namespace.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment