-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdebug_kit.h
114 lines (98 loc) · 2.71 KB
/
debug_kit.h
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* debug-kit header.
* written by Shuangquan Li, [email protected]
* created on 2016-5-10
* last edit on 2016-9-5 add __split_for_watch instead of split and strip in string_extended.h
*/
// NOTE: this is C++11 required
#ifndef __DEBUG_KIT_H__
#define __DEBUG_KIT_H__
#include "cil_config.h"
#include "output_extended.h"
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <cassert>
// convert all types of variables into string.
inline std::string to_string(const std::string& a) { return a; }
inline std::string to_string(const char* a) { return std::string(a); }
template<typename T> inline std::string to_string(const T& t) {
std::ostringstream os;
os << t;
return os.str();
}
#ifdef __cpp11
// split string s by ',' out of parenthesis, and remove white spaces of splited string.
inline std::vector<std::string> __split_for_watch(const std::string& s) {
std::vector<std::string> ret;
std::string subs;
int n = int(s.size());
int i = 0;
while (i < n && (s[i] == ',' || s[i] == ' ')) ++i;
for (; i < n; ++i) {
if (s[i] == ' ') continue;
if (s[i] == '(') {
int sum = -1;
subs += s[i];
while (sum) {
++i;
subs += s[i];
if (s[i] == '(') --sum;
if (s[i] == ')') ++sum;
}
continue;
}
if (s[i] == '<') {
int sum = -1;
subs += s[i];
while (sum) {
++i;
subs += s[i];
if (s[i] == '<') --sum;
if (s[i] == '>') ++sum;
}
continue;
}
if (s[i] == ',') {
ret.push_back(subs);
subs.clear();
while (i + 1 < n && (s[i + 1] == ',' || s[i + 1] == ' ')) ++i;
continue;
}
subs += s[i];
}
if (subs.size()) {
ret.push_back(subs);
subs.clear();
}
return ret;
}
// get the variables' name strings in vector
template <typename A>
inline void variable_value_to_string(std::vector<std::string>& values, const A& a) {
values.push_back(to_string(a));
}
template <typename A, typename... Args>
inline void variable_value_to_string(std::vector<std::string>& values, const A& a, const Args&... rest) {
values.push_back(to_string(a));
variable_value_to_string(values, rest...);
}
// get the variable names and values in string, then output in pair like "key = value"
#define watch(...) {\
std::vector<std::string> values;\
variable_value_to_string(values, __VA_ARGS__);\
std::string var_names = std::string(#__VA_ARGS__);\
std::vector<std::string> keys = __split_for_watch(var_names);\
for (size_t i = 0; i < values.size(); ++i) {\
std::cout << keys[i] << " = " << values[i] << std::endl;\
}\
std::cout << std::endl;\
}
#else
// define watch as nothing if no cplusplus-11 support.
#define watch /\
/
#endif
/* eof */
#endif