Sprout/sprout/range/range_container.hpp

171 lines
5.4 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2015-01-10 10:13:57 +00:00
Copyright (c) 2011-2015 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
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)
=============================================================================*/
2012-04-04 08:48:02 +00:00
#ifndef SPROUT_RANGE_RANGE_CONTAINER_HPP
#define SPROUT_RANGE_RANGE_CONTAINER_HPP
#include <stdexcept>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/container/traits.hpp>
#include <sprout/container/functions.hpp>
2012-12-22 11:10:27 +00:00
#include <sprout/range/type_traits/lvalue_iterator.hpp>
2012-10-05 15:58:56 +00:00
#include <sprout/utility/swap.hpp>
#include <sprout/utility/move.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/type_traits/integral_constant.hpp>
2012-04-04 08:48:02 +00:00
namespace sprout {
namespace range {
//
// range_container
//
template<typename Iterator>
struct range_container
: public sprout::container_traits_facade<Iterator>
2012-04-04 08:48:02 +00:00
{
public:
typedef Iterator iterator;
typedef iterator const_iterator;
typedef typename std::iterator_traits<Iterator>::value_type value_type;
typedef typename std::iterator_traits<Iterator>::reference reference;
2014-07-12 10:04:34 +00:00
typedef typename std::conditional<
std::is_reference<reference>::value,
typename std::remove_reference<reference>::type const&,
reference
>::type const_reference;
2012-04-04 08:48:02 +00:00
typedef typename std::iterator_traits<Iterator>::difference_type difference_type;
typedef typename std::make_unsigned<difference_type>::type size_type;
typedef typename std::iterator_traits<Iterator>::pointer pointer;
2014-07-12 10:04:34 +00:00
typedef typename std::conditional<
std::is_pointer<pointer>::value,
typename std::remove_pointer<pointer>::type const*,
pointer
>::type const_pointer;
2012-04-04 08:48:02 +00:00
private:
iterator first_;
iterator last_;
public:
// construct/copy/destroy:
2012-11-24 13:46:23 +00:00
SPROUT_CONSTEXPR range_container()
: first_(), last_()
{}
SPROUT_CONSTEXPR range_container(iterator const& first, iterator const& last)
: first_(first), last_(last)
2012-04-04 08:48:02 +00:00
{}
template<typename Iterator2>
2013-10-31 03:37:38 +00:00
SPROUT_CXX14_CONSTEXPR void swap(range_container<Iterator2>& other)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF(
2012-10-05 15:58:56 +00:00
SPROUT_NOEXCEPT_EXPR(sprout::swap(other.first_, first_))
&& SPROUT_NOEXCEPT_EXPR(sprout::swap(other.last_, last_))
)
{
sprout::swap(other.first_, first_);
sprout::swap(other.last_, last_);
2012-04-04 08:48:02 +00:00
}
// iterators:
SPROUT_CONSTEXPR iterator begin() const {
return first_;
}
SPROUT_CONSTEXPR iterator end() const {
return last_;
}
// capacity:
SPROUT_CONSTEXPR size_type size() const {
return sprout::distance(first_, last_);
}
SPROUT_CONSTEXPR bool empty() const {
return first_ == last_;
}
// element access:
SPROUT_CONSTEXPR reference operator[](size_type i) const {
return *sprout::next(first_, i);
}
SPROUT_CONSTEXPR reference at(size_type i) const {
return i < size() ? *sprout::next(first_, i)
: (throw std::out_of_range("sprout::range::range_container<>: index out of range"), *sprout::next(first_, i))
;
}
SPROUT_CONSTEXPR reference front() const {
return *first_;
}
SPROUT_CONSTEXPR reference back() const {
return *sprout::next(first_, size() - 1);
}
// others:
template<typename Iterator2>
2013-10-31 03:37:38 +00:00
SPROUT_CXX14_CONSTEXPR range_container<Iterator>& operator=(range_container<Iterator2> const& rhs) {
2012-04-04 08:48:02 +00:00
first_ = rhs.first_;
last_ = rhs.last_;
return *this;
}
template<typename Iterator2>
2013-10-31 03:37:38 +00:00
SPROUT_CXX14_CONSTEXPR range_container<Iterator>& operator=(range_container<Iterator2>&& rhs) {
first_ = sprout::move(rhs.first_);
last_ = sprout::move(rhs.last_);
2012-04-04 08:48:02 +00:00
return *this;
}
};
//
// swap
//
template<typename Iterator>
2013-10-31 03:37:38 +00:00
inline SPROUT_CXX14_CONSTEXPR void
swap(sprout::range::range_container<Iterator>& lhs, sprout::range::range_container<Iterator>& rhs) {
2012-04-04 08:48:02 +00:00
lhs.swap(rhs);
}
//
// is_range_container
//
template<typename T>
struct is_range_container
: public sprout::false_type
2012-04-13 12:23:49 +00:00
{};
template<typename T>
struct is_range_container<T const>
: public sprout::range::is_range_container<T>
{};
template<typename T>
struct is_range_container<T volatile>
: public sprout::range::is_range_container<T>
{};
template<typename T>
struct is_range_container<T const volatile>
: public sprout::range::is_range_container<T>
{};
template<typename Iterator>
struct is_range_container<sprout::range::range_container<Iterator> >
: public sprout::true_type
2012-04-04 08:48:02 +00:00
{};
//
// make_range_container
//
template<typename Range>
inline SPROUT_CONSTEXPR sprout::range::range_container<typename sprout::range::lvalue_iterator<Range>::type>
make_range_container(Range&& range) {
return sprout::range::range_container<typename sprout::range::lvalue_iterator<Range>::type>(
sprout::begin(SPROUT_FORWARD(Range, range)),
sprout::end(SPROUT_FORWARD(Range, range))
);
}
2012-04-04 08:48:02 +00:00
} // namespace range
//
// container_traits
//
template<typename Iterator>
struct container_traits<sprout::range::range_container<Iterator> >
: public sprout::detail::container_traits_default<sprout::range::range_container<Iterator> >
2012-04-04 08:48:02 +00:00
{};
} // namespace sprout
#endif // #ifndef SPROUT_RANGE_RANGE_CONTAINER_HPP