dump.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #pragma once
  2. #include "engine.h"
  3. #include "dumpers.h"
  4. #include "auto.h"
  5. #include "colorscheme.h"
  6. #include <util/stream/format.h>
  7. #include <util/system/type_name.h>
  8. #include <util/generic/hash_set.h>
  9. #include <utility>
  10. /*
  11. * Cout << DbgDump(any) << Endl;
  12. * Cout << DbgDumpDeep(any) << Endl;
  13. * Cout << DbgDump(any).SetIndent(true) << Endl;
  14. *
  15. * specialize TDumper<your type> for extending dumper
  16. */
  17. namespace NPrivate {
  18. template <class TColorScheme>
  19. struct TTraitsShallow {
  20. struct TDump: public TDumpBase, public TColorSchemeContainer<TColorScheme> {
  21. template <typename... Args>
  22. inline TDump(Args&&... args)
  23. : TDumpBase(std::forward<Args>(args)...)
  24. {
  25. }
  26. template <class V>
  27. inline void Pointer(const V* v) {
  28. if (v) {
  29. *this << DumpRaw("(") << DumpRaw(TypeName(v).data()) << DumpRaw(")") << Hex((size_t)v);
  30. } else {
  31. *this << DumpRaw("(") << DumpRaw(TypeName<V>().data()) << DumpRaw("*)nullptr");
  32. }
  33. }
  34. };
  35. };
  36. template <class TColorScheme>
  37. struct TTraitsDeep {
  38. struct TDump: public TDumpBase, public TColorSchemeContainer<TColorScheme> {
  39. template <typename... Args>
  40. inline TDump(Args&&... args)
  41. : TDumpBase(std::forward<Args>(args)...)
  42. {
  43. }
  44. template <class V>
  45. inline void Pointer(const V* v) {
  46. if (v && !Visited.contains((size_t)v)) {
  47. Visited.insert((size_t)v);
  48. *this << DumpRaw("(") << DumpRaw(TypeName(v).data()) << DumpRaw(")") << Hex((size_t)v) << DumpRaw(" -> ") << *v;
  49. Visited.erase((size_t)v);
  50. } else {
  51. *this << DumpRaw("(") << DumpRaw(TypeName<V>().data()) << DumpRaw("*)nullptr");
  52. }
  53. }
  54. THashSet<size_t> Visited;
  55. };
  56. };
  57. template <class T, class TTraits>
  58. struct TDbgDump {
  59. inline TDbgDump(const T* t)
  60. : T_(t)
  61. , Indent(false)
  62. {
  63. }
  64. inline void DumpTo(IOutputStream& out) const {
  65. typename TTraits::TDump d(out, Indent);
  66. d << *T_;
  67. }
  68. inline TDbgDump& SetIndent(bool v) noexcept {
  69. Indent = v;
  70. return *this;
  71. }
  72. const T* T_;
  73. bool Indent;
  74. };
  75. template <class T, class TTraits>
  76. static inline IOutputStream& operator<<(IOutputStream& out, const TDbgDump<T, TTraits>& d) {
  77. d.DumpTo(out);
  78. return out;
  79. }
  80. }
  81. template <class T, class TColorScheme = DBG_OUTPUT_DEFAULT_COLOR_SCHEME>
  82. static inline ::NPrivate::TDbgDump<T, ::NPrivate::TTraitsShallow<TColorScheme>> DbgDump(const T& t) {
  83. return {std::addressof(t)};
  84. }
  85. template <class T, class TColorScheme = DBG_OUTPUT_DEFAULT_COLOR_SCHEME>
  86. static inline ::NPrivate::TDbgDump<T, ::NPrivate::TTraitsDeep<TColorScheme>> DbgDumpDeep(const T& t) {
  87. return {std::addressof(t)};
  88. }