📄 iterator_range.hpp
字号:
typedef size_t size_type;
};
#endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
/// \brief This class adapts an iterator pair into a Range
///
/// \param I The iterator type
/// \param T The iterator range traits, used to deduce the Range's iterator, const_iterator, reference, const_reference and value_type
///
/// It is categoried as an Iterable Range
///
/// It could be used as follows
/// \htmlonly
/// <code>
/// <pre>
/// template<typename I>
/// void dump_elements(I from, I to)
/// {
/// for(iterator_range<I> r(from, to); r; ++r)
/// {
/// std::cout << &r; // Dump the current value to stdout
/// }
/// }
/// </pre>
/// </code>
/// \endhtmlonly
template< ss_typename_param_k I
#ifdef STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT
, ss_typename_param_k T = iterator_range_traits<I, is_const<I>::value> // Determines whether the iterator is const
#else /* ? STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
, ss_typename_param_k T
#endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
>
class iterator_range
: public iterable_range_tag
{
/// \name Member Types
/// @{
public:
/// The iterator type
typedef I iterator_type;
/// The traits type
typedef T traits_type;
/// The range tag type
typedef iterable_range_tag range_tag_type;
/// The current instantiation of this type
typedef iterator_range<I, T> class_type;
/// The value type
typedef ss_typename_type_k traits_type::value_type value_type;
/// The mutating (non-const) iterator type
typedef ss_typename_type_k traits_type::iterator iterator;
/// The non-mutating (const) iterator type
typedef ss_typename_type_k traits_type::const_iterator const_iterator;
/// The mutating (non-const) refernce type
typedef ss_typename_type_k traits_type::reference reference;
/// The non-mutating (const) reference type
typedef ss_typename_type_k traits_type::const_reference const_reference;
/// @}
/// \name Construction
/// @{
public:
#if !defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT) || \
defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_OVERLOAD_DISCRIMINATED) || \
( defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER == 1200)
/// Constructs from an iterator pair
iterator_range(iterator first, iterator last)
: m_position(first)
, m_last(last)
{}
#endif /* !STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT || STLSOFT_CF_MEMBER_TEMPLATE_CTOR_OVERLOAD_DISCRIMINATED */
#if defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT) && \
( !defined(STLSOFT_COMPILER_IS_MSVC) || \
_MSC_VER != 1200)
/// Constructs from an iterator pair
template <ss_typename_param_k I2>
iterator_range(I2 first, I2 last)
: m_position(first)
, m_last(last)
{}
#endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
#ifdef STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT
/// Constructs from an array
template <ss_typename_param_k T2, ss_size_t N>
iterator_range(T2 (&ar)[N])
: m_position(&ar[0])
, m_last(&ar[N])
{}
#endif /* STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT */
/// @}
/// \name Notional Range methods
/// @{
private:
STLSOFT_DEFINE_OPERATOR_BOOL_TYPES_T(class_type, boolean_generator_type, boolean_type);
public:
/// Indicates whether the range is open
ss_bool_t is_open() const
{
return m_position != m_last;
}
/// Returns the current value in the range
reference current()
{
STLSOFT_ASSERT(is_open());
return *m_position;
}
/// Returns the current value in the range
const_reference current() const
{
STLSOFT_ASSERT(is_open());
return *m_position;
}
/// Indicates whether the range is open
operator boolean_type() const
{
return boolean_generator_type::translate(is_open());
}
/// Returns the current value in the range
reference operator *()
{
return current();
}
/// Returns the current value in the range
const_reference operator *() const
{
return current();
}
/// Advances the current position in the range
class_type &advance()
{
STLSOFT_MESSAGE_ASSERT("Attempting to increment the range past its end point", is_open());
++m_position;
return *this;
}
/// Advances the current position in the range
class_type &operator ++()
{
return advance();
}
/// Advances the current position in the range, returning a copy of the
/// range prior to its being advanced
class_type operator ++(int)
{
class_type ret(*this);
operator ++();
return ret;
}
/// @}
/// \name Iterable Range methods
/// @{
public:
/// Returns an iterator to the current position of the range
iterator begin()
{
return m_position;
}
/// Returns an iterator to the end of the range
iterator end()
{
return m_last;
}
/// Returns an iterator to the current position of the range
const_iterator begin() const
{
return m_position;
}
/// Returns an iterator to the end of the range
const_iterator end() const
{
return m_last;
}
/// @}
/// \name Members
/// @{
private:
iterator m_position;
iterator m_last;
/// @}
};
////////////////////////////////////////////////////////////////////////////
// Functions
#ifdef STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT
template<ss_typename_param_k I>
inline iterator_range<I> make_iterator_range(I &first, I &last)
{
return iterator_range<I>(first, last);
}
#else /* ? STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
template<ss_typename_param_k I>
inline iterator_range<I, iterator_range_traits<I> > make_iterator_range(I &first, I &last)
{
return iterator_range<I, iterator_range_traits<I> >(first, last);
}
#if 0
template<ss_typename_param_k T>
inline iterator_range<T*, iterator_range_traits<T> > make_iterator_range(T *first, T *last)
{
return iterator_range<T*, iterator_range_traits<T> >(first, last);
}
#endif /* 0 */
template<ss_typename_param_k T>
inline iterator_range<T const*, const_pointer_iterator_range_traits<const T> > make_iterator_range(T const *first, T const *last)
{
return iterator_range<T const*, const_pointer_iterator_range_traits<const T> >(first, last);
}
#endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
////////////////////////////////////////////////////////////////////////////
// Unit-testing
#ifdef STLSOFT_UNITTEST
# include "./unittest/iterator_range_unittest_.h"
#endif /* STLSOFT_UNITTEST */
/* ////////////////////////////////////////////////////////////////////// */
#ifndef RANGELIB_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace rangelib
# else
} // namespace rangelib_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !RANGELIB_NO_NAMESPACE */
/* ////////////////////////////////////////////////////////////////////// */
#endif /* !RANGELIB_INCL_RANGELIB_HPP_ITERATOR_RANGE */
/* ////////////////////////////////////////////////////////////////////// */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -