common_view.h 985 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. #include <stream/detail/traits.h>
  11. #define FWD(x) std::forward<decltype(x)>(x)
  12. namespace stream::ranges {
  13. template <typename S> class common_view {
  14. private:
  15. S stream_;
  16. public:
  17. common_view(S && stream) : stream_(FWD(stream)) {}
  18. auto begin() const { return iterator::sentinel_iterator(stream_.begin()); }
  19. auto end() const { return decltype(begin())(); }
  20. auto empty() const {
  21. if constexpr (traits::has_empty_v<S>) {
  22. return stream_.empty();
  23. }
  24. }
  25. auto size() {
  26. if constexpr (traits::has_size_v<S>) {
  27. return stream_.size();
  28. }
  29. }
  30. };
  31. template <typename S> common_view(S &&) -> common_view<S>;
  32. }
  33. namespace stream::ranges::views {
  34. struct common {
  35. template <typename Stream> friend auto operator|(Stream && stream, common) {
  36. return common_view(FWD(stream));
  37. }
  38. };
  39. }
  40. #undef FWD