Bug 953296 - Implement mozilla::RemoveExtent. r=jcranmer
authorJeff Walden <jwalden@mit.edu>
Tue, 10 Jun 2014 12:58:17 -0700
changeset 189592 f2f60161a7ac105e535943cc9360e462aa7d69a7
parent 189591 2572716c3646aa70b0109e602ee5113c6ad17138
child 189593 d0ea5f3a97a7a194983bb5b17596101aaba12170
push id45108
push userjwalden@mit.edu
push dateThu, 19 Jun 2014 21:05:45 +0000
treeherdermozilla-inbound@befe068bc32c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjcranmer
bugs953296
milestone33.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 953296 - Implement mozilla::RemoveExtent. r=jcranmer
mfbt/TypeTraits.h
mfbt/tests/TestTypeTraits.cpp
--- a/mfbt/TypeTraits.h
+++ b/mfbt/TypeTraits.h
@@ -841,16 +841,43 @@ template<typename T>
 struct MakeUnsigned
   : EnableIf<IsIntegral<T>::value && !IsSame<bool, typename RemoveCV<T>::Type>::value,
              typename detail::MakeUnsigned<T>
             >::Type
 {};
 
 /* 20.9.7.4 Array modifications [meta.trans.arr] */
 
+/**
+ * RemoveExtent produces either the type of the elements of the array T, or T
+ * itself.
+ *
+ * mozilla::RemoveExtent<int>::Type is int;
+ * mozilla::RemoveExtent<const int[]>::Type is const int;
+ * mozilla::RemoveExtent<volatile int[5]>::Type is volatile int;
+ * mozilla::RemoveExtent<long[][17]>::Type is long[17].
+ */
+template<typename T>
+struct RemoveExtent
+{
+    typedef T Type;
+};
+
+template<typename T>
+struct RemoveExtent<T[]>
+{
+    typedef T Type;
+};
+
+template<typename T, decltype(sizeof(1)) N>
+struct RemoveExtent<T[N]>
+{
+    typedef T Type;
+};
+
 /* 20.9.7.5 Pointer modifications [meta.trans.ptr] */
 
 /* 20.9.7.6 Other transformations [meta.trans.other] */
 
 /**
  * EnableIf is a struct containing a typedef of T if and only if B is true.
  *
  * mozilla::EnableIf<true, int>::Type is int;
--- a/mfbt/tests/TestTypeTraits.cpp
+++ b/mfbt/tests/TestTypeTraits.cpp
@@ -14,16 +14,17 @@ using mozilla::IsEmpty;
 using mozilla::IsLvalueReference;
 using mozilla::IsReference;
 using mozilla::IsRvalueReference;
 using mozilla::IsSame;
 using mozilla::IsSigned;
 using mozilla::IsUnsigned;
 using mozilla::MakeSigned;
 using mozilla::MakeUnsigned;
+using mozilla::RemoveExtent;
 
 static_assert(!IsArray<bool>::value, "bool not an array");
 static_assert(IsArray<bool[]>::value, "bool[] is an array");
 static_assert(IsArray<bool[5]>::value, "bool[5] is an array");
 
 static_assert(!IsLvalueReference<bool>::value, "bool not an lvalue reference");
 static_assert(!IsLvalueReference<bool*>::value, "bool* not an lvalue reference");
 static_assert(IsLvalueReference<bool&>::value, "bool& is an lvalue reference");
@@ -287,16 +288,25 @@ static_assert(IsSame<MakeUnsigned<unsign
 
 static_assert(IsSame<MakeUnsigned<char>::Type, unsigned char>::value,
               "char won't unsignify correctly");
 static_assert(IsSame<MakeUnsigned<volatile char>::Type, volatile unsigned char>::value,
               "volatile char won't unsignify correctly");
 static_assert(IsSame<MakeUnsigned<const char>::Type, const unsigned char>::value,
               "const char won't unsignify correctly");
 
+static_assert(IsSame<RemoveExtent<int>::Type, int>::value,
+              "removing extent from non-array must return the non-array");
+static_assert(IsSame<RemoveExtent<const int[]>::Type, const int>::value,
+              "removing extent from unknown-bound array must return element type");
+static_assert(IsSame<RemoveExtent<volatile int[5]>::Type, volatile int>::value,
+              "removing extent from known-bound array must return element type");
+static_assert(IsSame<RemoveExtent<long[][17]>::Type, long[17]>::value,
+              "removing extent from multidimensional array must return element type");
+
 /*
  * Android's broken [u]intptr_t inttype macros are broken because its PRI*PTR
  * macros are defined as "ld", but sizeof(long) is 8 and sizeof(intptr_t)
  * is 4 on 32-bit Android. We redefine Android's PRI*PTR macros in
  * IntegerPrintfMacros.h and assert here that our new definitions match the
  * actual type sizes seen at compile time.
  */
 #if defined(ANDROID) && !defined(__LP64__)