ZeroErr
载入中...
搜索中...
未找到
dbg.h
浏览该文件的文档.
1#pragma once
3
4#include "zeroerr/color.h"
5#include "zeroerr/print.h"
6
7
8#include <tuple> // for std::get and std::tie
9
11
12#ifndef ZEROERR_DISABLE_DBG_MARCO
13#define dbg(...) zeroerr::DebugExpr(__FILE__, __LINE__, __func__, #__VA_ARGS__, __VA_ARGS__)
14#else
15#define dbg(...) (__VA_ARGS__)
16#endif
17
18namespace zeroerr {
19
20template <class T1, class... T>
21struct last {
22 using type = typename last<T...>::type;
23};
24
25template <class T1>
26struct last<T1> {
27 using type = T1;
28};
29
34template <typename... Args>
35auto get_last(Args&&... args) -> typename last<Args...>::type {
36 return std::get<sizeof...(Args) - 1>(std::tie(args...));
37}
38
39
44template <typename... T>
45auto DebugExpr(const char* file, unsigned line, const char* func, const char* exprs, T... t) ->
46 typename last<T...>::type {
47 zeroerr::String fileName(file);
48 auto p = fileName.find_last_of('/');
49 if (p != zeroerr::String::npos) fileName = fileName.substr(p + 1);
50
51 getStderrPrinter().os << Dim << "[" << fileName << ":" << line << " " << func << "] " << Reset;
52 getStderrPrinter().os << FgCyan << exprs << Reset << " = ";
53 Printer print(getStderrPrinter().os);
54 print.line_break = "";
55 print(t...);
56 getStderrPrinter().os << " (" << FgGreen;
57 zeroerr::String typenames[] = {print.type(t)...};
58 for (unsigned i = 0; i < sizeof...(T); ++i) {
59 if (i != 0) getStderrPrinter().os << ", ";
60 getStderrPrinter().os << typenames[i];
61 }
62 getStderrPrinter().os << Reset << ")" << '\n';
63 return get_last(t...);
64}
65
66
67} // namespace zeroerr
68
Definition string.h:41
size_type find_last_of(char value, size_type pos=npos) const noexcept
Definition string.cpp:109
String substr(size_type pos=0, size_type count=npos) const
Definition string.cpp:127
static constexpr size_type npos
Definition string.h:47
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_POP
Definition config.h:283
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
Definition config.h:235
Definition assert_text.cpp:5
const char * FgCyan
Definition color.cpp:48
auto DebugExpr(const char *file, unsigned line, const char *func, const char *exprs, T... t) -> typename last< T... >::type
DebugExpr is a function to print any type of variable with its type name. It is used by dbg macro.
Definition dbg.h:45
const char * Dim
Definition color.cpp:36
const char * FgGreen
Definition color.cpp:44
Printer & getStderrPrinter()
Definition print.cpp:14
auto get_last(Args &&... args) -> typename last< Args... >::type
get_last is a function to get the last argument of a variadic template. It is used by DebugExpr.
Definition dbg.h:35
const char * Reset
Definition color.cpp:34
A functor class Printer for printing a value of any type.
Definition print.h:74
StringBuffer & os
Definition print.h:121
static zeroerr::String type(const T &t)
Definition print.h:126
const char * line_break
Definition print.h:120
T1 type
Definition dbg.h:27
Definition dbg.h:21
typename last< T... >::type type
Definition dbg.h:22