11#if defined(ZEROERR_ENABLE_PFR) && (ZEROERR_CXX_STANDARD >= 14)
15#if defined(ZEROERR_ENABLE_MAGIC_ENUM) && (ZEROERR_CXX_STANDARD >= 17)
16#include "magic_enum.hpp"
19#if defined(ZEROERR_ENABLE_DSVIZ)
41template <
typename T,
typename =
void>
63 template <
typename... Args>
66 call(std::forward<Args>(args)...);
72 call(std::forward<
decltype(value)>(value));
78 auto& ss =
static_cast<std::stringstream&
>(
os);
79 ss.str(std::string());
84 template <
typename T,
typename... V>
85 void call(T value, V... others) {
87 call(std::forward<V>(others)...);
113 static std::string
type(
const T& t) {
117#if defined(ZEROERR_ENABLE_MAGIC_ENUM) && (ZEROERR_CXX_STANDARD >= 17)
119 print(T value,
unsigned level,
const char* lb,
rank<0>) {
os <<
tab(level) << magic_enum::enum_name(value) << lb; }
122 print(T value,
unsigned level, const
char* lb,
rank<0>) {
os <<
tab(level) << value << lb; }
126 print(T value,
unsigned level, const
char* lb,
rank<0>) {
os <<
tab(level) << value << lb; }
129 print(T value,
unsigned level, const
char* lb,
rank<0>) {
130 if (value ==
nullptr)
131 os <<
tab(level) <<
"nullptr" << lb;
133 os <<
tab(level) <<
"<" <<
type(value) <<
" at " <<
static_cast<const void*
>(value) <<
">" << lb;
138 print(T value,
unsigned level, const
char* lb,
rank<0>) {
144 print(T value,
unsigned level, const
char* lb,
rank<1>) {
145 os <<
tab(level) <<
'\'' << value <<
'\'' << lb;
148#if defined(ZEROERR_ENABLE_PFR) && (ZEROERR_CXX_STANDARD >= 14)
149 template <
class StructType,
unsigned... I>
150 void print_struct(
const StructType& s,
unsigned,
const char*,
detail::seq<I...>) {
151 int _[] = {(
os << (I == 0 ?
"" :
", ") << pfr::get<I>(s), 0)...};
156 print(
const T& value,
unsigned level,
const char* lb, rank<1>) {
157 os <<
tab(level) <<
"{";
159 detail::gen_seq<pfr::tuple_size<T>::value>{});
160 os <<
tab(level) <<
"}" << lb;
165 print(T value,
unsigned level, const
char* lb,
rank<2>) {
166 os <<
tab(level) << (value ?
"true" :
"false") << lb;
170 print(T value,
unsigned level, const
char* lb,
rank<2>) {
os <<
tab(level) << value << lb; }
174 print(const T& value,
unsigned level, const
char* lb,
rank<2>) {
177 for (
auto iter = value.begin(); iter != value.end(); ++iter) {
178 if (std::next(iter) == value.end())
last =
true;
181 os <<
tab(level) <<
"}" << lb;
185 print(const T& value,
unsigned level, const
char* lb,
rank<3>) {
186 os <<
tab(level) <<
"[";
188 for (
auto iter = value.begin(); iter != value.end(); ++iter) {
189 if (std::next(iter) == value.end())
last =
true;
192 os <<
tab(level) <<
"]" << lb;
197 print(T value,
unsigned level, const
char* lb,
rank<3>) {
198 if (value.get() ==
nullptr)
199 os <<
tab(level) <<
"nullptr" << lb;
201 os <<
tab(level) <<
"<" <<
type(value) <<
" at " << value.get() <<
">" << lb;
205 print(const T& value,
unsigned level, const
char* lb,
rank<4>) {
208 for (
auto iter = value.begin(); iter != value.end(); ++iter) {
209 if (std::next(iter) == value.end())
last =
true;
214 os <<
tab(level) <<
"}" << lb;
218 print(T value,
unsigned level, const
char* lb,
rank<4>) {
219 os <<
tab(level) <<
"(" << value.real() <<
"+" << value.imag() <<
"i)" << lb;
223 print(T value,
unsigned level, const
char* lb,
rank<4>) {
227 template <
class TupType>
230 template <
class TupType,
unsigned... I>
232 int _[] = {(
os << (I == 0 ?
"" :
", "),
237 template <
class... Args>
238 void print(
const std::tuple<Args...>& value,
unsigned level,
const char* lb,
rank<3>) {
239 os <<
tab(level) <<
"(";
250 char* res = abi::__cxa_demangle(name, NULL, NULL, &status);
251 std::string ret = (status == 0) ? res : name;
261 throw std::runtime_error(
"Printer is not using stringstream");
262 return static_cast<std::stringstream&
>(
os).
str();
264 operator std::string()
const {
return str(); }
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_POP
Definition config.h:268
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
Definition config.h:220
void_t< decltype(zeroerr::PrinterExt(std::declval< zeroerr::Printer & >(), std::declval< T & >(), 0, nullptr, zeroerr::rank< zeroerr::max_rank >()))> has_printer_ext
Definition print.h:47
void void_t
Definition typetraits.h:41
Definition benchmark.cpp:17
Printer & getStderrPrinter()
Definition print.cpp:12
void PrinterExt(Printer &, T, unsigned, const char *, rank< 0 >)
PrinterExt is an extension of Printer that allows user to write custom rules for printing.
Definition print.h:289
Printer & getStdoutPrinter()
Definition print.cpp:7
constexpr unsigned max_rank
Definition print.h:29
A functor class Printer for printing a value of any type.
Definition print.h:62
Printer()
Definition print.h:98
int indent
Definition print.h:106
bool clear_stream_before_printing
Definition print.h:110
Printer & operator()(Args &&... args)
Definition print.h:64
~Printer()
Definition print.h:99
void print(const std::tuple< Args... > &value, unsigned level, const char *lb, rank< 3 >)
Definition print.h:238
void print_tuple(const TupType &, unsigned, const char *, detail::seq<>)
Definition print.h:228
std::ostream & os
Definition print.h:108
bool isColorful
Definition print.h:103
static std::string demangle(const char *name)
Definition print.h:247
bool isQuoted
Definition print.h:105
static std::string type(const T &t)
Definition print.h:113
const char * quote()
Definition print.h:245
std::string str() const
Definition print.h:259
bool isCompact
Definition print.h:104
std::string tab(unsigned level)
Definition print.h:244
Printer & operator()(std::initializer_list< T > &&value)
Definition print.h:70
Printer(std::ostream &os)
Definition print.h:97
void call(T value)
Definition print.h:91
void check_stream()
Definition print.h:76
print(T value, unsigned level, const char *lb, rank< 0 >)
Definition print.h:122
void print_tuple(const TupType &_tup, unsigned level, const char *, detail::seq< I... >)
Definition print.h:231
bool use_stringstream
Definition print.h:109
const char * line_break
Definition print.h:107
void call(T value, V... others)
Definition print.h:85
friend std::ostream & operator<<(std::ostream &os, const Printer &P)
Definition print.h:266
Definition typetraits.h:63
has_extension is a type trait to check if user defined PrinterExt for a type
Definition print.h:42
Definition typetraits.h:60
rank is a helper class for Printer to define the priority of overloaded functions.
Definition typetraits.h:32
#define ZEROERR_IS_MAP
Definition typetraits.h:280
#define ZEROERR_IS_ENUM
Definition typetraits.h:265
#define ZEROERR_IS_CONTAINER
Definition typetraits.h:266
#define ZEROERR_IS_POD
Definition typetraits.h:281
#define ZEROERR_IS_FLOAT
Definition typetraits.h:264
#define ZEROERR_IS_INT
Definition typetraits.h:263
#define ZEROERR_IS_POINTER
Definition typetraits.h:268
#define ZEROERR_IS_CLASS
Definition typetraits.h:271
#define ZEROERR_IS_STREAMABLE
Definition typetraits.h:272
#define ZEROERR_IS_CHAR
Definition typetraits.h:269
#define ZEROERR_IS_WCHAR
Definition typetraits.h:270
#define ZEROERR_IS_STRING
Definition typetraits.h:267
#define ZEROERR_IS_AUTOPTR
Definition typetraits.h:276
#define ZEROERR_IS_COMPLEX
Definition typetraits.h:274
#define ZEROERR_IS_BOOL
Definition typetraits.h:275
#define ZEROERR_ENABLE_IF(x)
Definition typetraits.h:260
#define ZEROERR_IS_ARRAY
Definition typetraits.h:273