Changeset 104333 in webkit


Ignore:
Timestamp:
Jan 6, 2012 1:57:22 PM (12 years ago)
Author:
weinig@apple.com
Message:

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.

Location:
trunk/Source/JavaScriptCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r104330 r104333  
     12012-01-06  Sam Weinig  <sam@webkit.org>
     2
     3        Add a DecayArray type trait as a first step towards merging OwnPtr and OwnArrayPtr
     4        https://bugs.webkit.org/show_bug.cgi?id=75737
     5
     6        Reviewed by Anders Carlsson.
     7
     8        * wtf/TypeTraits.cpp:
     9        * wtf/TypeTraits.h:
     10        Added a DecayArray trait, that can convert T[] and T[3] -> T*. DecayArray
     11        is composed of some helpers which are also exposed, Conditional<>, which
     12        can provide one type or another based on a boolean predicate, IsArray<>
     13        which can deduce array types, and RemoveExtent<>, which removes the extent
     14        from an array type.
     15
    1162012-01-06  Oliver Hunt  <oliver@apple.com>
    217
  • trunk/Source/JavaScriptCore/wtf/TypeTraits.cpp

    r69236 r104333  
    140140COMPILE_ASSERT((IsSameType<int, RemoveReference<int&>::Type>::value), WTF_Test_RemoveReference_int_reference);
    141141
     142
     143typedef int IntArray[];
     144typedef int IntArraySized[4];
     145
     146COMPILE_ASSERT((IsArray<IntArray>::value), WTF_Test_IsArray_int_array);
     147COMPILE_ASSERT((IsArray<IntArraySized>::value), WTF_Test_IsArray_int_sized_array);
     148
     149COMPILE_ASSERT((IsSameType<int, RemoveExtent<IntArray>::Type>::value), WTF_Test_RemoveExtent_int_array);
     150COMPILE_ASSERT((IsSameType<int, RemoveExtent<IntArraySized>::Type>::value), WTF_Test_RemoveReference_int_sized_array);
     151
     152COMPILE_ASSERT((IsSameType<int*, DecayArray<IntArray>::Type>::value), WTF_Test_DecayArray_int_array);
     153COMPILE_ASSERT((IsSameType<int*, DecayArray<IntArraySized>::Type>::value), WTF_Test_DecayArray_int_sized_array);
     154
     155COMPILE_ASSERT((IsSameType<int*, DecayArray<IntArray&>::Type>::value), WTF_Test_DecayArray_int_array_reference);
     156COMPILE_ASSERT((IsSameType<int*, DecayArray<IntArraySized&>::Type>::value), WTF_Test_DecayArray_int_sized_array_reference);
     157
    142158} // namespace WTF
  • trunk/Source/JavaScriptCore/wtf/TypeTraits.h

    r85945 r104333  
    3636    // The following are provided in this file:
    3737    //
     38    //   Conditional<Predicate, If, Then>::Type
     39    //
    3840    //   IsInteger<T>::value
    3941    //   IsPod<T>::value, see the definition for a note about its limitations
    4042    //   IsConvertibleToInteger<T>::value
     43    //
     44    //   IsArray<T>::value
    4145    //
    4246    //   IsSameType<T, U>::value
     
    4751    //   RemoveVolatile<T>::Type
    4852    //   RemoveConstVolatile<T>::Type
     53    //   RemoveExtent<T>::Type
     54    //
     55    //   DecayArray<T>::Type
    4956    //
    5057    //   COMPILE_ASSERT's in TypeTraits.cpp illustrate their usage and what they do.
     58
     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; };
    5161
    5262    template<typename T> struct IsInteger           { static const bool value = false; };
     
    105115    };
    106116
     117
     118    template <class T> struct IsArray {
     119        static const bool value = false;
     120    };
     121
     122    template <class T> struct IsArray<T[]> {
     123        static const bool value = true;
     124    };
     125
     126    template <class T, size_t N> struct IsArray<T[N]> {
     127        static const bool value = true;
     128    };
     129
     130
    107131    template <typename T, typename U> struct IsSameType {
    108132        static const bool value = false;
     
    181205    template <typename T> struct RemoveReference<T&> {
    182206        typedef T Type;
     207    };
     208
     209    template <typename T> struct RemoveExtent {
     210        typedef T Type;
     211    };
     212
     213    template <typename T> struct RemoveExtent<T[]> {
     214        typedef T Type;
     215    };
     216
     217    template <typename T, size_t N> struct RemoveExtent<T[N]> {
     218        typedef T Type;
     219    };
     220
     221    template <class T> struct DecayArray {
     222        typedef typename RemoveReference<T>::Type U;
     223    public:
     224        typedef typename Conditional<
     225            IsArray<U>::value,
     226            typename RemoveExtent<U>::Type*,
     227            typename RemoveConstVolatile<U>::Type
     228        >::Type Type;
    183229    };
    184230
Note: See TracChangeset for help on using the changeset viewer.