aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/holder.h
blob: 32752bb9f0bcb98fa7d9fded8e5f780774aa7652 (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
#pragma once

#include <util/generic/ptr.h>

#include <utility>
#include <type_traits>

class IInputStream;
class IOutputStream;

namespace NPrivate {
    template <class Stream, bool isInput = std::is_base_of<IInputStream, Stream>::value>
    struct TStreamBase {
        using TType = IInputStream;
    };

    template <class Stream>
    struct TStreamBase<Stream, false> {
        using TType = IOutputStream;
    };

} // namespace NPrivate

/**
 * An ownership-gaining wrapper for proxy streams.
 *
 * Example usage:
 * \code
 * TCountingInput* input = new THoldingStream<TCountingInput>(new TStringInput(s));
 * \encode
 *
 * In this example, resulting counting input also owns a string input that it
 * was constructed on top of.
 */
template <class Base, class StreamBase = typename ::NPrivate::TStreamBase<Base>::TType>
class THoldingStream: private THolder<StreamBase>, public Base {
public:
    template <class... Args>
    inline THoldingStream(THolder<StreamBase> stream, Args&&... args)
        : THolder<StreamBase>(std::move(stream))
        , Base(this->Get(), std::forward<Args>(args)...)
    {
    }
};