Bug 1329513 - RemoveRawOrSmartPointerHelper to extract T from T*, RefPtr<T>, and nsCOMPtr<T> - r?froydnj draft
authorGerald Squelart <gsquelart@mozilla.com>
Mon, 09 Jan 2017 09:32:36 +1100
changeset 457793 4db25092c64d87f34e9691f1e80617c9a0bf3662
parent 457792 4068c9ce12eb6995027a2039a17957c21bf4608a
child 457794 1516d9dd5a4724cf452bc11e771b2a6ca01d8c47
child 457799 aa2cac47036d0d1d7a36c8c0137fe21f21b4961b
push id40897
push usergsquelart@mozilla.com
push dateMon, 09 Jan 2017 23:32:39 +0000
reviewersfroydnj
bugs1329513
milestone53.0a1
Bug 1329513 - RemoveRawOrSmartPointerHelper to extract T from T*, RefPtr<T>, and nsCOMPtr<T> - r?froydnj Implemented in the same style as RemovePointer and RemoveSmartPointer, for consistency. It could have been done by invoking the latter two, but I didn't want to add an unnecessary extra layer of templates; the cost of the code duplication should be negligible. MozReview-Commit-ID: IH4lZkbRYGZ
xpcom/glue/nsThreadUtils.h
xpcom/glue/tests/gtest/TestThreadUtils.cpp
--- a/xpcom/glue/nsThreadUtils.h
+++ b/xpcom/glue/nsThreadUtils.h
@@ -353,16 +353,49 @@ struct RemoveSmartPointerHelper<T, nsCOM
 
 } // namespace detail
 
 template<typename T>
 struct RemoveSmartPointer
   : detail::RemoveSmartPointerHelper<T, typename RemoveCV<T>::Type>
 {};
 
+namespace detail {
+
+template<typename T, typename CVRemoved>
+struct RemoveRawOrSmartPointerHelper
+{
+  typedef T Type;
+};
+
+template<typename T, typename Pointee>
+struct RemoveRawOrSmartPointerHelper<T, Pointee*>
+{
+  typedef Pointee Type;
+};
+
+template<typename T, typename Pointee>
+struct RemoveRawOrSmartPointerHelper<T, RefPtr<Pointee>>
+{
+  typedef Pointee Type;
+};
+
+template<typename T, typename Pointee>
+struct RemoveRawOrSmartPointerHelper<T, nsCOMPtr<Pointee>>
+{
+  typedef Pointee Type;
+};
+
+} // namespace detail
+
+template<typename T>
+struct RemoveRawOrSmartPointer
+  : detail::RemoveRawOrSmartPointerHelper<T, typename RemoveCV<T>::Type>
+{};
+
 } // namespace mozilla
 
 inline nsISupports*
 ToSupports(mozilla::Runnable *p)
 {
   return static_cast<nsIRunnable*>(p);
 }
 
--- a/xpcom/glue/tests/gtest/TestThreadUtils.cpp
+++ b/xpcom/glue/tests/gtest/TestThreadUtils.cpp
@@ -2,16 +2,17 @@
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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 <stdio.h>
 #include <stdlib.h>
 #include "nsThreadUtils.h"
+#include "mozilla/UniquePtr.h"
 #include "gtest/gtest.h"
 
 // {9e70a320-be02-11d1-8031-006008159b5a}
 #define NS_IFOO_IID \
   {0x9e70a320, 0xbe02, 0x11d1,    \
     {0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}}
 
 TEST(ThreadUtils, TypeTraits)
@@ -32,32 +33,65 @@ TEST(ThreadUtils, TypeTraits)
                 "IsRefcountedSmartPointer<const nsCOMPtr<...>> should be true");
   static_assert(mozilla::IsRefcountedSmartPointer<volatile nsCOMPtr<int>>::value,
                 "IsRefcountedSmartPointer<volatile nsCOMPtr<...>> should be true");
   static_assert(mozilla::IsRefcountedSmartPointer<const volatile nsCOMPtr<int>>::value,
                 "IsRefcountedSmartPointer<const volatile nsCOMPtr<...>> should be true");
 
   static_assert(mozilla::IsSame<int, mozilla::RemoveSmartPointer<int>::Type>::value,
                 "RemoveSmartPointer<int>::Type should be int");
+  static_assert(mozilla::IsSame<int*, mozilla::RemoveSmartPointer<int*>::Type>::value,
+                "RemoveSmartPointer<int*>::Type should be int*");
+  static_assert(mozilla::IsSame<UniquePtr<int>, mozilla::RemoveSmartPointer<UniquePtr<int>>::Type>::value,
+                "RemoveSmartPointer<UniquePtr<int>>::Type should be UniquePtr<int>");
   static_assert(mozilla::IsSame<int, mozilla::RemoveSmartPointer<RefPtr<int>>::Type>::value,
                 "RemoveSmartPointer<RefPtr<int>>::Type should be int");
   static_assert(mozilla::IsSame<int, mozilla::RemoveSmartPointer<const RefPtr<int>>::Type>::value,
                 "RemoveSmartPointer<const RefPtr<int>>::Type should be int");
   static_assert(mozilla::IsSame<int, mozilla::RemoveSmartPointer<volatile RefPtr<int>>::Type>::value,
                 "RemoveSmartPointer<volatile RefPtr<int>>::Type should be int");
   static_assert(mozilla::IsSame<int, mozilla::RemoveSmartPointer<const volatile RefPtr<int>>::Type>::value,
                 "RemoveSmartPointer<const volatile RefPtr<int>>::Type should be int");
   static_assert(mozilla::IsSame<int, mozilla::RemoveSmartPointer<nsCOMPtr<int>>::Type>::value,
                 "RemoveSmartPointer<nsCOMPtr<int>>::Type should be int");
   static_assert(mozilla::IsSame<int, mozilla::RemoveSmartPointer<const nsCOMPtr<int>>::Type>::value,
                 "RemoveSmartPointer<const nsCOMPtr<int>>::Type should be int");
   static_assert(mozilla::IsSame<int, mozilla::RemoveSmartPointer<volatile nsCOMPtr<int>>::Type>::value,
                 "RemoveSmartPointer<volatile nsCOMPtr<int>>::Type should be int");
   static_assert(mozilla::IsSame<int, mozilla::RemoveSmartPointer<const volatile nsCOMPtr<int>>::Type>::value,
                 "RemoveSmartPointer<const volatile nsCOMPtr<int>>::Type should be int");
+
+  static_assert(mozilla::IsSame<int, mozilla::RemoveRawOrSmartPointer<int>::Type>::value,
+                "RemoveRawOrSmartPointer<int>::Type should be int");
+  static_assert(mozilla::IsSame<UniquePtr<int>, mozilla::RemoveRawOrSmartPointer<UniquePtr<int>>::Type>::value,
+                "RemoveRawOrSmartPointer<UniquePtr<int>>::Type should be UniquePtr<int>");
+  static_assert(mozilla::IsSame<int, mozilla::RemoveRawOrSmartPointer<int*>::Type>::value,
+                "RemoveRawOrSmartPointer<int*>::Type should be int");
+  static_assert(mozilla::IsSame<const int, mozilla::RemoveRawOrSmartPointer<const int*>::Type>::value,
+                "RemoveRawOrSmartPointer<const int*>::Type should be const int");
+  static_assert(mozilla::IsSame<volatile int, mozilla::RemoveRawOrSmartPointer<volatile int*>::Type>::value,
+                "RemoveRawOrSmartPointer<volatile int*>::Type should be volatile int");
+  static_assert(mozilla::IsSame<const volatile int, mozilla::RemoveRawOrSmartPointer<const volatile int*>::Type>::value,
+                "RemoveRawOrSmartPointer<const volatile int*>::Type should be const volatile int");
+  static_assert(mozilla::IsSame<int, mozilla::RemoveRawOrSmartPointer<RefPtr<int>>::Type>::value,
+                "RemoveRawOrSmartPointer<RefPtr<int>>::Type should be int");
+  static_assert(mozilla::IsSame<int, mozilla::RemoveRawOrSmartPointer<const RefPtr<int>>::Type>::value,
+                "RemoveRawOrSmartPointer<const RefPtr<int>>::Type should be int");
+  static_assert(mozilla::IsSame<int, mozilla::RemoveRawOrSmartPointer<volatile RefPtr<int>>::Type>::value,
+                "RemoveRawOrSmartPointer<volatile RefPtr<int>>::Type should be int");
+  static_assert(mozilla::IsSame<int, mozilla::RemoveRawOrSmartPointer<const volatile RefPtr<int>>::Type>::value,
+                "RemoveRawOrSmartPointer<const volatile RefPtr<int>>::Type should be int");
+  static_assert(mozilla::IsSame<int, mozilla::RemoveRawOrSmartPointer<nsCOMPtr<int>>::Type>::value,
+                "RemoveRawOrSmartPointer<nsCOMPtr<int>>::Type should be int");
+  static_assert(mozilla::IsSame<int, mozilla::RemoveRawOrSmartPointer<const nsCOMPtr<int>>::Type>::value,
+                "RemoveRawOrSmartPointer<const nsCOMPtr<int>>::Type should be int");
+  static_assert(mozilla::IsSame<int, mozilla::RemoveRawOrSmartPointer<volatile nsCOMPtr<int>>::Type>::value,
+                "RemoveRawOrSmartPointer<volatile nsCOMPtr<int>>::Type should be int");
+  static_assert(mozilla::IsSame<int, mozilla::RemoveRawOrSmartPointer<const volatile nsCOMPtr<int>>::Type>::value,
+                "RemoveRawOrSmartPointer<const volatile nsCOMPtr<int>>::Type should be int");
 }
 
 namespace TestThreadUtils {
 
 static bool gDebug = false;
 static int gAlive, gZombies;
 static int gAllConstructions, gConstructions, gCopyConstructions,
            gMoveConstructions, gDestructions, gAssignments, gMoves;