any_view.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // view.h
  3. // stream
  4. //
  5. // Created by Sam Jaffe on 3/29/23.
  6. //
  7. #pragma once
  8. #include <memory>
  9. #include <stream/common_view.h>
  10. #include <stream/detail/traits.h>
  11. #include <stream/forward.h>
  12. namespace stream::ranges {
  13. template <typename T> class view_iterator : public facade<view_iterator<T>> {
  14. private:
  15. T (*dereference_)(void *){nullptr};
  16. bool (*equal_to_)(void *, void *){nullptr};
  17. void (*increment_)(void *){nullptr};
  18. std::shared_ptr<void> impl_{nullptr};
  19. public:
  20. template <typename It>
  21. view_iterator(It impl)
  22. : dereference_([](void * p) -> T { return **((It *)p); }),
  23. equal_to_([](void * l, void * r) { return *((It *)l) == *((It *)r); }),
  24. increment_([](void * p) { ++(*(It *)(p)); }),
  25. impl_(std::make_shared<It>(impl)) {}
  26. T dereference() const { return dereference_(impl_.get()); }
  27. void increment() { increment_(impl_.get()); }
  28. bool equal_to(view_iterator const & other) const {
  29. return impl_ == other.impl_ ||
  30. (impl_ && other.impl_ && equal_to_(impl_.get(), other.impl_.get()));
  31. }
  32. };
  33. template <typename T> class any_view {
  34. private:
  35. template <typename S>
  36. using sentinel_iterator =
  37. common_iterator<detail::begin_t<S>, detail::end_t<S>>;
  38. template <typename S, typename It>
  39. using iter_cast_t =
  40. std::conditional_t<detail::is_sentinal_v<S>, sentinel_iterator<S>, It>;
  41. using make_iter_t = view_iterator<T> (*)(void *);
  42. private:
  43. std::shared_ptr<void> impl_{nullptr};
  44. make_iter_t begin_{nullptr};
  45. make_iter_t end_{nullptr};
  46. public:
  47. template <typename S>
  48. any_view(std::shared_ptr<S> impl)
  49. : impl_(impl), begin_(begin_function<S>()), end_(end_function<S>()) {}
  50. template <typename S>
  51. any_view(S & impl) : any_view(std::shared_ptr<S>(&impl, [](void *) {})) {}
  52. template <typename S>
  53. any_view(S const & impl)
  54. : any_view(std::shared_ptr<S const>(&impl, [](void *) {})) {}
  55. template <typename S>
  56. any_view(S && impl) : any_view(std::make_shared<S>(std::forward<S>(impl))) {}
  57. auto begin() const { return begin_(impl_.get()); }
  58. auto end() const { return end_(impl_.get()); }
  59. private:
  60. template <typename S> static make_iter_t begin_function() {
  61. return [](void * p) -> view_iterator<T> {
  62. return iter_cast_t<S, detail::begin_t<S>>(static_cast<S *>(p)->begin());
  63. };
  64. }
  65. template <typename S> static make_iter_t end_function() {
  66. return [](void * p) -> view_iterator<T> {
  67. return iter_cast_t<S, detail::end_t<S>>(static_cast<S *>(p)->end());
  68. };
  69. }
  70. };
  71. template <typename S>
  72. any_view(std::shared_ptr<S>) -> any_view<detail::value_type<S>>;
  73. template <typename S> any_view(S &) -> any_view<detail::value_type<S>>;
  74. template <typename S> any_view(S const &) -> any_view<detail::value_type<S>>;
  75. template <typename S> any_view(S &&) -> any_view<detail::value_type<S>>;
  76. }