Bug 1538081 - Part 2: Add MOZ_DEFINE_DBG. r=froydnj,gerald
☠☠ backed out by 4e1afd840437 ☠ ☠
authorCameron McCormack <cam@mcc.id.au>
Sat, 30 Mar 2019 00:21:17 +0000
changeset 466887 26df801e44db03a402a6a9d3aa6143aa49f78cab
parent 466886 113ac188a69e7d00c828e4400e81eb3dd25fbf46
child 466888 d6d331abbf0ebf89f4c7a3f391e4ac4b943c37cb
push id112603
push usernerli@mozilla.com
push dateSat, 30 Mar 2019 09:35:57 +0000
treeherdermozilla-inbound@7c3183c56eb6 [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 2: Add MOZ_DEFINE_DBG. r=froydnj,gerald Depends on D24583 Differential Revision: https://phabricator.services.mozilla.com/D25023
mfbt/DbgMacro.h
--- a/mfbt/DbgMacro.h
+++ b/mfbt/DbgMacro.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/. */
 
 #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 <stdio.h>
 #include <sstream>
 
 namespace mozilla {
 
 namespace detail {
 
 // Predicate to check whether T can be dereferenced and then inserted into an
@@ -117,9 +119,36 @@ auto&& MozDbg(const char* aFile, int aLi
 //                         // operator<<, but that temporary is returned from
 //                         // MOZ_DBG as an rvalue reference and so wants to
 //                         // invoke B's move constructor to initialize b2
 #ifndef MOZILLA_OFFICIAL
 #  define MOZ_DBG(expression_...) \
     mozilla::detail::MozDbg(__FILE__, __LINE__, #expression_, expression_)
 #endif
 
+// Helper macro for MOZ_DEFINE_DBG.
+#define MOZ_DBG_FIELD(name_) << #name_ << " = " << aValue.name_
+
+// Macro to define an operator<<(ostream&) for a struct or class that displays
+// the type name and the values of the specified member variables.  Must be
+// called inside the struct or class.
+//
+// For example:
+//
+//   struct Point {
+//     float x;
+//     float y;
+//
+//     MOZ_DEFINE_DBG(Point, x, y)
+//   };
+//
+// generates an operator<< that outputs strings like
+// "Point { x = 1.0, y = 2.0 }".
+#define MOZ_DEFINE_DBG(type_, members_...)                                   \
+  friend std::ostream& operator<<(std::ostream& aOut, const type_& aValue) { \
+    return aOut << #type_                                                    \
+                << (MOZ_ARG_COUNT(members_) == 0 ? "{ " : "")                \
+                       MOZ_FOR_EACH_SEPARATED(MOZ_DBG_FIELD, (<< ", "), (),  \
+                                              (members_))                    \
+                << (MOZ_ARG_COUNT(members_) == 0 ? " }" : "");               \
+  }
+
 #endif  // mozilla_DbgMacro_h