From b8b3bafc7c90c5ec6142f281bacfc58eaa9442ad Mon Sep 17 00:00:00 2001 From: Jeremy Siek Date: Thu, 27 Jul 2000 14:39:05 +0000 Subject: [PATCH] initial checkin of iterator_adaptors, same as original wrapped_iterator except for changing the name to iterator_adaptor [SVN r7644] --- include/boost/iterator_adaptors.hpp | 211 ++++++++++++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 include/boost/iterator_adaptors.hpp diff --git a/include/boost/iterator_adaptors.hpp b/include/boost/iterator_adaptors.hpp new file mode 100644 index 0000000..5a76439 --- /dev/null +++ b/include/boost/iterator_adaptors.hpp @@ -0,0 +1,211 @@ +// (C) Copyright David Abrahams 2000. Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +#ifndef BOOST_ITERATOR_ADAPTOR_DWA053000_HPP_ +# define BOOST_ITERATOR_ADAPTOR_DWA053000_HPP_ + +// MSVC complains about the wrong stuff unless you disable this. We should add +// this to config.hpp + +#include +#include + +namespace boost { + +// Just a "type envelope"; works around some MSVC deficiencies. +template +struct type {}; + +// Default policies for wrapped iterators. You can use this as a base +// class if you want to customize particular policies. +struct default_iterator_policies +{ + template + static Reference dereference(type, const Iterator& x) + { return *x; } + + template + static void increment(Iterator& x) + { ++x; } + + template + static void decrement(Iterator& x) + { --x; } + + template + static void advance(Iterator& x, DifferenceType n) + { x += n; } + + template + static Difference distance(type, Iterator1& x, Iterator2& y) + { return y - x; } + + template + static bool equal(Iterator1& x, Iterator2& y) + { return x == y; } + + template + static bool less(Iterator1& x, Iterator2& y) + { return x < y; } +}; + +// iterator_adaptor - A generalized adaptor around an existing iterator, which is itself an iterator +// +// Iterator - the iterator type being wrapped. +// +// Policies - a set of policies determining how the resulting iterator +// works. +// +// NonconstIterator - the corresponding non-const iterator type for +// Iterator, if any. You don't need to supply this if you are not make a +// const/non-const iterator pair. +// +// Traits - a class satisfying the same requirements as a specialization of +// std::iterator_traits for the resulting iterator. +// +template > +struct iterator_adaptor + : std::iterator +{ + typedef iterator_adaptor Self; +public: + typedef typename Traits::difference_type difference_type; + typedef typename Traits::value_type value_type; + typedef typename Traits::pointer pointer; + typedef typename Traits::reference reference; + typedef typename Traits::iterator_category iterator_category; + + iterator_adaptor(const Iterator& impl) + : m_impl(impl) {} + + template + iterator_adaptor(const iterator_adaptor& rhs) + : m_impl(rhs.m_impl) {} + + template + Self& operator=(const iterator_adaptor& rhs) + { m_impl = rhs.m_impl; return *this; } + + reference operator*() const { + return Policies::dereference(type(), m_impl); + } + +#ifdef _MSC_VER +# pragma warning(push) +# pragma warning( disable : 4284 ) +#endif + + pointer operator->() const + { return &*this; } + +#ifdef _MSC_VER +# pragma warning(pop) +#endif + + reference operator[](difference_type n) + { return *(*this + n); } + + Self& operator++() { + Policies::increment(m_impl); + return *this; + } + + Self& operator++(int) { Self tmp(*this); ++*this; return tmp; } + + Self& operator--() { + Policies::decrement(m_impl); + return *this; + } + + Self& operator--(int) { Self tmp(*this); --*this; return tmp; } + + Self& operator+=(difference_type n) { + Policies::advance(m_impl, n); + return *this; + } + + Self& operator-=(difference_type n) { + Policies::advance(m_impl, -n); + return *this; + } + +private: + typedef Policies policies_type; +public: // too many compilers have trouble when this is private. + Iterator m_impl; +}; + +template +iterator_adaptor +operator-(iterator_adaptor p, const typename Traits::difference_type x) +{ + return p -= x; +} + +template +iterator_adaptor +operator+(iterator_adaptor p, const typename Traits::difference_type x) +{ + return p += x; +} + +template +iterator_adaptor +operator+(const typename Traits::difference_type x, iterator_adaptor p) +{ + return p += x; +} + +template +typename Traits1::difference_type operator-( + const iterator_adaptor& x, + const iterator_adaptor& y ) +{ + typedef typename Traits1::difference_type difference_type; + return Policies::distance(type(), y.m_impl, x.m_impl); +} + +template +inline bool +operator==(const iterator_adaptor& x, const iterator_adaptor& y) { + return Policies::equal(x.m_impl, y.m_impl); +} + +template +inline bool +operator<(const iterator_adaptor& x, const iterator_adaptor& y) { + return Policies::less(x.m_impl, y.m_impl); +} + +template +inline bool +operator>(const iterator_adaptor& x, + const iterator_adaptor& y) { + return Policies::less(y.m_impl, x.m_impl); +} + +template +inline bool +operator>=(const iterator_adaptor& x, const iterator_adaptor& y) { + return !Policies::less(x.m_impl, y.m_impl); +} + +template +inline bool +operator<=(const iterator_adaptor& x, + const iterator_adaptor& y) { + return !Policies::less(y.m_impl, x.m_impl); +} + +template +inline bool +operator!=(const iterator_adaptor& x, + const iterator_adaptor& y) { + return !Policies::equal(x.m_impl, y.m_impl); +} + +} + +#endif