From a635fa285572b4aa9b502188c2ef2382f58f2002 Mon Sep 17 00:00:00 2001 From: Cameron McCormack Date: Sat, 30 Mar 2019 04:25:15 +0000 Subject: [PATCH] Bug 1538081 - Part 2: Add MOZ_DEFINE_DBG. r=froydnj,gerald Depends on D24583 Differential Revision: https://phabricator.services.mozilla.com/D25023 --HG-- extra : moz-landing-system : lando --- mfbt/DbgMacro.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/mfbt/DbgMacro.h b/mfbt/DbgMacro.h index fdeebef0241b..508bd87093bc 100644 --- a/mfbt/DbgMacro.h +++ b/mfbt/DbgMacro.h @@ -9,6 +9,8 @@ /* a MOZ_DBG macro that outputs a wrapped value to stderr then returns it */ +#include "mozilla/MacroForEach.h" + #include #include @@ -122,4 +124,31 @@ auto&& MozDbg(const char* aFile, int aLine, const char* aExpression, 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