44#define BEMAN_ANY_VIEW_DETAIL_ITERATOR_ADAPTOR_HPP
55
66#include < beman/any_view/detail/iterator_interface.hpp>
7+ #include < beman/any_view/detail/utility.hpp>
78
89#include < compare>
910#include < iterator>
10- #include < utility>
1111
1212namespace beman ::any_view::detail {
1313
@@ -34,6 +34,10 @@ class iterator_adaptor : public iterator_interface<IterConceptT, ElementT, RefT,
3434 static constexpr bool random_access = std::derived_from<IterConceptT, std::random_access_iterator_tag>;
3535 static constexpr bool contiguous = std::derived_from<IterConceptT, std::contiguous_iterator_tag>;
3636
37+ static constexpr auto down_cast (const iterator_interface& other) {
38+ return dynamic_cast <const iterator_adaptor*>(std::addressof (other));
39+ }
40+
3741 public:
3842 constexpr iterator_adaptor (IteratorT&& iterator, SentinelT&& sentinel) noexcept (get_noexcept())
3943 : iterator(std::move(iterator)), sentinel(std::move(sentinel)) {}
@@ -42,7 +46,7 @@ class iterator_adaptor : public iterator_interface<IterConceptT, ElementT, RefT,
4246 if constexpr (std::copy_constructible<IteratorT> and std::copy_constructible<SentinelT>) {
4347 ::new (destination) iterator_adaptor (*this );
4448 } else {
45- std:: unreachable ();
49+ unreachable ();
4650 }
4751 }
4852
@@ -51,15 +55,15 @@ class iterator_adaptor : public iterator_interface<IterConceptT, ElementT, RefT,
5155 std::is_nothrow_move_constructible_v<SentinelT>) {
5256 ::new (destination) iterator_adaptor (std::move (*this ));
5357 } else {
54- std:: unreachable ();
58+ unreachable ();
5559 }
5660 }
5761
5862 [[nodiscard]] constexpr auto copy () const -> iterator_adaptor* override {
5963 if constexpr (std::copy_constructible<IteratorT> and std::copy_constructible<SentinelT>) {
6064 return new iterator_adaptor (*this );
6165 } else {
62- std:: unreachable ();
66+ unreachable ();
6367 }
6468 }
6569
@@ -71,16 +75,16 @@ class iterator_adaptor : public iterator_interface<IterConceptT, ElementT, RefT,
7175 if constexpr (contiguous) {
7276 return std::to_address (iterator);
7377 } else {
74- std:: unreachable ();
78+ unreachable ();
7579 }
7680 }
7781
7882 constexpr auto operator ++() -> void override { ++iterator; }
7983
8084 [[nodiscard]] constexpr auto operator ==(const iterator_interface& other) const -> bool override {
8185 if constexpr (forward) {
82- if (type () == other. type ( )) {
83- return iterator == static_cast < const iterator_adaptor&>(other). iterator ;
86+ if (const auto adaptor = down_cast (other )) {
87+ return iterator == adaptor-> iterator ;
8488 }
8589 }
8690
@@ -91,14 +95,14 @@ class iterator_adaptor : public iterator_interface<IterConceptT, ElementT, RefT,
9195 if constexpr (bidirectional) {
9296 --iterator;
9397 } else {
94- std:: unreachable ();
98+ unreachable ();
9599 }
96100 }
97101
98102 [[nodiscard]] constexpr auto operator <=>(const iterator_interface& other) const -> std::partial_ordering override {
99103 if constexpr (random_access) {
100- if (type () == other. type ( )) {
101- return iterator <=> static_cast < const iterator_adaptor&>(other). iterator ;
104+ if (const auto adaptor = down_cast (other )) {
105+ return iterator <=> adaptor-> iterator ;
102106 }
103107 }
104108
@@ -107,29 +111,25 @@ class iterator_adaptor : public iterator_interface<IterConceptT, ElementT, RefT,
107111
108112 [[nodiscard]] constexpr auto operator -(const iterator_interface& other) const -> DiffT override {
109113 if constexpr (random_access) {
110- if (type () == other. type ( )) {
111- return iterator - static_cast < const iterator_adaptor&>(other). iterator ;
114+ if (const auto adaptor = down_cast (other )) {
115+ return iterator - adaptor-> iterator ;
112116 }
113117 }
114118
115- std:: unreachable ();
119+ unreachable ();
116120 }
117121
118122 constexpr auto operator +=(DiffT offset) -> void override {
119123 if constexpr (random_access) {
120124 iterator += offset;
121125 } else {
122- std:: unreachable ();
126+ unreachable ();
123127 }
124128 }
125129
126130 [[nodiscard]] constexpr auto operator ==(std::default_sentinel_t ) const -> bool override {
127131 return iterator == sentinel;
128132 }
129-
130- [[nodiscard]] constexpr auto type () const noexcept -> const std::type_info& override {
131- return typeid (iterator_adaptor);
132- }
133133};
134134
135135} // namespace beman::any_view::detail
0 commit comments