Repository navigation
Expand file tree
/
Copy pathdebug.hpp
More file actions
83 lines (70 loc) · 1.71 KB
/
Copy pathdebug.hpp
File metadata and controls
83 lines (70 loc) · 1.71 KB
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
#pragma once
#include <iostream>
#include <iterator>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
namespace debug {
#ifndef ONLINE_JUDGE
namespace detail {
template <class T>
concept StringLike = std::is_convertible_v<T, std::string_view>;
template <class T>
concept Iterable = requires(const T &value) {
std::begin(value);
std::end(value);
} && !StringLike<T>;
template <class T>
void print(const T &value);
template <class T>
void print(const T &value) {
if constexpr (StringLike<T>) {
std::cerr << value;
} else if constexpr (requires { value.first; value.second; }) {
std::cerr << '(';
print(value.first);
std::cerr << ", ";
print(value.second);
std::cerr << ')';
} else if constexpr (Iterable<T>) {
std::cerr << '[';
bool first = true;
for (const auto &item : value) {
if (!first) {
std::cerr << ", ";
}
first = false;
print(item);
}
std::cerr << ']';
} else {
std::cerr << value;
}
}
inline void log_names(std::string_view names) {
std::cerr << names << " = ";
}
template <class T, class... Rest>
void log_values(const T &value, const Rest &...rest) {
print(value);
if constexpr (sizeof...(rest) > 0) {
std::cerr << ", ";
log_values(rest...);
} else {
std::cerr << '\n';
}
}
}
template <class... Args>
void log(std::string_view names, const Args &...args) {
detail::log_names(names);
detail::log_values(args...);
}
#endif
}
#ifndef ONLINE_JUDGE
#define dbg(...) ::debug::log(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...) do { } while (false)
#endif