proxy.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #pragma once
  2. #include <iterator/facade.h>
  3. #include <iterator/forwards.h>
  4. #include <iterator/detail/macro.h>
  5. namespace iterator {
  6. template <typename It, typename Self, typename Cat>
  7. class Proxy : public Facade<Self> {
  8. public:
  9. static constexpr bool single_pass_iterator = true;
  10. private:
  11. It impl_;
  12. public:
  13. Proxy() = default;
  14. Proxy(It impl) : impl_(impl) {}
  15. template <typename... Args> Proxy(Args &&... args) : impl_(FWD(args)...) {}
  16. decltype(auto) dereference() const { return *impl_; }
  17. void increment() { ++impl_; }
  18. bool equal_to(Self const & other) const { return impl_ == other.impl_; }
  19. bool at_end() const
  20. requires(has_sentinel<It>)
  21. {
  22. return impl() == typename It::sentinel_type();
  23. }
  24. protected:
  25. auto impl() const { return impl_; }
  26. };
  27. template <typename It, typename Self>
  28. class Proxy<It, Self, std::forward_iterator_tag> : public Facade<Self> {
  29. private:
  30. It impl_;
  31. public:
  32. Proxy() = default;
  33. Proxy(It impl) : impl_(impl) {}
  34. template <typename... Args> Proxy(Args &&... args) : impl_(FWD(args)...) {}
  35. decltype(auto) dereference() const { return *impl_; }
  36. void increment() { ++impl_; }
  37. bool equal_to(Self const & other) const { return impl_ == other.impl_; }
  38. bool at_end() const
  39. requires(has_sentinel<It>)
  40. {
  41. return impl() == typename It::sentinel_type();
  42. }
  43. protected:
  44. auto impl() const { return impl_; }
  45. };
  46. template <typename It, typename Self>
  47. class Proxy<It, Self, std::bidirectional_iterator_tag> : public Facade<Self> {
  48. private:
  49. It impl_;
  50. public:
  51. Proxy() = default;
  52. Proxy(It impl) : impl_(impl) {}
  53. template <typename... Args> Proxy(Args &&... args) : impl_(FWD(args)...) {}
  54. decltype(auto) dereference() const { return *impl_; }
  55. void increment() { ++impl_; }
  56. void decrement() { --impl_; }
  57. bool equal_to(Self const & other) const { return impl_ == other.impl_; }
  58. bool at_end() const
  59. requires(has_sentinel<It>)
  60. {
  61. return impl() == typename It::sentinel_type();
  62. }
  63. protected:
  64. auto impl() const { return impl_; }
  65. };
  66. template <typename It, typename Self>
  67. class Proxy<It, Self, std::random_access_iterator_tag> : public Facade<Self> {
  68. public:
  69. using difference_type = typename std::iterator_traits<It>::difference_type;
  70. private:
  71. It impl_;
  72. public:
  73. Proxy() = default;
  74. Proxy(It impl) : impl_(impl) {}
  75. template <typename... Args> Proxy(Args &&... args) : impl_(FWD(args)...) {}
  76. decltype(auto) dereference() const { return *impl_; }
  77. void advance(difference_type off) { impl_ += off; }
  78. bool equal_to(Self const & other) const { return impl_ == other.impl_; }
  79. difference_type distance_to(Self const & other) const {
  80. return other.impl_ - impl_;
  81. }
  82. friend auto operator-(sentinel_for<It> auto sentinel, Self const & self) {
  83. return sentinel - self.impl();
  84. }
  85. bool at_end() const
  86. requires(has_sentinel<It>)
  87. {
  88. return (typename It::sentinel_type() - impl()) <= 0;
  89. }
  90. protected:
  91. auto impl() const { return impl_; }
  92. };
  93. }
  94. #include <iterator/detail/undef.h>