common.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // common_view.h
  3. // stream
  4. //
  5. // Created by Sam Jaffe on 3/30/23.
  6. //
  7. #pragma once
  8. #include <stream/forward.h>
  9. #include <stream/detail/traits.h>
  10. #include <stream/iterator/common_iterator.h>
  11. #include <stream/view/interface.h>
  12. #include <stream/detail/macro.h>
  13. namespace stream::ranges {
  14. template <typename S>
  15. class common_view
  16. : public view_interface<common_view<S>, common_iterator<detail::begin_t<S>,
  17. detail::end_t<S>>> {
  18. private:
  19. using iterator = common_iterator<detail::begin_t<S>, detail::end_t<S>>;
  20. private:
  21. S stream_;
  22. public:
  23. common_view(S && stream) : stream_(FWD(stream)) {}
  24. auto begin() const { return iterator(stream_.begin()); }
  25. auto end() const { return iterator(stream_.end()); }
  26. };
  27. template <typename S> common_view(S &&) -> common_view<S>;
  28. }
  29. namespace stream::ranges::views {
  30. struct common {
  31. template <typename Stream> friend auto operator|(Stream && stream, common) {
  32. if constexpr (detail::has_sentinal_v<Stream>) {
  33. return common_view(FWD(stream));
  34. } else {
  35. return FWD(stream);
  36. }
  37. }
  38. };
  39. }
  40. #include <stream/detail/undef.h>