aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson_pull/detail/output/stream.h
blob: 494e298068597f52c624627f8ddd288bcff20af4 (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
#pragma once

#include "buffered.h"

#include <library/cpp/yson_pull/detail/macros.h>
#include <library/cpp/yson_pull/exceptions.h>

#include <util/stream/output.h>
#include <util/stream/file.h>
#include <util/system/file.h>

namespace NYsonPull {
    namespace NDetail {
        namespace NOutput {
            class TStream: public TBuffered<TStream> {
                IOutputStream* Output; 

            public:
                TStream(IOutputStream* output, size_t buffer_size) 
                    : TBuffered<TStream>(buffer_size)
                    , Output(output)
                {
                }

                void write(TStringBuf data) {
                    Output->Write(data);
                }
            };

            template <typename TOutput>
            class TOwned: public TBuffered<TOwned<TOutput>> {
                TOutput Output;

            public:
                template <typename... Args>
                TOwned(size_t buffer_size, Args&&... args)
                    : TBuffered<TOwned>(buffer_size)
                    , Output(std::forward<Args>(args)...)
                {
                }

                void write(TStringBuf data) {
                    Output.Write(data);
                }
            };

            class TFHandle: public TOwned<TUnbufferedFileOutput> {
            public:
                TFHandle(int fd, size_t buffer_size)
                    : TOwned<TUnbufferedFileOutput>(buffer_size, Duplicate(fd))
                {
                }
            };
        }
    }     // namespace NDetail
}