aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/output.h
blob: 3b40c4396fb8d45df2bf01a52326590dcc637c21 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#pragma once

#include "fwd.h" 
#include "labeled.h"

#include <util/generic/noncopyable.h>
#include <util/generic/string.h>
#include <util/generic/strbuf.h>
#include <util/generic/typetraits.h>

#include <type_traits>

/**
 * @addtogroup Streams_Base
 * @{
 */

/**
 * Abstract output stream.
 */
class IOutputStream: public TNonCopyable { 
public:
    /**
     * Data block for output.
     */
    struct TPart {
        inline TPart(const void* Buf, size_t Len) noexcept
            : buf(Buf)
            , len(Len)
        {
        }

        inline TPart(const TStringBuf s) noexcept 
            : buf(s.data())
            , len(s.size())
        {
        }

        inline TPart() noexcept
            : buf(nullptr)
            , len(0)
        {
        }

        inline ~TPart() = default;

        static inline TPart CrLf() noexcept {
            return TPart("\r\n", 2);
        }

        const void* buf;
        size_t len;
    };

    IOutputStream() noexcept; 
    virtual ~IOutputStream(); 

    IOutputStream(IOutputStream&&) noexcept { 
    }

    IOutputStream& operator=(IOutputStream&&) noexcept { 
        return *this;
    };

    /**
     * Writes into this stream.
     *
     * @param buf                       Data to write.
     * @param len                       Number of bytes to write.
     */
    inline void Write(const void* buf, size_t len) {
        if (len) {
            DoWrite(buf, len);
        }
    }

    /**
     * Writes a string into this stream.
     *
     * @param st                        String to write.
     */
    inline void Write(const TStringBuf st) { 
        Write(st.data(), st.size());
    }

    /**
     * Writes several data blocks into this stream.
     *
     * @param parts                     Pointer to the start of the data blocks
     *                                  array.
     * @param count                     Number of data blocks to write.
     */
    inline void Write(const TPart* parts, size_t count) {
        if (count > 1) {
            DoWriteV(parts, count);
        } else if (count) {
            DoWrite(parts->buf, parts->len);
        }
    }

    /**
     * Writes a single character into this stream.
     *
     * @param ch                        Character to write.
     */
    inline void Write(char ch) {
        DoWriteC(ch);
    }

    /**
     * Flushes this stream's buffer, if any.
     *
     * Note that this can also be done with a `Flush` manipulator:
     * @code
     * stream << "some string" << Flush;
     * @endcode
     */
    inline void Flush() {
        DoFlush();
    }

    /**
     * Flushes and closes this stream. No more data can be written into a stream
     * once it's closed.
     */
    inline void Finish() {
        DoFinish();
    }

protected:
    /**
     * Writes into this stream.
     *
     * @param buf                       Data to write.
     * @param len                       Number of bytes to write.
     * @throws yexception               If IO error occurs.
     */
    virtual void DoWrite(const void* buf, size_t len) = 0;

    /**
     * Writes several data blocks into this stream.
     *
     * @param parts                     Pointer to the start of the data blocks
     *                                  array.
     * @param count                     Number of data blocks to write.
     * @throws yexception               If IO error occurs.
     */
    virtual void DoWriteV(const TPart* parts, size_t count);

    /**
     * Writes a single character into this stream. Can be overridden with a faster implementation.
     *
     * @param ch                        Character to write.
     */
    virtual void DoWriteC(char ch);

    /**
     * Flushes this stream's buffer, if any.
     *
     * @throws yexception               If IO error occurs.
     */
    virtual void DoFlush();

    /**
     * Flushes and closes this stream. No more data can be written into a stream
     * once it's closed.
     *
     * @throws yexception               If IO error occurs.
     */
    virtual void DoFinish();
};

/**
 * `operator<<` for `IOutputStream` by default delegates to this function. 
 *
 * Note that while `operator<<` uses overloading (and thus argument-dependent
 * lookup), `Out` uses template specializations. This makes it possible to
 * have a single `Out` declaration, and then just provide specializations in
 * cpp files, letting the linker figure everything else out. This approach
 * reduces compilation times.
 *
 * However, if the flexibility of overload resolution is needed, then one should
 * just overload `operator<<`.
 *
 * @param out                           Output stream to write into.
 * @param value                         Value to write.
 */
template <class T>
void Out(IOutputStream& out, typename TTypeTraits<T>::TFuncParam value); 

#define Y_DECLARE_OUT_SPEC(MODIF, T, stream, value) \ 
    template <>                                     \ 
    MODIF void Out<T>(IOutputStream & stream, TTypeTraits<T>::TFuncParam value) 

template <>
inline void Out<const char*>(IOutputStream& o, const char* t) { 
    if (t) {
        o.Write(t);
    } else {
        o.Write("(null)");
    }
}

template <>
void Out<const wchar16*>(IOutputStream& o, const wchar16* w); 

template <>
void Out<const wchar32*>(IOutputStream& o, const wchar32* w);

static inline IOutputStream& operator<<(IOutputStream& o, TStreamManipulator m) { 
    m(o);

    return o;
}

static inline IOutputStream& operator<<(IOutputStream& o, const char* t) { 
    Out<const char*>(o, t);

    return o;
}

static inline IOutputStream& operator<<(IOutputStream& o, char* t) { 
    Out<const char*>(o, t);

    return o;
}

template <class T>
static inline std::enable_if_t<std::is_scalar<T>::value, IOutputStream&> operator<<(IOutputStream& o, T t) {
    Out<T>(o, t);

    return o;
}

template <class T>
static inline std::enable_if_t<!std::is_scalar<T>::value, IOutputStream&> operator<<(IOutputStream& o, const T& t) {
    Out<T>(o, t);

    return o;
}

static inline IOutputStream& operator<<(IOutputStream& o, const wchar16* t) { 
    Out<const wchar16*>(o, t);
    return o;
}

static inline IOutputStream& operator<<(IOutputStream& o, wchar16* t) { 
    Out<const wchar16*>(o, t);
    return o;
}

static inline IOutputStream& operator<<(IOutputStream& o, const wchar32* t) {
    Out<const wchar32*>(o, t);
    return o;
}

static inline IOutputStream& operator<<(IOutputStream& o, wchar32* t) {
    Out<const wchar32*>(o, t);
    return o;
}

namespace NPrivate {
    IOutputStream& StdOutStream() noexcept; 
    IOutputStream& StdErrStream() noexcept; 
}

/**
 * Standard output stream.
 */
#define Cout (::NPrivate::StdOutStream())

/**
 * Standard error stream.
 */
#define Cerr (::NPrivate::StdErrStream())

/**
 * Standard log stream.
 */
#define Clog Cerr

/**
 * End-of-line output manipulator, basically the same as `std::endl`.
 */
static inline void Endl(IOutputStream& o) { 
    (o << '\n').Flush();
}

/**
 * Flushing stream manipulator, basically the same as `std::flush`.
 */
static inline void Flush(IOutputStream& o) { 
    o.Flush();
}

/*
 * Also see format.h for additional manipulators.
 */

#include "debug.h"

void RedirectStdioToAndroidLog(bool redirect);

/** @} */