aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson_pull/bridge.h
blob: ac767dcba01b13ea0b1167d4c07b61756d1f2f4b (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
#pragma once

#include "consumer.h"
#include "event.h"
#include "writer.h"

namespace NYsonPull {
    //! \brief Connect YSON stream producer and consumer.
    //!
    //! Useful for writing YSON stream filters.
    //! \p Producer must have a \p next_event() method (like \p NYsonPull::reader).
    //! \p Consumer must be like \p NYsonPull::consumer interface.
    template <typename Producer, typename Consumer>
    inline void Bridge(Producer&& producer, Consumer&& consumer) {
        for (;;) {
            auto& event = producer.NextEvent();
            consumer.OnEvent(event);
            if (event.Type() == EEventType::EndStream) {
                break;
            }
        }
    }

    template <typename Producer>
    inline void Bridge(Producer&& producer, TWriter& writer_) {
        Bridge(std::forward<Producer>(producer), writer_.GetConsumer());
    }

    template <typename Producer>
    inline void Bridge(Producer&& producer, TWriter&& writer_) {
        Bridge(std::forward<Producer>(producer), writer_.GetConsumer());
    }

}