range.h 810 B

1234567891011121314151617181920212223242526272829303132333435
  1. #pragma once
  2. #include "reader.h"
  3. #include <util/generic/iterator.h>
  4. namespace NYsonPull {
  5. class TStreamEventsRange: public TInputRangeAdaptor<TStreamEventsRange> {
  6. TReader Reader_;
  7. bool AtEnd;
  8. public:
  9. TStreamEventsRange(THolder<NInput::IStream> stream, EStreamType mode)
  10. : Reader_{std::move(stream), mode}
  11. , AtEnd(false)
  12. {
  13. }
  14. const TEvent* Last() const noexcept {
  15. return &Reader_.LastEvent();
  16. }
  17. const TEvent* Next() {
  18. if (Y_UNLIKELY(AtEnd)) {
  19. return nullptr;
  20. }
  21. auto* event = &Reader_.NextEvent();
  22. if (event->Type() == EEventType::EndStream) {
  23. AtEnd = true;
  24. }
  25. return event;
  26. }
  27. };
  28. }