common_view.h 838 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //
  2. // common_view.h
  3. // stream
  4. //
  5. // Created by Sam Jaffe on 3/30/23.
  6. //
  7. #pragma once
  8. #include <iterator/sentinel_iterator.h>
  9. #include <stream/forward.h>
  10. #define FWD(x) std::forward<decltype(x)>(x)
  11. namespace stream::ranges {
  12. template <typename S> class common_view {
  13. private:
  14. S stream_;
  15. public:
  16. common_view(S && stream) : stream_(FWD(stream)) {}
  17. auto begin() const { return iterator::sentinel_iterator(stream_.begin()); }
  18. auto end() const { return decltype(begin())(); }
  19. bool empty() const { return stream_.empty(); }
  20. size_t size() const { return stream_.size(); }
  21. };
  22. template <typename S> common_view(S &&) -> common_view<S>;
  23. }
  24. namespace stream::ranges::views {
  25. struct common {
  26. template <typename Stream> friend auto operator|(Stream && stream, common) {
  27. return common_view(FWD(stream));
  28. }
  29. };
  30. }
  31. #undef FWD