aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/dbg_output/engine.h
blob: f13c728c391ce8db3ebc4e5fdc22509a7141a91d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#pragma once

#include <util/stream/output.h>

#include <utility>
#include <util/generic/strbuf.h>

template <class T>
struct TDumper {
    template <class S>
    static inline void Dump(S& s, const T& t) {
        s.Stream() << t;
    }
};

namespace NDumpPrivate {
    template <class T, class V>
    inline void Dump(T& t, const V& v) {
        ::TDumper<V>::Dump(t, v);
    }

    template <class T, class V>
    inline T&& operator<<(T&& t, V&& v) {
        Dump(t, v);

        return std::forward<T>(t);
    }

    struct TADLBase {
    };
}

struct TDumpBase: public ::NDumpPrivate::TADLBase {
    inline TDumpBase(IOutputStream& out, bool indent) noexcept
        : Out(&out)
        , IndentLevel(0)
        , Indent(indent)
    {
    }

    inline IOutputStream& Stream() const noexcept {
        return *Out;
    }

    void Char(char ch);
    void Char(wchar16 ch);

    void String(const TStringBuf& s);
    void String(const TWtringBuf& s);

    void Raw(const TStringBuf& s);

    IOutputStream* Out;
    size_t IndentLevel;
    bool Indent;
};

struct TIndentScope {
    inline TIndentScope(TDumpBase& d)
        : D(&d)
    {
        ++(D->IndentLevel);
    }

    inline ~TIndentScope() {
        --(D->IndentLevel);
    }

    TDumpBase* D;
};

template <class TChar>
struct TRawLiteral {
    const TBasicStringBuf<TChar> S;
};

template <class TChar>
static inline TRawLiteral<TChar> DumpRaw(const TBasicStringBuf<TChar>& s) noexcept {
    return {s};
}

template <class TChar>
static inline TRawLiteral<TChar> DumpRaw(const TChar* s) noexcept {
    return {s};
}

template <class C>
struct TDumper<TRawLiteral<C>> {
    template <class S>
    static inline void Dump(S& s, const TRawLiteral<C>& v) {
        s.Raw(v.S);
    }
};

struct TIndentNewLine {
};

static inline TIndentNewLine IndentNewLine() noexcept {
    return {};
}

template <>
struct TDumper<TIndentNewLine> {
    template <class S>
    static inline void Dump(S& s, const TIndentNewLine&) {
        if (s.Indent) {
            s << DumpRaw("\n") << DumpRaw(TString(s.IndentLevel * 4, ' ').data());
        }
    }
};

template <class P>
struct TDumper<const P*> {
    template <class S>
    static inline void Dump(S& s, const P* p) {
        s.Pointer(p);
    }
};

template <class P>
struct TDumper<P*>: public TDumper<const P*> {
};

struct TCharDumper {
    template <class S, class V>
    static inline void Dump(S& s, const V& v) {
        s.Char(v);
    }
};

template <class S, class V>
static inline void OutSequence(S& s, const V& v, const char* openTag, const char* closeTag) {
    s.ColorScheme.Markup(s);
    s << DumpRaw(openTag);

    {
        TIndentScope scope(s);
        size_t cnt = 0;

        for (const auto& x : v) {
            if (cnt) {
                s.ColorScheme.Markup(s);
                s << DumpRaw(", ");
            }

            s << IndentNewLine();
            s.ColorScheme.Literal(s);
            s << x;
            ++cnt;
        }
    }

    s << IndentNewLine();
    s.ColorScheme.Markup(s);
    s << DumpRaw(closeTag);
    s.ColorScheme.ResetType(s);
}

struct TAssocDumper {
    template <class S, class V>
    static inline void Dump(S& s, const V& v) {
        ::OutSequence(s, v, "{", "}");
    }
};

struct TSeqDumper {
    template <class S, class V>
    static inline void Dump(S& s, const V& v) {
        ::OutSequence(s, v, "[", "]");
    }
};

struct TStrDumper {
    template <class S, class V>
    static inline void Dump(S& s, const V& v) {
        s.ColorScheme.String(s);
        s.String(v);
        s.ColorScheme.ResetType(s);
    }
};