📄 member_selector_iterator.hpp
字号:
/* /////////////////////////////////////////////////////////////////////////
* File: stlsoft/iterators/member_selector_iterator.hpp
*
* Purpose: member_selector_iterator class.
*
* Created: 7th April 2005
* Updated: 10th June 2006
*
* Home: http://stlsoft.org/
*
* Copyright (c) 2005-2006, Matthew Wilson and Synesis Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
* any contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* ////////////////////////////////////////////////////////////////////// */
/// \file stlsoft/iterators/member_selector_iterator.hpp
///
/// member_selector_iterator class.
#ifndef STLSOFT_INCL_STLSOFT_ITERATORS_HPP_MEMBER_SELECTOR_ITERATOR
#define STLSOFT_INCL_STLSOFT_ITERATORS_HPP_MEMBER_SELECTOR_ITERATOR
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define STLSOFT_VER_STLSOFT_ITERATORS_HPP_MEMBER_SELECTOR_ITERATOR_MAJOR 2
# define STLSOFT_VER_STLSOFT_ITERATORS_HPP_MEMBER_SELECTOR_ITERATOR_MINOR 3
# define STLSOFT_VER_STLSOFT_ITERATORS_HPP_MEMBER_SELECTOR_ITERATOR_REVISION 5
# define STLSOFT_VER_STLSOFT_ITERATORS_HPP_MEMBER_SELECTOR_ITERATOR_EDIT 38
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Compatibility
*/
/*
[Incompatibilies-start]
STLSOFT_COMPILER_IS_WATCOM:
[Incompatibilies-end]
*/
/* /////////////////////////////////////////////////////////////////////////
* Includes
*/
#ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
# include <stlsoft/stlsoft.h>
#endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
#ifndef STLSOFT_INCL_STLSOFT_HPP_ITERATOR
# include <stlsoft/iterator.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_HPP_ITERATOR */
#ifdef STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT
# ifndef STLSOFT_INCL_STLSOFT_HPP_META
# include <stlsoft/meta.hpp>
# endif /* !STLSOFT_INCL_STLSOFT_HPP_META */
# ifndef STLSOFT_INCL_STLSOFT_META_HPP_BASE_TYPE_TRAITS
# include <stlsoft/meta/base_type_traits.hpp>
# endif /* !STLSOFT_INCL_STLSOFT_META_HPP_BASE_TYPE_TRAITS */
# if defined(STLSOFT_CF_STD_LIBRARY_IS_DINKUMWARE_VC) && \
STLSOFT_CF_STD_LIBRARY_DINKUMWARE_VC_VERSION < STLSOFT_CF_DINKUMWARE_VC_VERSION_7_1
# ifndef STLSOFT_INCL_STLSOFT_UTIL_STD_DINKUMWARE_ITERATOR_TRAITS
# include <stlsoft/util/std/dinkumware_iterator_traits.hpp>
# endif /* STLSOFT_INCL_STLSOFT_UTIL_STD_DINKUMWARE_ITERATOR_TRAITS */
# endif /* old-dinkumware */
#endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
#ifdef STLSOFT_UNITTEST
# include <algorithm>
#endif /* STLSOFT_UNITTEST */
/* /////////////////////////////////////////////////////////////////////////
* Namespace
*/
#ifndef _STLSOFT_NO_NAMESPACE
namespace stlsoft
{
#endif /* _STLSOFT_NO_NAMESPACE */
/* ////////////////////////////////////////////////////////////////////// */
/* ////////////////////////////////////////////////////////////////////// */
/// \weakgroup iterators Iterators
/// \brief STL-compatible iterators
/// \weakgroup adaptors Adaptors
/// \brief Adaptor functions and classes used throughout the STLSoft libraries
/// \weakgroup adaptors_iterator Iterator Adaptors
/// \brief Classes that provide iterator adaptation
/// \ingroup iterators adaptors
/// @{
/* /////////////////////////////////////////////////////////////////////////
* Classes
*/
template< ss_typename_param_k I
, ss_typename_param_k C
, ss_typename_param_k M
>
struct msi_parent_type
// Unfortunately, we can't just go with what iterator form the library supports,
// because we have to deal with:
//
// - VC 7.0, whose Dinkumware library thinks its iterator form is form-1, but
// the compiler doesn't do partial template specialiation
// - Intel 6/7/7.1/8, which may be used with Visual C++ 6 or 7.0 Dinkumware
// libraries
//
#if defined(STLSOFT_ITERATOR_ITERATOR_FORM1_SUPPORT) && \
defined(STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT) && \
( !defined(STLSOFT_CF_STD_LIBRARY_IS_DINKUMWARE_VC) || \
STLSOFT_CF_STD_LIBRARY_DINKUMWARE_VC_VERSION >= STLSOFT_CF_DINKUMWARE_VC_VERSION_7_1) && \
!defined(STLSOFT_COMPILER_IS_BORLAND)
: public iterator_base< ss_typename_type_k stlsoft_ns_qual_std(iterator_traits)<I>::iterator_category
, M
, ss_ptrdiff_t
, M*
, M&
>
#elif defined(STLSOFT_ITERATOR_ITERATOR_FORM2_SUPPORT) || \
( defined(STLSOFT_CF_STD_LIBRARY_IS_DINKUMWARE_VC) && \
STLSOFT_CF_STD_LIBRARY_DINKUMWARE_VC_VERSION == STLSOFT_CF_DINKUMWARE_VC_VERSION_7_0) || \
( defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER == 1300) || \
defined(STLSOFT_COMPILER_IS_BORLAND)
: public iterator_base< stlsoft_ns_qual_std(input_iterator_tag)
, M
, ss_ptrdiff_t
, M*
, M&
>
#elif defined(STLSOFT_ITERATOR_ITERATOR_FORM3_SUPPORT)
// do not derive
#else /* ? STLSOFT_ITERATOR_ITERATOR_FORM??????_SUPPORT */
# error iterator support type not discriminated
#endif /* !STLSOFT_ITERATOR_ITERATOR_FORM1_SUPPORT */
{
/// \name Member Types
/// @{
public:
#if defined(STLSOFT_ITERATOR_ITERATOR_FORM3_SUPPORT)
typedef stlsoft_ns_qual_std(input_iterator_tag) iterator_category;
#endif /* form ? */
typedef I base_iterator_type;
typedef C selected_class_type;
typedef M value_type;
typedef value_type *pointer;
typedef value_type const *const_pointer;
typedef value_type &reference;
typedef value_type const &const_reference;
typedef ss_size_t size_type;
typedef ss_ptrdiff_t difference_type;
/// @}
};
// class member_selector_iterator
/// An iterator adaptor class template that presents a member of the underlying
/// iterator's value type as the apparent value type.
///
/// \param I The type of the base iterator, whose value type should be C, or convertible to C
/// \param C The class of the member pointer
/// \param M The type of the member pointer
template< ss_typename_param_k I
, ss_typename_param_k C
, ss_typename_param_k M
>
// [[synesis:class:iterator: member_selector_iterator<T<I>, T<C>, T<M>>]]
class member_selector_iterator
: public msi_parent_type<I, C, M>
{
/// \name Member Types
/// @{
private:
typedef msi_parent_type<I, C, M> parent_class_type;
public:
typedef ss_typename_type_k parent_class_type::base_iterator_type base_iterator_type;
typedef ss_typename_type_k parent_class_type::selected_class_type selected_class_type;
typedef ss_typename_type_k parent_class_type::value_type value_type;
typedef ss_typename_type_k parent_class_type::pointer pointer;
typedef ss_typename_type_k parent_class_type::const_pointer const_pointer;
typedef ss_typename_type_k parent_class_type::reference reference;
typedef ss_typename_type_k parent_class_type::const_reference const_reference;
typedef ss_typename_type_k parent_class_type::size_type size_type;
typedef ss_typename_type_k parent_class_type::difference_type difference_type;
typedef member_selector_iterator<I, C, M> class_type;
/// @}
/// \name Construction
/// @{
public:
/// \brief Constructs an instance from an iterator and a member pointer
member_selector_iterator(base_iterator_type it, M C::*member)
: m_it(it)
, m_member(member)
{}
/// Copy constructor
member_selector_iterator(class_type const &rhs)
: m_it(rhs.m_it)
, m_member(rhs.m_member)
{}
/// Destructor
~member_selector_iterator() stlsoft_throw_0()
{
void (*p)() = constraints;
STLSOFT_SUPPRESS_UNUSED(p);
}
private:
static void constraints()
{
C *p = 0; // C* is a proper pointer
M C::*pm = 0; // M C:: are meaningful class/member types
M const &m = p->*pm; // Pointer to member can be dereferenced
STLSOFT_SUPPRESS_UNUSED(m);
}
public:
/// \brief A copy of the base iterator
base_iterator_type base() const
{
return m_it;
}
/// \brief A copy of the base iterator
///
/// \deprecated This is replaced by base()
base_iterator_type current() const
{
return m_it;
}
/// @}
/// \name Accessors
/// @{
public:
/// \brief Constructs an instance from an iterator and a member pointer
value_type C::* member() const // NOTE: Has to be C, not selected_class_type, or VC 7.0+ spits
{
return m_member;
}
/// @}
/// \name Input Iterator methods
/// @{
public:
/// Pre-increment iterator
///
/// \note Increments the base iterator
class_type &operator ++()
{
++m_it;
return *this;
}
/// Post-increment iterator
///
/// \note Increments the base iterator
class_type operator ++(int)
{
class_type r(*this);
operator ++();
return r;
}
# if defined(STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT) && \
!defined(STLSOFT_COMPILER_IS_BORLAND)
/// Returns a mutating (non-const) reference to the selected member of the base iterators current value
reference operator *()
{
return (*m_it).*m_member;
}
# endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT && !STLSOFT_COMPILER_IS_BORLAND */
/// Returns a non-mutating (const) reference to the selected member of the base iterators current value
const_reference operator *() const
{
return (*m_it).*m_member;
}
/// Evaluates two instances for equality
bool equal(class_type const &rhs) const
{
STLSOFT_ASSERT(m_member == rhs.m_member);
return m_it == rhs.m_it;
}
/// @}
/// \name Bidirectional Iterator methods
/// @{
public:
/// Pre-decrement iterator
///
/// \note Decrements the base iterator
class_type &operator --()
{
--m_it;
return *this;
}
/// Post-decrement iterator
///
/// \note Decrements the base iterator
class_type operator --(int)
{
class_type r(*this);
operator --();
return r;
}
/// @}
/// \name Random-Access Iterator methods
/// @{
public:
/// Moves the iterator forward
///
/// \param n The amount by which to increment the iterator's current position
class_type &operator +=(difference_type n)
{
m_it += n;
return *this;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -