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

#include "reader.h"

#include <util/generic/iterator.h>

namespace NYsonPull {
    class TStreamEventsRange: public TInputRangeAdaptor<TStreamEventsRange> { 
        TReader Reader_;
        bool AtEnd;

    public:
        TStreamEventsRange(THolder<NInput::IStream> stream, EStreamType mode)
            : Reader_{std::move(stream), mode}
            , AtEnd(false)
        {
        }

        const TEvent* Last() const noexcept {
            return &Reader_.LastEvent();
        }

        const TEvent* Next() {
            if (Y_UNLIKELY(AtEnd)) {
                return nullptr;
            }

            auto* event = &Reader_.NextEvent();
            if (event->Type() == EEventType::EndStream) {
                AtEnd = true;
            }
            return event;
        }
    };
}