diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/dbg_output/dump.h | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/dbg_output/dump.h')
-rw-r--r-- | library/cpp/dbg_output/dump.h | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/library/cpp/dbg_output/dump.h b/library/cpp/dbg_output/dump.h new file mode 100644 index 0000000000..c7efa105ee --- /dev/null +++ b/library/cpp/dbg_output/dump.h @@ -0,0 +1,106 @@ +#pragma once + +#include "engine.h" +#include "dumpers.h" +#include "auto.h" +#include "colorscheme.h" + +#include <util/stream/format.h> +#include <util/system/type_name.h> +#include <util/generic/hash_set.h> +#include <utility> + +/* + * Cout << DbgDump(any) << Endl; + * Cout << DbgDumpDeep(any) << Endl; + * Cout << DbgDump(any).SetIndent(true) << Endl; + * + * specialize TDumper<your type> for extending dumper + */ + +namespace NPrivate { + template <class TColorScheme> + struct TTraitsShallow { + struct TDump: public TDumpBase, public TColorSchemeContainer<TColorScheme> { + template <typename... Args> + inline TDump(Args&&... args) + : TDumpBase(std::forward<Args>(args)...) + { + } + + template <class V> + inline void Pointer(const V* v) { + if (v) { + *this << DumpRaw("(") << DumpRaw(TypeName(v).data()) << DumpRaw(")") << Hex((size_t)v); + } else { + *this << DumpRaw("(") << DumpRaw(TypeName<V>().data()) << DumpRaw("*)nullptr"); + } + } + }; + }; + + template <class TColorScheme> + struct TTraitsDeep { + struct TDump: public TDumpBase, public TColorSchemeContainer<TColorScheme> { + template <typename... Args> + inline TDump(Args&&... args) + : TDumpBase(std::forward<Args>(args)...) + { + } + + template <class V> + inline void Pointer(const V* v) { + if (v && !Visited.contains((size_t)v)) { + Visited.insert((size_t)v); + *this << DumpRaw("(") << DumpRaw(TypeName(v).data()) << DumpRaw(")") << Hex((size_t)v) << DumpRaw(" -> ") << *v; + Visited.erase((size_t)v); + } else { + *this << DumpRaw("(") << DumpRaw(TypeName<V>().data()) << DumpRaw("*)nullptr"); + } + } + + THashSet<size_t> Visited; + }; + }; + + template <class T, class TTraits> + struct TDbgDump { + inline TDbgDump(const T* t) + : T_(t) + , Indent(false) + { + } + + inline void DumpTo(IOutputStream& out) const { + typename TTraits::TDump d(out, Indent); + + d << *T_; + } + + inline TDbgDump& SetIndent(bool v) noexcept { + Indent = v; + + return *this; + } + + const T* T_; + bool Indent; + }; + + template <class T, class TTraits> + static inline IOutputStream& operator<<(IOutputStream& out, const TDbgDump<T, TTraits>& d) { + d.DumpTo(out); + + return out; + } +} + +template <class T, class TColorScheme = DBG_OUTPUT_DEFAULT_COLOR_SCHEME> +static inline ::NPrivate::TDbgDump<T, ::NPrivate::TTraitsShallow<TColorScheme>> DbgDump(const T& t) { + return {std::addressof(t)}; +} + +template <class T, class TColorScheme = DBG_OUTPUT_DEFAULT_COLOR_SCHEME> +static inline ::NPrivate::TDbgDump<T, ::NPrivate::TTraitsDeep<TColorScheme>> DbgDumpDeep(const T& t) { + return {std::addressof(t)}; +} |