From cca34a1ad7656dd0f9a50fddbf79ba390a4822e5 Mon Sep 17 00:00:00 2001 From: Johannes Kapfhammer Date: Sun, 6 Oct 2019 21:17:37 +0200 Subject: [PATCH] dbg: allow empty argument list --- example.cpp | 3 ++- include/bits/soi-dbg.hpp | 30 +++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/example.cpp b/example.cpp index b2c81d9..b7b3195 100644 --- a/example.cpp +++ b/example.cpp @@ -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{{3,"three"},{1,"one"}}); dbg(unordered_map{{3,"three"},{1,"one"}}); dbg(unordered_set{3,1,4}); - cout << "hi\n"; + dbg(); } diff --git a/include/bits/soi-dbg.hpp b/include/bits/soi-dbg.hpp index 346b3ee..7da31f9 100644 --- a/include/bits/soi-dbg.hpp +++ b/include/bits/soi-dbg.hpp @@ -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 T &&identity(T &&t) { return std::forward(t); } } // end namespace detail @@ -192,11 +197,26 @@ template T &&identity(T &&t) { return std::forward(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(), __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(), __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