Skip to content

Instantly share code, notes, and snippets.

@amedama41
Created January 31, 2014 12:13
Show Gist options
  • Save amedama41/8730956 to your computer and use it in GitHub Desktop.
Save amedama41/8730956 to your computer and use it in GitHub Desktop.
#include <boost/iterator/iterator_adaptor.hpp>
#include <boost/utility.hpp>
namespace canard {
template <class Iterator>
class slide_view_iterator;
namespace detail {
template <class Iterator>
struct slide_iterator_base {
typedef boost::iterator_adaptor<
slide_view_iterator<Iterator>
, Iterator
> type;
};
} // namespace detail
template <class Iterator>
class slide_view_iterator
: public detail::slide_iterator_base<Iterator>::type
{
typedef typename detail::slide_iterator_base<
Iterator
>::type super_t;
friend class boost::iterator_core_access;
public:
typedef typename std::iterator_traits<Iterator>::difference_type difference_type;
typedef Iterator base_iterator;
slide_view_iterator() {}
slide_view_iterator(base_iterator iter, difference_type n = 0)
: super_t(boost::next(iter, n))
, m_iterator(iter)
{
}
base_iterator const& base() const
{
return m_iterator;
}
private:
void advance(difference_type n)
{
std::advance(m_iterator, n);
std::advance(this->base_reference(), n);
}
void increment()
{
++m_iterator;
++(this->base_reference());
}
void decrement()
{
--m_iterator;
--(this->base_reference());
}
base_iterator m_iterator;
};
template <class Iterator, class Difference = typename std::iterator_traits<Iterator>::difference_type>
slide_view_iterator<Iterator>
make_slide_view_iterator(Iterator iter, Difference n = 0)
{
return slide_view_iterator<Iterator>(iter, n);
}
} // namespace canard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment