blob: d3c1c11970640f0bf708b21f25c0b5082d56440c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#pragma once
#include <sstream>
#include <string>
class DebugPrintVisitor
{
public:
DebugPrintVisitor(std::string const & name) : m_name(name) {}
template <typename T>
void operator()(T const & t, char const * name = nullptr)
{
if (!m_empty)
m_os << ", ";
m_empty = false;
if (name)
m_os << name << ": ";
m_os << DebugPrint(t);
}
std::string ToString() const { return m_name + " [" + m_os.str() + "]"; }
private:
std::string m_name;
bool m_empty = true;
std::ostringstream m_os;
};
#define DECLARE_VISITOR(...) \
template <typename Visitor> \
void Visit(Visitor & visitor) \
{ \
__VA_ARGS__; \
} \
template <typename Visitor> \
void Visit(Visitor & visitor) const \
{ \
__VA_ARGS__; \
}
#define DECLARE_DEBUG_PRINT(className) \
friend std::string DebugPrint(className const & c) \
{ \
DebugPrintVisitor visitor(#className); \
c.Visit(visitor); \
return visitor.ToString(); \
}
#define DECLARE_VISITOR_AND_DEBUG_PRINT(className, ...) \
DECLARE_VISITOR(__VA_ARGS__) \
DECLARE_DEBUG_PRINT(className)
|