Created
March 14, 2023 16:05
-
-
Save player131007/7630205b462163c72bc1b62aaf640f0d to your computer and use it in GitHub Desktop.
Make your debug printing life easier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef debug_macro | |
#define debug_macro | |
#if __cplusplus < 201402L | |
#error "debug header requires C++14 and above" | |
#endif | |
#include <iosfwd> | |
#include <utility> | |
#include <type_traits> | |
namespace detail { | |
struct dblock { | |
static int cnt; | |
dblock() {++cnt;} | |
~dblock() {--cnt;} | |
}; | |
int dblock::cnt=0; | |
template<typename It> | |
struct rge { | |
It b,e; | |
rge(It b,It e) : b(b), e(e) {} | |
}; | |
template<typename ...T, size_t ...Is> | |
void print_tup(std::ostream &os, const std::tuple<T...> &t, std::index_sequence<Is...>) { | |
os << '('; | |
using swallow = int[]; | |
(void)swallow{0, ((os << (Is==0 ? "" : ", ") << std::get<Is>(t)),0)...}; | |
os << ')'; | |
} | |
} | |
template<typename T1,typename T2> | |
std::ostream& operator<<(std::ostream &os, const std::pair<T1,T2> &p) { | |
return os << '(' << p.first << ", " << p.second << ')'; | |
} | |
template<typename It> | |
std::ostream& operator<<(std::ostream &os,const detail::rge<It> &r) { | |
os << '{'; | |
for(It i=r.b;i!=r.e;++i) { | |
if(i!=r.b) os << ", "; | |
os << *i; | |
} | |
return os << '}'; | |
} | |
template<typename ...T> | |
std::ostream& operator<<(std::ostream &os, const std::tuple<T...> &t) { | |
detail::print_tup(os,t,std::make_index_sequence<sizeof...(T)>()); | |
return os; | |
} | |
#define cerr for(int i=0;i<detail::dblock::cnt;i++) cerr << '\t'; cerr | |
#define dbg(val) "["#val": " << (val) << "]" | |
#define dbr(a,b,e) "["#a": " << detail::rge<common_type_t<decltype(b),decltype(c)>>(b,e) << "]" | |
#define dbfr(a) dbr(a,begin(a),end(a)) | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment