From a17934adb53fb525dff4178917c4e73bd74ef9ff Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Tue, 22 Oct 2002 22:10:49 +0000 Subject: [PATCH] iterator_adaptor portability and fleshing-out [SVN r600] --- example/Jamfile | 1 + example/reverse_iterator.cpp | 9 ++- include/boost/iterator/iterator_adaptors.hpp | 78 +++++++++++--------- 3 files changed, 49 insertions(+), 39 deletions(-) create mode 100644 example/Jamfile diff --git a/example/Jamfile b/example/Jamfile new file mode 100644 index 0000000..921ff30 --- /dev/null +++ b/example/Jamfile @@ -0,0 +1 @@ +unit-test ia1 : reverse_iterator.cpp : ../../.. $(BOOST) ; \ No newline at end of file diff --git a/example/reverse_iterator.cpp b/example/reverse_iterator.cpp index 73c7c64..1aec7e3 100644 --- a/example/reverse_iterator.cpp +++ b/example/reverse_iterator.cpp @@ -1,8 +1,8 @@ +#include +#include #include #include -#include "boost/iterator/iterator_adaptors.hpp" -#include "boost/cstdlib.hpp" - +#include int main() { @@ -10,5 +10,6 @@ int main() boost::reverse_iterator first(x + 4), last(x); std::copy(first, last, std::ostream_iterator(std::cout, " ")); - std::cout << std::endl; + std::cout << std::endl; + return 0; } diff --git a/include/boost/iterator/iterator_adaptors.hpp b/include/boost/iterator/iterator_adaptors.hpp index 609e985..192b4d4 100644 --- a/include/boost/iterator/iterator_adaptors.hpp +++ b/include/boost/iterator/iterator_adaptors.hpp @@ -1,61 +1,62 @@ #ifndef BOOST_ITERATOR_ADAPTORS_HPP #define BOOST_ITERATOR_ADAPTORS_HPP -#include "boost/utility.hpp" // for prior +#include // for prior +#include namespace boost { -template -struct iterator_comparisons { }; +template +struct iterator_comparisons : Base { }; -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); } -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); } -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) +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) +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) +template +inline bool operator<=(const iterator_comparisons& xb, + const iterator_comparisons& yb) { const D1& x = static_cast(xb); const D2& y = static_cast(yb); @@ -63,24 +64,24 @@ inline bool operator<=(const iterator_comparisons& xb, } -template -struct iterator_arith : public iterator_comparisons { }; +template +struct iterator_arith : public iterator_comparisons { }; -template -D operator+(iterator_arith i, D n) { return i += n; } +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+(D n, iterator_arith i) { return i += n; } -template -D operator-(const iterator_arith& i, - const iterator_arith& j) +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 +class iterator_adaptor : public iterator_arith > { public: typedef V value_type; @@ -108,6 +109,11 @@ public: { derived().advance(-n); return derived(); } Derived operator-(difference_type x) const { Derived result(derived()); return result -= x; } + + template + bool equal(iterator_adaptor const& x) const + { return this->derived().base() == x.derived().base(); } + private: Derived& derived() { return static_cast(*this); } const Derived& derived() const { return static_cast(*this); } @@ -120,20 +126,22 @@ template , V, R, P, C, D> { typedef iterator_adaptor, V, R, P, C, D> super; - friend class super; +// friend class super; // hmm, I don't like the next two lines - template friend struct iterator_comparisons; - template friend struct iterator_arith; +// 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 { *boost::prior(m_base); } + 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)