bridge.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. #pragma once
  2. #include "consumer.h"
  3. #include "event.h"
  4. #include "writer.h"
  5. namespace NYsonPull {
  6. //! \brief Connect YSON stream producer and consumer.
  7. //!
  8. //! Useful for writing YSON stream filters.
  9. //! \p Producer must have a \p next_event() method (like \p NYsonPull::reader).
  10. //! \p Consumer must be like \p NYsonPull::consumer interface.
  11. template <typename Producer, typename Consumer>
  12. inline void Bridge(Producer&& producer, Consumer&& consumer) {
  13. for (;;) {
  14. auto& event = producer.NextEvent();
  15. consumer.OnEvent(event);
  16. if (event.Type() == EEventType::EndStream) {
  17. break;
  18. }
  19. }
  20. }
  21. template <typename Producer>
  22. inline void Bridge(Producer&& producer, TWriter& writer_) {
  23. Bridge(std::forward<Producer>(producer), writer_.GetConsumer());
  24. }
  25. template <typename Producer>
  26. inline void Bridge(Producer&& producer, TWriter&& writer_) {
  27. Bridge(std::forward<Producer>(producer), writer_.GetConsumer());
  28. }
  29. }