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/detail/macro.h>
  12. namespace stream::ranges {
  13. template <typename S> class common_view {
  14. private:
  15. using iterator = common_iterator<detail::begin_t<S>, detail::end_t<S>>;
  16. private:
  17. S stream_;
  18. public:
  19. common_view(S && stream) : stream_(FWD(stream)) {}
  20. auto begin() const { return iterator(stream_.begin()); }
  21. auto end() const { return iterator(stream_.end()); }
  22. SFINAE(detail::has_empty_v<S>, bool) empty() const { return stream_.empty(); }
  23. SFINAE(detail::has_size_v<S>, bool) size() const { return stream_.size(); }
  24. };
  25. template <typename S> common_view(S &&) -> common_view<S>;
  26. }
  27. namespace stream::ranges::views {
  28. struct common {
  29. template <typename Stream> friend auto operator|(Stream && stream, common) {
  30. if constexpr (detail::is_sentinal_v<Stream>) {
  31. return common_view(FWD(stream));
  32. } else {
  33. return FWD(stream);
  34. }
  35. }
  36. };
  37. }
  38. #include <stream/detail/undef.h>