Minor improvements to DUMP macro

- output enum values names with help of magic_enum
- output string literals as is, without type info
This commit is contained in:
Vasily Nemkov 2021-11-26 12:20:08 +02:00
parent 6c5746a067
commit e9466e505b

View File

@ -6,6 +6,7 @@
#include <tuple> #include <tuple>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <magic_enum.hpp>
/** Usage: /** Usage:
* *
@ -61,6 +62,13 @@ std::enable_if_t<priority == 2, Out> & dumpImpl(Out & out, T && x, std::decay_t<
} }
template <int priority, typename Out, typename T>
std::enable_if_t<priority == 3 && std::is_enum_v<std::decay_t<T>>, Out> &
dumpImpl(Out & out, T && x)
{
return out << magic_enum::enum_name(x);
}
/// string and const char * - output not as container or pointer. /// string and const char * - output not as container or pointer.
template <int priority, typename Out, typename T> template <int priority, typename Out, typename T>
@ -131,15 +139,26 @@ Out & dumpValue(Out & out, T && x)
template <typename Out, typename T> template <typename Out, typename T>
Out & dump(Out & out, const char * name, T && x) Out & dump(Out & out, const char * name, T && x)
{ {
// Dumping string literal, printing name and demangled type is irrelevant.
if constexpr (std::is_same_v<const char *, std::decay_t<std::remove_reference_t<T>>>)
{
const auto name_len = strlen(name);
const auto value_len = strlen(x);
// `name` is the same as quoted `x`
if (name_len > 2 && value_len > 0 && name[0] == '"' && name[name_len - 1] == '"'
&& strncmp(name + 1, x, std::min(value_len, name_len) - 1) == 0)
return out << x;
}
out << demangle(typeid(x).name()) << " " << name << " = "; out << demangle(typeid(x).name()) << " " << name << " = ";
return dumpValue(out, x); return dumpValue(out, x) << "; ";
} }
#ifdef __clang__ #ifdef __clang__
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#endif #endif
#define DUMPVAR(VAR) ::dump(std::cerr, #VAR, (VAR)); std::cerr << "; "; #define DUMPVAR(VAR) ::dump(std::cerr, #VAR, (VAR));
#define DUMPHEAD std::cerr << __FILE__ << ':' << __LINE__ << " [ " << getThreadId() << " ] "; #define DUMPHEAD std::cerr << __FILE__ << ':' << __LINE__ << " [ " << getThreadId() << " ] ";
#define DUMPTAIL std::cerr << '\n'; #define DUMPTAIL std::cerr << '\n';