common_view.h 778 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. //
  2. // common_view.h
  3. // stream
  4. //
  5. // Created by Sam Jaffe on 3/30/23.
  6. //
  7. #pragma once
  8. #include <iterator/sentinal_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::sentinal_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. }
  23. namespace stream::ranges::views {
  24. struct common {
  25. template <typename Stream> friend auto operator|(Stream && stream, common) {
  26. return common_view(FWD(stream));
  27. }
  28. };
  29. }
  30. #undef FWD