Bug 1538081 - Part 3: Add operator<<(ostream&) definitions for a few common types. r=froydnj,gerald
☠☠ backed out by 4e1afd840437 ☠ ☠
authorCameron McCormack <cam@mcc.id.au>
Sat, 30 Mar 2019 00:21:24 +0000
changeset 466882 d6d331abbf0ebf89f4c7a3f391e4ac4b943c37cb
parent 466881 26df801e44db03a402a6a9d3aa6143aa49f78cab
child 466883 ced61a86c74c6ce2a966b2ac7880cfe3e7a8e0d8
push id35784
push usernerli@mozilla.com
push dateSat, 30 Mar 2019 09:32:04 +0000
treeherdermozilla-central@d42c60ccf0d0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj, gerald
bugs1538081
milestone68.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 1538081 - Part 3: Add operator<<(ostream&) definitions for a few common types. r=froydnj,gerald Depends on D25023 Differential Revision: https://phabricator.services.mozilla.com/D25024
mfbt/Array.h
mfbt/DbgMacro.h
mfbt/RefPtr.h
xpcom/base/nsCOMPtr.h
xpcom/ds/nsTArray.h
xpcom/string/nsString.h
--- a/mfbt/Array.h
+++ b/mfbt/Array.h
@@ -9,16 +9,17 @@
 #ifndef mozilla_Array_h
 #define mozilla_Array_h
 
 #include "mozilla/Assertions.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/Move.h"
 #include "mozilla/ReverseIterator.h"
 
+#include <ostream>
 #include <stddef.h>
 
 namespace mozilla {
 
 template <typename T, size_t Length>
 class Array {
   T mArr[Length];
 
@@ -83,11 +84,18 @@ class Array<T, 0> {
  public:
   T& operator[](size_t aIndex) { MOZ_CRASH("indexing into zero-length array"); }
 
   const T& operator[](size_t aIndex) const {
     MOZ_CRASH("indexing into zero-length array");
   }
 };
 
+// MOZ_DBG support
+
+template <typename T, size_t Length>
+std::ostream& operator<<(std::ostream& aOut, const Array<T, Length>& aArray) {
+  return aOut << MakeSpan(aArray);
+}
+
 } /* namespace mozilla */
 
 #endif /* mozilla_Array_h */
--- a/mfbt/DbgMacro.h
+++ b/mfbt/DbgMacro.h
@@ -5,16 +5,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_DbgMacro_h
 #define mozilla_DbgMacro_h
 
 /* a MOZ_DBG macro that outputs a wrapped value to stderr then returns it */
 
 #include "mozilla/MacroForEach.h"
+#include "mozilla/Span.h"
 
 #include <stdio.h>
 #include <sstream>
 
 namespace mozilla {
 
 namespace detail {
 
@@ -72,16 +73,38 @@ auto&& MozDbg(const char* aFile, int aLi
   fputs(s.str().c_str(), stderr);
   return std::forward<T>(aValue);
 }
 
 }  // namespace detail
 
 }  // namespace mozilla
 
+template <class ElementType, size_t Extent>
+std::ostream& operator<<(std::ostream& aOut,
+                         const mozilla::Span<ElementType, Extent>& aSpan) {
+  aOut << '[';
+  if (!aSpan.IsEmpty()) {
+    aOut << aSpan[0];
+    for (size_t i = 1; i < aSpan.Length(); ++i) {
+      aOut << ", " << aSpan[i];
+    }
+  }
+  return aOut << ']';
+}
+
+// Don't define this for char[], since operator<<(ostream&, char*) is already
+// defined.
+template <typename T, size_t N,
+          typename = std::enable_if_t<!std::is_same<T, char>::value>>
+std::ostream& operator<<(std::ostream& aOut, const T (&aArray)[N]) {
+  return aOut << mozilla::MakeSpan(aArray);
+  // return aOut << mozilla::Span(aArray);
+}
+
 // MOZ_DBG is a macro like the Rust dbg!() macro -- it will print out the
 // expression passed to it to stderr and then return the value.  It is available
 // only in MOZILLA_OFFICIAL builds, so you shouldn't land any uses of it in the
 // tree.
 //
 // It should work for any type T that has an operator<<(std::ostream&, const T&)
 // defined for it.
 //
--- a/mfbt/RefPtr.h
+++ b/mfbt/RefPtr.h
@@ -5,16 +5,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_RefPtr_h
 #define mozilla_RefPtr_h
 
 #include "mozilla/AlreadyAddRefed.h"
 #include "mozilla/Assertions.h"
 #include "mozilla/Attributes.h"
+#include "mozilla/DbgMacro.h"
 
 /*****************************************************************************/
 
 // template <class T> class RefPtrGetterAddRefs;
 
 class nsQueryReferent;
 class nsCOMPtr_helper;
 class nsISupports;
@@ -518,16 +519,23 @@ inline bool operator!=(const RefPtr<T>& 
   return aLhs.get() != nullptr;
 }
 
 template <class T>
 inline bool operator!=(decltype(nullptr), const RefPtr<T>& aRhs) {
   return nullptr != aRhs.get();
 }
 
+// MOZ_DBG support
+
+template <class T>
+std::ostream& operator<<(std::ostream& aOut, const RefPtr<T>& aObj) {
+  return mozilla::DebugValue(aOut, aObj.get());
+}
+
 /*****************************************************************************/
 
 template <class T>
 inline already_AddRefed<T> do_AddRef(T* aObj) {
   RefPtr<T> ref(aObj);
   return ref.forget();
 }
 
--- a/xpcom/base/nsCOMPtr.h
+++ b/xpcom/base/nsCOMPtr.h
@@ -1471,9 +1471,16 @@ RefPtr<T>& RefPtr<T>::operator=(const ns
 }
 
 template <class T>
 inline already_AddRefed<T> do_AddRef(const nsCOMPtr<T>& aObj) {
   nsCOMPtr<T> ref(aObj);
   return ref.forget();
 }
 
+// MOZ_DBG support
+
+template <class T>
+std::ostream& operator<<(std::ostream& aOut, const nsCOMPtr<T>& aObj) {
+  return aOut << aObj.get();
+}
+
 #endif  // !defined(nsCOMPtr_h___)
--- a/xpcom/ds/nsTArray.h
+++ b/xpcom/ds/nsTArray.h
@@ -9,16 +9,17 @@
 
 #include "nsTArrayForwardDeclare.h"
 #include "mozilla/Alignment.h"
 #include "mozilla/ArrayIterator.h"
 #include "mozilla/Assertions.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/BinarySearch.h"
 #include "mozilla/CheckedInt.h"
+#include "mozilla/DbgMacro.h"
 #include "mozilla/fallible.h"
 #include "mozilla/FunctionTypeTraits.h"
 #include "mozilla/MathAlgorithms.h"
 #include "mozilla/MemoryReporting.h"
 #include "mozilla/Move.h"
 #include "mozilla/mozalloc.h"
 #include "mozilla/ReverseIterator.h"
 #include "mozilla/TypeTraits.h"
@@ -31,16 +32,17 @@
 #include "nscore.h"
 #include "nsQuickSort.h"
 #include "nsDebug.h"
 #include "nsISupportsImpl.h"
 #include "nsRegionFwd.h"
 #include <functional>
 #include <initializer_list>
 #include <new>
+#include <ostream>
 
 namespace JS {
 template <class T>
 class Heap;
 } /* namespace JS */
 
 class nsRegion;
 namespace mozilla {
@@ -2642,16 +2644,24 @@ Span<ElementType> MakeSpan(nsTArray_Impl
 template <class ElementType, class TArrayAlloc>
 Span<const ElementType> MakeSpan(
     const nsTArray_Impl<ElementType, TArrayAlloc>& aTArray) {
   return aTArray;
 }
 
 }  // namespace mozilla
 
+// MOZ_DBG support
+
+template <class E, class Alloc>
+std::ostream& operator<<(std::ostream& aOut,
+                         const nsTArray_Impl<E, Alloc>& aTArray) {
+  return aOut << mozilla::MakeSpan(aTArray);
+}
+
 // Assert that AutoTArray doesn't have any extra padding inside.
 //
 // It's important that the data stored in this auto array takes up a multiple of
 // 8 bytes; e.g. AutoTArray<uint32_t, 1> wouldn't work.  Since AutoTArray
 // contains a pointer, its size must be a multiple of alignof(void*).  (This is
 // because any type may be placed into an array, and there's no padding between
 // elements of an array.)  The compiler pads the end of the structure to
 // enforce this rule.
--- a/xpcom/string/nsString.h
+++ b/xpcom/string/nsString.h
@@ -2,16 +2,18 @@
 /* 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/. */
 
 #ifndef nsString_h___
 #define nsString_h___
 
+#include <ostream>
+
 #include "mozilla/Attributes.h"
 
 #include "nsStringFwd.h"
 
 #include "nsAString.h"
 #include "nsDependentSubstring.h"
 #include "nsReadableUtils.h"
 
@@ -117,14 +119,26 @@ class NS_ConvertUTF8toUTF16 : public nsA
     AppendUTF8toUTF16(aCString, *this);
   }
 
  private:
   // NOT TO BE IMPLEMENTED
   NS_ConvertUTF8toUTF16(char16_t) = delete;
 };
 
+// MOZ_DBG support
+
+inline std::ostream& operator<<(std::ostream& aOut, const nsACString& aString) {
+  aOut << '"';
+  aOut.write(aString.Data(), aString.Length());
+  return aOut << '"';
+}
+
+inline std::ostream& operator<<(std::ostream& aOut, const nsAString& aString) {
+  return aOut << NS_ConvertUTF16toUTF8(aString);
+}
+
 // the following are included/declared for backwards compatibility
 #include "nsDependentString.h"
 #include "nsLiteralString.h"
 #include "nsPromiseFlatString.h"
 
 #endif  // !defined(nsString_h___)