1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/iterator/next_iterator.hpp

140 lines
4.3 KiB
C++
Raw Normal View History

2013-08-12 16:55:26 +00:00
/*=============================================================================
Copyright (c) 2011-2013 Bolero MURAKAMI
https://github.com/bolero-MURAKAMI/Sprout
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#ifndef SPROUT_ITERATOR_NEXT_ITERATOR_HPP
#define SPROUT_ITERATOR_NEXT_ITERATOR_HPP
#include <iterator>
#include <utility>
#include <sprout/config.hpp>
#include <sprout/iterator/next.hpp>
#include <sprout/utility/value_holder/value_holder.hpp>
#include <sprout/utility/swap.hpp>
namespace sprout {
//
// next_iterator
//
template<typename Iterator>
struct next_iterator
: public std::iterator<
std::forward_iterator_tag,
typename std::iterator_traits<Iterator>::value_type,
typename std::iterator_traits<Iterator>::difference_type,
typename std::iterator_traits<Iterator>::pointer,
typename std::iterator_traits<Iterator>::reference
>
{
public:
typedef Iterator iterator_type;
typedef std::forward_iterator_tag iterator_category;
typedef typename std::iterator_traits<iterator_type>::value_type value_type;
typedef typename std::iterator_traits<iterator_type>::difference_type difference_type;
typedef typename std::iterator_traits<iterator_type>::pointer pointer;
typedef typename std::iterator_traits<iterator_type>::reference reference;
private:
typedef sprout::value_holder<reference> holder_type;
private:
holder_type holder_;
protected:
iterator_type current;
public:
SPROUT_CONSTEXPR next_iterator()
: holder_(), current()
{}
2013-08-12 16:55:26 +00:00
SPROUT_CONSTEXPR next_iterator(next_iterator const& other)
: holder_(other.holder_), current(other.current)
{}
SPROUT_CONSTEXPR next_iterator(iterator_type it)
: holder_(*it), current(sprout::next(it))
{}
template<typename U>
SPROUT_CONSTEXPR next_iterator(next_iterator<U> const& it)
: holder_(*it), current(it.base())
{}
template<typename U>
SPROUT_CXX14_CONSTEXPR next_iterator& operator=(next_iterator<U> const& it) {
2013-08-12 16:55:26 +00:00
next_iterator temp(it);
temp.swap(*this);
return *this;
}
SPROUT_CONSTEXPR iterator_type base() const {
return current;
}
SPROUT_CONSTEXPR reference operator*() const {
return *holder_;
}
SPROUT_CONSTEXPR pointer operator->() const {
return &*(*this);
}
SPROUT_CXX14_CONSTEXPR next_iterator& operator++() {
2013-08-12 16:55:26 +00:00
holder_ = holder_type(*current);
++current;
return *this;
}
SPROUT_CXX14_CONSTEXPR next_iterator operator++(int) {
2013-08-12 16:55:26 +00:00
next_iterator result(*this);
holder_ = holder_type(*current);
++current;
return result;
}
SPROUT_CONSTEXPR next_iterator next() const {
return next_iterator(current);
}
SPROUT_CXX14_CONSTEXPR void swap(next_iterator& other)
2013-08-12 16:55:26 +00:00
SPROUT_NOEXCEPT_EXPR(
SPROUT_NOEXCEPT_EXPR(swap(holder_, other.holder_)) && SPROUT_NOEXCEPT_EXPR(swap(current, other.current))
)
{
swap(holder_, other.holder_);
swap(current, other.current);
}
};
template<typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR bool
operator==(sprout::next_iterator<Iterator1> const& lhs, sprout::next_iterator<Iterator2> const& rhs) {
return lhs.base() == rhs.base();
}
template<typename Iterator1, typename Iterator2>
inline SPROUT_CONSTEXPR bool
operator!=(sprout::next_iterator<Iterator1> const& lhs, sprout::next_iterator<Iterator2> const& rhs) {
return !(lhs == rhs);
}
//
// make_next_iterator
//
template<typename Iterator>
inline SPROUT_CONSTEXPR sprout::next_iterator<Iterator>
make_next_iterator(Iterator it) {
return sprout::next_iterator<Iterator>(it);
}
//
// swap
//
template<typename Iterator>
inline SPROUT_CXX14_CONSTEXPR void
2013-08-12 16:55:26 +00:00
swap(sprout::next_iterator<Iterator>& lhs, sprout::next_iterator<Iterator>& rhs)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs)))
{
lhs.swap(rhs);
}
//
// iterator_next
//
template<typename Iterator>
inline SPROUT_CONSTEXPR sprout::next_iterator<Iterator>
iterator_next(sprout::next_iterator<Iterator> const& it) {
return it.next();
}
} // namespace sprout
#endif // #ifndef SPROUT_ITERATOR_NEXT_ITERATOR_HPP