engine.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #pragma once
  2. #include <util/stream/output.h>
  3. #include <utility>
  4. #include <util/generic/strbuf.h>
  5. template <class T>
  6. struct TDumper {
  7. template <class S>
  8. static inline void Dump(S& s, const T& t) {
  9. s.Stream() << t;
  10. }
  11. };
  12. namespace NDumpPrivate {
  13. template <class T, class V>
  14. inline void Dump(T& t, const V& v) {
  15. ::TDumper<V>::Dump(t, v);
  16. }
  17. template <class T, class V>
  18. inline T&& operator<<(T&& t, V&& v) {
  19. Dump(t, v);
  20. return std::forward<T>(t);
  21. }
  22. struct TADLBase {
  23. };
  24. }
  25. struct TDumpBase: public ::NDumpPrivate::TADLBase {
  26. inline TDumpBase(IOutputStream& out, bool indent) noexcept
  27. : Out(&out)
  28. , IndentLevel(0)
  29. , Indent(indent)
  30. {
  31. }
  32. inline IOutputStream& Stream() const noexcept {
  33. return *Out;
  34. }
  35. void Char(char ch);
  36. void Char(wchar16 ch);
  37. void String(const TStringBuf& s);
  38. void String(const TWtringBuf& s);
  39. void Raw(const TStringBuf& s);
  40. IOutputStream* Out;
  41. size_t IndentLevel;
  42. bool Indent;
  43. };
  44. struct TIndentScope {
  45. inline TIndentScope(TDumpBase& d)
  46. : D(&d)
  47. {
  48. ++(D->IndentLevel);
  49. }
  50. inline ~TIndentScope() {
  51. --(D->IndentLevel);
  52. }
  53. TDumpBase* D;
  54. };
  55. template <class TChar>
  56. struct TRawLiteral {
  57. const TBasicStringBuf<TChar> S;
  58. };
  59. template <class TChar>
  60. static inline TRawLiteral<TChar> DumpRaw(const TBasicStringBuf<TChar>& s) noexcept {
  61. return {s};
  62. }
  63. template <class TChar>
  64. static inline TRawLiteral<TChar> DumpRaw(const TChar* s) noexcept {
  65. return {s};
  66. }
  67. template <class C>
  68. struct TDumper<TRawLiteral<C>> {
  69. template <class S>
  70. static inline void Dump(S& s, const TRawLiteral<C>& v) {
  71. s.Raw(v.S);
  72. }
  73. };
  74. struct TIndentNewLine {
  75. };
  76. static inline TIndentNewLine IndentNewLine() noexcept {
  77. return {};
  78. }
  79. template <>
  80. struct TDumper<TIndentNewLine> {
  81. template <class S>
  82. static inline void Dump(S& s, const TIndentNewLine&) {
  83. if (s.Indent) {
  84. s << DumpRaw("\n") << DumpRaw(TString(s.IndentLevel * 4, ' ').data());
  85. }
  86. }
  87. };
  88. template <class P>
  89. struct TDumper<const P*> {
  90. template <class S>
  91. static inline void Dump(S& s, const P* p) {
  92. s.Pointer(p);
  93. }
  94. };
  95. template <class P>
  96. struct TDumper<P*>: public TDumper<const P*> {
  97. };
  98. struct TCharDumper {
  99. template <class S, class V>
  100. static inline void Dump(S& s, const V& v) {
  101. s.Char(v);
  102. }
  103. };
  104. template <class S, class V>
  105. static inline void OutSequence(S& s, const V& v, const char* openTag, const char* closeTag) {
  106. s.ColorScheme.Markup(s);
  107. s << DumpRaw(openTag);
  108. {
  109. TIndentScope scope(s);
  110. size_t cnt = 0;
  111. for (const auto& x : v) {
  112. if (cnt) {
  113. s.ColorScheme.Markup(s);
  114. s << DumpRaw(", ");
  115. }
  116. s << IndentNewLine();
  117. s.ColorScheme.Literal(s);
  118. s << x;
  119. ++cnt;
  120. }
  121. }
  122. s << IndentNewLine();
  123. s.ColorScheme.Markup(s);
  124. s << DumpRaw(closeTag);
  125. s.ColorScheme.ResetType(s);
  126. }
  127. struct TAssocDumper {
  128. template <class S, class V>
  129. static inline void Dump(S& s, const V& v) {
  130. ::OutSequence(s, v, "{", "}");
  131. }
  132. };
  133. struct TSeqDumper {
  134. template <class S, class V>
  135. static inline void Dump(S& s, const V& v) {
  136. ::OutSequence(s, v, "[", "]");
  137. }
  138. };
  139. struct TStrDumper {
  140. template <class S, class V>
  141. static inline void Dump(S& s, const V& v) {
  142. s.ColorScheme.String(s);
  143. s.String(v);
  144. s.ColorScheme.ResetType(s);
  145. }
  146. };