Bug 953296 - Implement mozilla::IsArray. r=froydnj
authorJeff Walden <jwalden@mit.edu>
Mon, 30 Dec 2013 01:07:32 -0600
changeset 162229 a6330cc7cc46ab4190a7d1265f9dc6f0d6db930b
parent 162228 3d519786b8584b3c926d2fa3a8d3e0b480804781
child 162230 b3ae449c47ec91a26e9da135acc7d7a0c606ad80
push id38143
push userjwalden@mit.edu
push dateMon, 06 Jan 2014 23:07:15 +0000
treeherdermozilla-inbound@b3ae449c47ec [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs953296
milestone29.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::IsArray. r=froydnj
mfbt/TypeTraits.h
mfbt/tests/TestTypeTraits.cpp
--- a/mfbt/TypeTraits.h
+++ b/mfbt/TypeTraits.h
@@ -4,16 +4,18 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* Template-based metaprogramming and type-testing facilities. */
 
 #ifndef mozilla_TypeTraits_h
 #define mozilla_TypeTraits_h
 
+#include "mozilla/Types.h"
+
 /*
  * These traits are approximate copies of the traits and semantics from C++11's
  * <type_traits> header.  Don't add traits not in that header!  When all
  * platforms provide that header, we can convert all users and remove this one.
  */
 
 #include <wchar.h>
 
@@ -109,16 +111,41 @@ struct IsFloatingPointHelper
  * mozilla::IsFloatingPoint<long double>::value is true;
  * mozilla::IsFloatingPoint<double*>::value is false.
  */
 template<typename T>
 struct IsFloatingPoint
   : detail::IsFloatingPointHelper<typename RemoveCV<T>::Type>
 {};
 
+namespace detail {
+
+template<typename T>
+struct IsArrayHelper : FalseType {};
+
+template<typename T, decltype(sizeof(1)) N>
+struct IsArrayHelper<T[N]> : TrueType {};
+
+template<typename T>
+struct IsArrayHelper<T[]> : TrueType {};
+
+} // namespace detail
+
+/**
+ * IsArray determines whether a type is an array type, of known or unknown
+ * length.
+ *
+ * mozilla::IsArray<int>::value is false;
+ * mozilla::IsArray<int[]>::value is true;
+ * mozilla::IsArray<int[5]>::value is true.
+ */
+template<typename T>
+struct IsArray : detail::IsArrayHelper<typename RemoveCV<T>::Type>
+{};
+
 /**
  * IsPointer determines whether a type is a pointer type (but not a pointer-to-
  * member type).
  *
  * mozilla::IsPointer<struct S*>::value is true;
  * mozilla::IsPointer<int**>::value is true;
  * mozilla::IsPointer<void (*)(void)>::value is true;
  * mozilla::IsPointer<int>::value is false;
--- a/mfbt/tests/TestTypeTraits.cpp
+++ b/mfbt/tests/TestTypeTraits.cpp
@@ -1,29 +1,34 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/Assertions.h"
 #include "mozilla/TypeTraits.h"
 
+using mozilla::IsArray;
 using mozilla::IsBaseOf;
 using mozilla::IsClass;
 using mozilla::IsConvertible;
 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;
 
+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");
 static_assert(!IsLvalueReference<bool&&>::value, "bool&& not an lvalue reference");
 
 static_assert(!IsLvalueReference<void>::value, "void not an lvalue reference");
 static_assert(!IsLvalueReference<void*>::value, "void* not an lvalue reference");