dbg: allow empty argument list

This commit is contained in:
Johannes Kapfhammer 2019-10-06 21:17:37 +02:00
parent daeda942ad
commit cca34a1ad7
2 changed files with 27 additions and 6 deletions

View File

@ -5,6 +5,7 @@ signed main() {
dbg("hi");
string s="world";
dbg(s);
dbg();
dbg(false);
dbg(true);
dbg('c');
@ -17,5 +18,5 @@ signed main() {
dbg(map<int, string>{{3,"three"},{1,"one"}});
dbg(unordered_map<int, string>{{3,"three"},{1,"one"}});
dbg(unordered_set<int>{3,1,4});
cout << "hi\n";
dbg();
}

View File

@ -183,6 +183,11 @@ auto dbg_print(const char (&msg)[N], std::string const &, char const *file,
return msg;
}
void dbg_print_status(char const *file, int line, char const *function_name) {
std::cerr << ANSI_VALUE << "[" << file << ":" << line << " (" << function_name
<< ")]" << ANSI_VALUE << '\n';
}
template <typename T> T &&identity(T &&t) { return std::forward<T>(t); }
} // end namespace detail
@ -192,11 +197,26 @@ template <typename T> T &&identity(T &&t) { return std::forward<T>(t); }
#ifdef SOI_RELEASE
#define dbg(...) dbg_macro::identity(__VA_ARGS__)
#else
#define dbg(...) \
soi::detail::dbg_print( \
(__VA_ARGS__), \
soi::detail::sanitized_type_name<decltype(__VA_ARGS__)>(), __FILE__, \
__LINE__, __func__, #__VA_ARGS__)
#define SOI_ARG16(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, ...) _15
#define SOI_IS_NONEMPTY(...) SOI_ARG16(1, ##__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0)
#define SOI_DBG_IMPL_0() \
soi::detail::dbg_print_status(__FILE__, __LINE__, __func__)
#define SOI_DBG_IMPL_1(...) \
soi::detail::dbg_print((__VA_ARGS__), \
soi::detail::sanitized_type_name<decltype(__VA_ARGS__)>(), __FILE__, \
__LINE__, __func__, #__VA_ARGS__)
#define SOI_CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
#define SOI_PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
#define SOI_DBG_IMPL(is_nonempty, ...) \
SOI_PRIMITIVE_CAT(SOI_DBG_IMPL_, is_nonempty)(__VA_ARGS__)
#define dbg(...) \
SOI_DBG_IMPL(SOI_IS_NONEMPTY(__VA_ARGS__), __VA_ARGS__)
#endif
#endif // SOI_DBG