diff --git a/include/boost/iterator/iterator_adaptors.hpp b/include/boost/iterator/iterator_adaptors.hpp index 192b4d4..79d95b1 100644 --- a/include/boost/iterator/iterator_adaptors.hpp +++ b/include/boost/iterator/iterator_adaptors.hpp @@ -6,153 +6,196 @@ namespace boost { -template -struct iterator_comparisons : Base { }; +template +struct repository + : iterator -template -inline bool operator==(const iterator_comparisons& xb, +{ + typedef Final type; + typedef V value_type; + typedef R reference; + typedef C iterator_category; + typedef D difference_type; +}; + +template +struct downcastable : Base +{ + typedef typename Base::type final_t; + public: + final_t& downcast() { return static_cast(*this); } + const final_t& downcast() const { return static_cast(*this); } +}; + +template +struct iterator_comparisons : Base +{ +}; + +template +inline bool operator==(const iterator_comparisons& xb, + const iterator_comparisons& yb) +{ + return xb.downcast().equal(yb.downcast()); +} + +template +inline bool operator!=(const iterator_comparisons& xb, const iterator_comparisons& yb) { - const D1& x = static_cast(xb); - const D2& y = static_cast(yb); - return x.equal(y); + return !xb.downcast().equal(yb.downcast()); } -template -inline bool operator!=(const iterator_comparisons& xb, - const iterator_comparisons& yb) +template +inline bool operator<(const iterator_comparisons& xb, + const iterator_comparisons& yb) { - const D1& x = static_cast(xb); - const D2& y = static_cast(yb); - return !x.equal(y); + return yb.downcast().distance_from(xb.downcast()) < 0; } -template -inline bool operator<(const iterator_comparisons& xb, - const iterator_comparisons& yb) +template +inline bool operator>(const iterator_comparisons& xb, + const iterator_comparisons& yb) { - const D1& x = static_cast(xb); - const D2& y = static_cast(yb); - return y.distance(x) < 0; + return yb.downcast().distance_from(xb.downcast()) > 0; } -template -inline bool operator>(const iterator_comparisons& xb, - const iterator_comparisons& yb) +template +inline bool operator>=(const iterator_comparisons& xb, + const iterator_comparisons& yb) { - const D1& x = static_cast(xb); - const D2& y = static_cast(yb); - return y.distance(x) > 0; + return yb.downcast().distance_from(xb.downcast()) >= 0; } -template -inline bool operator>=(const iterator_comparisons& xb, - const iterator_comparisons& yb) +template +inline bool operator<=(const iterator_comparisons& xb, + const iterator_comparisons& yb) { - const D1& x = static_cast(xb); - const D2& y = static_cast(yb); - return y.distance(x) >= 0; -} - -template -inline bool operator<=(const iterator_comparisons& xb, - const iterator_comparisons& yb) -{ - const D1& x = static_cast(xb); - const D2& y = static_cast(yb); - return y.distance(x) <= 0; + return yb.downcast().distance_from(xb.downcast()) <= 0; } -template -struct iterator_arith : public iterator_comparisons { }; +template +struct iterator_arith : B { }; -template -D operator+(iterator_arith i, D n) { return i += n; } - -template -D operator+(D n, iterator_arith i) { return i += n; } - -template -D operator-(const iterator_arith& i, - const iterator_arith& j) - { return static_cast(j).distance(static_cast(i)); } - - -template -class iterator_adaptor : public iterator_arith > +template +typename Base::final operator+( + iterator_arith i, typename Base::distance n) { -public: - typedef V value_type; - typedef R reference; - typedef P pointer; - typedef C iterator_category; - typedef D difference_type; + return i.downcast() += n; +} - reference operator*() const - { return derived().dereference(); } - //operator->() const { return detail::operator_arrow(*this, iterator_category()); } - reference operator[](difference_type n) const +template +typename Base::final operator+( + typename Base::distance n, iterator_arith i) +{ + return i.downcast() += n; +} + +template +typename Base1::difference_type operator-( + const iterator_arith& i, + const iterator_arith& j) +{ + return j.downcast().distance_from(i.downcast()); +} + + +template > > > + > +struct iterator_adaptor : B +{ + typedef V value_type; + typedef R reference; + typedef P pointer; + typedef C iterator_category; + typedef D difference_type; + + reference operator*() const + { return downcast().dereference(); } + //operator->() const { return detail::operator_arrow(*this, iterator_category()); } + reference operator[](difference_type n) const { return *(*this + n); } - Derived& operator++() - { derived().increment(); return derived(); } - Derived operator++(int) - { Derived tmp(derived()); ++*this; return tmp; } - Derived& operator--() - { derived().decrement(); return derived(); } - Derived operator--(int) - { Derived tmp(derived()); --*this; return tmp; } - Derived& operator+=(difference_type n) - { derived().advance(n); return derived(); } - Derived& operator-=(difference_type n) - { derived().advance(-n); return derived(); } - Derived operator-(difference_type x) const - { Derived result(derived()); return result -= x; } + Final& operator++() + { downcast().increment(); return downcast(); } + Final operator++(int) + { Final tmp(downcast()); ++*this; return tmp; } + Final& operator--() + { downcast().decrement(); return downcast(); } + Final operator--(int) + { Final tmp(downcast()); --*this; return tmp; } + Final& operator+=(difference_type n) + { downcast().advance(n); return downcast(); } + Final& operator-=(difference_type n) + { downcast().advance(-n); return downcast(); } + Final operator-(difference_type x) const + { Final result(downcast()); return result -= x; } - template - bool equal(iterator_adaptor const& x) const - { return this->derived().base() == x.derived().base(); } + template + bool equal(iterator_adaptor const& x) const + { + return this->downcast().base() == x.downcast().base(); + } -private: - Derived& derived() { return static_cast(*this); } - const Derived& derived() const { return static_cast(*this); } + void increment() { ++this->downcast().base(); } + void decrement() { --this->downcast().base(); } + + private: + using B::downcast; }; - template -class reverse_iterator : public iterator_adaptor, V, R, P, C, D> + class V, class R, class P, class C, class D> +struct reverse_iterator + : iterator_adaptor< + reverse_iterator, V, R, P, C, D + > { - typedef iterator_adaptor, V, R, P, C, D> super; + typedef iterator_adaptor, V, R, P, C, D> super; + // friend class super; - // hmm, I don't like the next two lines + // hmm, I don't like the next two lines // template friend struct iterator_comparisons; // template friend struct iterator_arith; -public: - reverse_iterator(const Base& x) : m_base(x) { } - - reverse_iterator() { } - - Base const& base() const { return m_base; } - template - reverse_iterator(const reverse_iterator& y) - : m_base(y.m_base) { } - //private: hmm, the above friend decls aren't doing the job. - typename super::reference dereference() const { return *boost::prior(m_base); } - void increment() { --m_base; } - void decrement() { ++m_base; } - void advance(typename super::difference_type n) - { m_base -= n; } + public: + reverse_iterator(const Base& x) : m_base(x) { } - template - typename super::difference_type - distance(const reverse_iterator& y) const - { return m_base - y.m_base; } + reverse_iterator() { } - Base m_base; + Base const& base() const { return m_base; } + + template + reverse_iterator(const reverse_iterator& y) + : m_base(y.m_base) { } + + + struct state : default_iterator_policies + { + }; + + typename super::reference dereference() const { return *boost::prior(m_base); } + + void increment() { --m_base; } + void decrement() { ++m_base; } + void advance(typename super::difference_type n) + { + m_base -= n; + } + + template + typename super::difference_type + distance_from(const reverse_iterator& y) const + { + return m_base - y.m_base; + } + private: + Base m_base; };