facade.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #pragma once
  2. #include <iterator>
  3. #include <type_traits>
  4. #include <iterator/detail/arrow_proxy.h>
  5. #include <iterator/detail/facade_traits.h>
  6. #include <iterator/detail/traits.h>
  7. namespace iterator {
  8. template <typename D, typename T>
  9. using difference_type_arg_t =
  10. std::enable_if_t<std::is_convertible_v<D, detail::distance_to_t<T>>>;
  11. template <typename self_type> class facade {
  12. public:
  13. decltype(auto) operator*() const { return self().dereference(); }
  14. decltype(auto) operator->() const {
  15. if constexpr (std::is_reference<decltype(**this)>{}) {
  16. return std::addressof(**this);
  17. } else {
  18. return detail::arrow_proxy{**this};
  19. }
  20. }
  21. template <typename D, typename = difference_type_arg_t<D, self_type>>
  22. decltype(auto) operator[](D off) const {
  23. return *(self() + off);
  24. }
  25. self_type & operator++() {
  26. if constexpr (detail::is_advanceable_iterator_v<self_type>) {
  27. self() += 1;
  28. } else {
  29. self().increment();
  30. }
  31. return self();
  32. }
  33. auto operator++(int) {
  34. if constexpr (detail::is_single_pass_iterator_v<self_type>) {
  35. ++*this;
  36. } else {
  37. auto tmp = self();
  38. ++*this;
  39. return tmp;
  40. }
  41. }
  42. self_type & operator--() {
  43. if constexpr (detail::is_advanceable_iterator_v<self_type>) {
  44. self() -= 1;
  45. } else {
  46. self().decrement();
  47. }
  48. return self();
  49. }
  50. self_type operator--(int) {
  51. auto tmp = self();
  52. --*this;
  53. return tmp;
  54. }
  55. template <typename D, typename = difference_type_arg_t<D, self_type>>
  56. friend self_type & operator+=(self_type & self, D off) {
  57. static_assert(detail::is_advanceable_iterator_v<self_type>,
  58. "must be advancable");
  59. self.advance(off);
  60. return self;
  61. }
  62. template <typename D, typename = difference_type_arg_t<D, self_type>>
  63. friend self_type & operator-=(self_type & self, D off) {
  64. static_assert(detail::is_advanceable_iterator_v<self_type>,
  65. "must be advancable");
  66. self.advance(-off);
  67. return self;
  68. }
  69. template <typename D, typename = difference_type_arg_t<D, self_type>>
  70. friend auto operator+(self_type self, D off) {
  71. static_assert(detail::is_advanceable_iterator_v<self_type>,
  72. "must be advancable");
  73. return self += off;
  74. }
  75. template <typename D, typename = difference_type_arg_t<D, self_type>>
  76. friend auto operator+(D off, self_type self) {
  77. static_assert(detail::is_advanceable_iterator_v<self_type>,
  78. "must be advancable");
  79. return self += off;
  80. }
  81. template <typename D, typename = difference_type_arg_t<D, self_type>>
  82. friend auto operator-(self_type self, D off) {
  83. static_assert(detail::is_advanceable_iterator_v<self_type>,
  84. "must be advancable");
  85. return self -= off;
  86. }
  87. friend auto operator-(self_type const & left, self_type const & right) {
  88. return right.distance_to(left);
  89. }
  90. friend bool operator==(self_type const & left, self_type const & right) {
  91. if constexpr (detail::has_distance_to_v<self_type>) {
  92. return (left - right) == 0;
  93. } else {
  94. return left.equal_to(right);
  95. }
  96. }
  97. friend bool operator!=(self_type const & left, self_type const & right) {
  98. return !(left == right);
  99. }
  100. friend bool operator<(self_type const & left, self_type const & right) {
  101. return (left - right) < 0;
  102. }
  103. friend bool operator<=(self_type const & left, self_type const & right) {
  104. return (left - right) <= 0;
  105. }
  106. friend bool operator>(self_type const & left, self_type const & right) {
  107. return (left - right) > 0;
  108. }
  109. friend bool operator>=(self_type const & left, self_type const & right) {
  110. return (left - right) >= 0;
  111. }
  112. private:
  113. self_type & self() { return *static_cast<self_type *>(this); }
  114. self_type const & self() const {
  115. return *static_cast<self_type const *>(this);
  116. }
  117. };
  118. }
  119. #include <iterator/detail/macro.h>
  120. // In C++20, a concept/requires could be used to eschew the need for the below
  121. // macros.
  122. template <typename I> struct std::iterator_traits<::iterator::facade<I>> {
  123. using reference = DEREF_TYPE(I);
  124. using value_type = std::remove_cv_t<std::remove_reference_t<reference>>;
  125. using pointer = TYPE(I, operator->());
  126. using difference_type = ::iterator::detail::distance_to_t<I>;
  127. using iterator_category = ::iterator::detail::facade_category_t<I>;
  128. };
  129. #define MAKE_ITERATOR_FACADE_TYPEDEFS(type) \
  130. template <> \
  131. struct std::iterator_traits<type> \
  132. : std::iterator_traits<::iterator::facade<type>> {}
  133. #define MAKE_ITERATOR_FACADE_TYPEDEFS_T(type) \
  134. template <typename... T> \
  135. struct std::iterator_traits<type<T...>> \
  136. : std::iterator_traits<::iterator::facade<type<T...>>> {}
  137. #include <iterator/detail/undef.h>