Sprout/sprout/iterator/value_iterator.hpp

213 lines
6.4 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +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)
=============================================================================*/
2011-10-06 12:45:01 +00:00
#ifndef SPROUT_ITERATOR_VALUE_ITERATOR_HPP
#define SPROUT_ITERATOR_VALUE_ITERATOR_HPP
#include <cstddef>
#include <iterator>
#include <utility>
2011-10-08 02:30:39 +00:00
#include <type_traits>
2011-10-06 12:45:01 +00:00
#include <sprout/config.hpp>
2013-08-06 15:15:09 +00:00
#include <sprout/limits.hpp>
2011-10-06 12:45:01 +00:00
#include <sprout/iterator/next.hpp>
#include <sprout/iterator/prev.hpp>
2012-04-01 13:15:09 +00:00
#include <sprout/iterator/distance.hpp>
2013-02-07 14:12:57 +00:00
#include <sprout/utility/value_holder/value_holder.hpp>
2012-10-05 15:58:56 +00:00
#include <sprout/utility/swap.hpp>
2011-10-06 12:45:01 +00:00
namespace sprout {
//
// value_iterator
//
template<typename T>
class value_iterator
: public std::iterator<
std::random_access_iterator_tag,
typename sprout::value_holder<T>::value_type,
std::ptrdiff_t,
typename sprout::value_holder<T>::mutable_or_const_pointer,
typename sprout::value_holder<T>::mutable_or_const_reference
>
{
public:
typedef T type;
2011-12-22 14:28:14 +00:00
typedef typename std::conditional<
2011-10-08 02:30:39 +00:00
std::is_reference<type>::value,
typename std::decay<type>::type const&,
typename std::decay<type>::type const
>::type const_type;
2011-10-06 12:45:01 +00:00
private:
typedef std::iterator<
std::random_access_iterator_tag,
typename sprout::value_holder<T>::value_type,
std::ptrdiff_t,
typename sprout::value_holder<T>::mutable_or_const_pointer,
typename sprout::value_holder<T>::mutable_or_const_reference
> base_type;
public:
typedef typename base_type::iterator_category iterator_category;
typedef typename base_type::value_type value_type;
typedef typename base_type::difference_type difference_type;
typedef typename base_type::pointer pointer;
typedef typename base_type::reference reference;
private:
sprout::value_holder<T> holder_;
2013-02-23 06:21:27 +00:00
difference_type index_;
2011-10-06 12:45:01 +00:00
private:
2013-02-23 06:21:27 +00:00
SPROUT_CONSTEXPR value_iterator(sprout::value_holder<T> const& r, difference_type index)
: holder_(r), index_(index)
2011-10-06 12:45:01 +00:00
{}
public:
SPROUT_CONSTEXPR value_iterator()
2013-02-23 06:21:27 +00:00
: holder_(), index_()
2011-10-06 12:45:01 +00:00
{}
value_iterator(value_iterator const&) = default;
explicit SPROUT_CONSTEXPR value_iterator(
typename sprout::value_holder<T>::param_type p,
2013-08-06 15:15:09 +00:00
difference_type index = sprout::numeric_limits<difference_type>::max()
)
2013-02-23 06:21:27 +00:00
: holder_(p), index_(index)
2011-10-06 12:45:01 +00:00
{}
2011-10-08 02:30:39 +00:00
operator value_iterator<const_type>() const {
2013-02-23 06:21:27 +00:00
return value_iterator<const_type>(holder_.get(), index_);
}
SPROUT_CONSTEXPR difference_type index() const {
return index_;
2011-10-08 02:30:39 +00:00
}
2011-10-06 12:45:01 +00:00
SPROUT_CONSTEXPR value_iterator next() const {
2013-02-23 06:21:27 +00:00
return value_iterator(holder_, index_ - 1);
2011-10-06 12:45:01 +00:00
}
SPROUT_CONSTEXPR value_iterator prev() const {
2013-02-23 06:21:27 +00:00
return value_iterator(holder_, index_ + 1);
2011-10-06 12:45:01 +00:00
}
2012-10-05 15:58:56 +00:00
void swap(value_iterator& other)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout::swap(holder_, other.holder_)))
{
sprout::swap(holder_, other.holder_);
2013-02-23 06:21:27 +00:00
sprout::swap(index_, other.index_);
2011-10-06 12:45:01 +00:00
}
SPROUT_CONSTEXPR reference operator*() const {
2013-02-23 06:21:27 +00:00
return holder_.get();
2011-10-06 12:45:01 +00:00
}
SPROUT_CONSTEXPR pointer operator->() const {
2013-02-23 06:21:27 +00:00
return holder_.get_pointer();
2011-10-06 12:45:01 +00:00
}
value_iterator& operator++() {
value_iterator temp(next());
temp.swap(*this);
return *this;
}
value_iterator operator++(int) {
value_iterator result(*this);
++*this;
return result;
}
value_iterator& operator--() {
value_iterator temp(prev());
temp.swap(*this);
return *this;
}
value_iterator operator--(int) {
value_iterator result(*this);
--*this;
return result;
}
SPROUT_CONSTEXPR value_iterator operator+(difference_type n) const {
2013-02-23 06:21:27 +00:00
return value_iterator(holder_, index_ - n);
2011-10-06 12:45:01 +00:00
}
SPROUT_CONSTEXPR value_iterator operator-(difference_type n) const {
2013-02-23 06:21:27 +00:00
return value_iterator(holder_, index_ + n);
2011-10-06 12:45:01 +00:00
}
value_iterator& operator+=(difference_type n) {
2013-02-23 06:21:27 +00:00
value_iterator temp(holder_, index_ - n);
temp.swap(*this);
return *this;
2011-10-06 12:45:01 +00:00
}
value_iterator& operator-=(difference_type n) {
2013-02-23 06:21:27 +00:00
value_iterator temp(holder_, index_ + n);
temp.swap(*this);
return *this;
2011-10-06 12:45:01 +00:00
}
2013-07-22 13:00:09 +00:00
SPROUT_CONSTEXPR reference operator[](difference_type) const {
2011-10-06 12:45:01 +00:00
return holder_.get();
}
};
2013-02-23 06:21:27 +00:00
template<typename T>
inline SPROUT_CONSTEXPR bool
operator==(sprout::value_iterator<T> const& lhs, sprout::value_iterator<T> const& rhs) {
return lhs.index() == rhs.index();
}
template<typename T>
inline SPROUT_CONSTEXPR bool
operator!=(sprout::value_iterator<T> const& lhs, sprout::value_iterator<T> const& rhs) {
return !(lhs == rhs);
}
template<typename T>
inline SPROUT_CONSTEXPR bool
operator<(sprout::value_iterator<T> const& lhs, sprout::value_iterator<T> const& rhs) {
return rhs.index() < lhs.index();
}
template<typename T>
inline SPROUT_CONSTEXPR bool
operator>(sprout::value_iterator<T> const& lhs, sprout::value_iterator<T> const& rhs) {
return rhs < lhs;
}
template<typename T>
inline SPROUT_CONSTEXPR bool
operator<=(sprout::value_iterator<T> const& lhs, sprout::value_iterator<T> const& rhs) {
return !(rhs < lhs);
}
template<typename T>
inline SPROUT_CONSTEXPR bool
operator>=(sprout::value_iterator<T> const& lhs, sprout::value_iterator<T> const& rhs) {
return !(lhs < rhs);
}
template<typename T>
inline SPROUT_CONSTEXPR typename sprout::value_iterator<T>::difference_type
operator-(sprout::value_iterator<T> const& lhs, sprout::value_iterator<T> const& rhs) {
return rhs.index() - lhs.index();
}
template<typename T>
inline SPROUT_CONSTEXPR sprout::value_iterator<T>
operator+(typename sprout::value_iterator<T>::difference_type n, sprout::value_iterator<T> const& it) {
return it + n;
}
2011-10-06 12:45:01 +00:00
//
// swap
//
template<typename T>
2012-10-05 15:58:56 +00:00
inline void
swap(sprout::value_iterator<T>& lhs, sprout::value_iterator<T>& rhs)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs)))
{
2011-10-06 12:45:01 +00:00
lhs.swap(rhs);
}
//
// iterator_next
2011-10-06 12:45:01 +00:00
//
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::value_iterator<T>
iterator_next(sprout::value_iterator<T> const& it) {
2011-10-06 12:45:01 +00:00
return it.next();
}
//
// iterator_prev
2011-10-06 12:45:01 +00:00
//
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::value_iterator<T>
iterator_prev(sprout::value_iterator<T> const& it) {
2011-10-06 12:45:01 +00:00
return it.prev();
}
} // namespace sprout
2011-10-06 12:45:01 +00:00
#endif // #ifndef SPROUT_ITERATOR_VALUE_ITERATOR_HPP