Chris@16: // (C) Copyright David Abrahams 2002. Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // Boost versions of Chris@16: // Chris@16: // std::iterator_traits<>::iterator_category Chris@16: // std::iterator_traits<>::difference_type Chris@16: // std::distance() Chris@16: // Chris@16: // ...for all compilers and iterators Chris@16: // Chris@16: // Additionally, if X is a pointer Chris@16: // std::iterator_traits::pointer Chris@16: Chris@16: // Otherwise, if partial specialization is supported or X is not a pointer Chris@16: // std::iterator_traits::value_type Chris@16: // std::iterator_traits::pointer Chris@16: // std::iterator_traits::reference Chris@16: // Chris@16: // See http://www.boost.org for most recent version including documentation. Chris@16: Chris@16: // Revision History Chris@16: // 04 Mar 2001 - More attempted fixes for Intel C++ (David Abrahams) Chris@16: // 03 Mar 2001 - Put all implementation into namespace Chris@16: // boost::detail::iterator_traits_. Some progress made on fixes Chris@16: // for Intel compiler. (David Abrahams) Chris@16: // 02 Mar 2001 - Changed BOOST_MSVC to BOOST_MSVC_STD_ITERATOR in a few Chris@16: // places. (Jeremy Siek) Chris@16: // 19 Feb 2001 - Improved workarounds for stock MSVC6; use yes_type and Chris@16: // no_type from type_traits.hpp; stopped trying to remove_cv Chris@16: // before detecting is_pointer, in honor of the new type_traits Chris@16: // semantics. (David Abrahams) Chris@16: // 13 Feb 2001 - Make it work with nearly all standard-conforming iterators Chris@16: // under raw VC6. The one category remaining which will fail is Chris@16: // that of iterators derived from std::iterator but not Chris@16: // boost::iterator and which redefine difference_type. Chris@16: // 11 Feb 2001 - Clean away code which can never be used (David Abrahams) Chris@16: // 09 Feb 2001 - Always have a definition for each traits member, even if it Chris@16: // can't be properly deduced. These will be incomplete types in Chris@16: // some cases (undefined), but it helps suppress MSVC errors Chris@16: // elsewhere (David Abrahams) Chris@16: // 07 Feb 2001 - Support for more of the traits members where possible, making Chris@16: // this useful as a replacement for std::iterator_traits when Chris@16: // used as a default template parameter. Chris@16: // 06 Feb 2001 - Removed useless #includes of standard library headers Chris@16: // (David Abrahams) Chris@16: Chris@16: #ifndef ITERATOR_DWA122600_HPP_ Chris@16: # define ITERATOR_DWA122600_HPP_ Chris@16: Chris@16: # include Chris@16: # include Chris@16: Chris@16: // STLPort 4.0 and betas have a bug when debugging is enabled and there is no Chris@16: // partial specialization: instead of an iterator_category typedef, the standard Chris@16: // container iterators have _Iterator_category. Chris@16: // Chris@16: // Also, whether debugging is enabled or not, there is a broken specialization Chris@16: // of std::iterator which has no Chris@16: // typedefs but iterator_category. Chris@16: # if defined(__SGI_STL_PORT) Chris@16: Chris@16: # if (__SGI_STL_PORT <= 0x410) && !defined(__STL_CLASS_PARTIAL_SPECIALIZATION) && defined(__STL_DEBUG) Chris@16: # define BOOST_BAD_CONTAINER_ITERATOR_CATEGORY_TYPEDEF Chris@16: # endif Chris@16: Chris@16: # define BOOST_BAD_OUTPUT_ITERATOR_SPECIALIZATION Chris@16: Chris@16: # endif // STLPort <= 4.1b4 && no partial specialization Chris@16: Chris@16: # if !defined(BOOST_NO_STD_ITERATOR_TRAITS) \ Chris@16: && !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) \ Chris@16: && !defined(BOOST_MSVC_STD_ITERATOR) Chris@16: Chris@16: namespace boost { namespace detail { Chris@16: Chris@16: // Define a new template so it can be specialized Chris@16: template Chris@16: struct iterator_traits Chris@16: : std::iterator_traits Chris@16: {}; Chris@16: using std::distance; Chris@16: Chris@16: }} // namespace boost::detail Chris@16: Chris@16: # else Chris@16: Chris@16: # if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) \ Chris@16: && !defined(BOOST_MSVC_STD_ITERATOR) Chris@16: Chris@16: // This is the case where everything conforms except BOOST_NO_STD_ITERATOR_TRAITS Chris@16: Chris@16: namespace boost { namespace detail { Chris@16: Chris@16: // Rogue Wave Standard Library fools itself into thinking partial Chris@16: // specialization is missing on some platforms (e.g. Sun), so fails to Chris@16: // supply iterator_traits! Chris@16: template Chris@16: struct iterator_traits Chris@16: { Chris@16: typedef typename Iterator::value_type value_type; Chris@16: typedef typename Iterator::reference reference; Chris@16: typedef typename Iterator::pointer pointer; Chris@16: typedef typename Iterator::difference_type difference_type; Chris@16: typedef typename Iterator::iterator_category iterator_category; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct iterator_traits Chris@16: { Chris@16: typedef T value_type; Chris@16: typedef T& reference; Chris@16: typedef T* pointer; Chris@16: typedef std::ptrdiff_t difference_type; Chris@16: typedef std::random_access_iterator_tag iterator_category; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct iterator_traits Chris@16: { Chris@16: typedef T value_type; Chris@16: typedef T const& reference; Chris@16: typedef T const* pointer; Chris@16: typedef std::ptrdiff_t difference_type; Chris@16: typedef std::random_access_iterator_tag iterator_category; Chris@16: }; Chris@16: Chris@16: }} // namespace boost::detail Chris@16: Chris@16: # else Chris@16: Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: Chris@16: # ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION Chris@16: # include Chris@16: # include Chris@16: # endif Chris@16: # ifdef BOOST_BAD_OUTPUT_ITERATOR_SPECIALIZATION Chris@16: # include Chris@16: # endif Chris@16: Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: Chris@16: // should be the last #include Chris@16: # include "boost/type_traits/detail/bool_trait_def.hpp" Chris@16: Chris@16: namespace boost { namespace detail { Chris@16: Chris@16: BOOST_MPL_HAS_XXX_TRAIT_DEF(value_type) Chris@16: BOOST_MPL_HAS_XXX_TRAIT_DEF(reference) Chris@16: BOOST_MPL_HAS_XXX_TRAIT_DEF(pointer) Chris@16: BOOST_MPL_HAS_XXX_TRAIT_DEF(difference_type) Chris@16: BOOST_MPL_HAS_XXX_TRAIT_DEF(iterator_category) Chris@16: Chris@16: // is_mutable_iterator -- Chris@16: // Chris@16: // A metafunction returning true iff T is a mutable iterator type Chris@16: // with a nested value_type. Will only work portably with iterators Chris@16: // whose operator* returns a reference, but that seems to be OK for Chris@16: // the iterators supplied by Dinkumware. Some input iterators may Chris@16: // compile-time if they arrive here, and if the compiler is strict Chris@16: // about not taking the address of an rvalue. Chris@16: Chris@16: // This one detects ordinary mutable iterators - the result of Chris@16: // operator* is convertible to the value_type. Chris@16: template Chris@16: type_traits::yes_type is_mutable_iterator_helper(T const*, BOOST_DEDUCED_TYPENAME T::value_type*); Chris@16: Chris@16: // Since you can't take the address of an rvalue, the guts of Chris@16: // is_mutable_iterator_impl will fail if we use &*t directly. This Chris@16: // makes sure we can still work with non-lvalue iterators. Chris@16: template T* mutable_iterator_lvalue_helper(T& x); Chris@16: int mutable_iterator_lvalue_helper(...); Chris@16: Chris@16: Chris@16: // This one detects output iterators such as ostream_iterator which Chris@16: // return references to themselves. Chris@16: template Chris@16: type_traits::yes_type is_mutable_iterator_helper(T const*, T const*); Chris@16: Chris@16: type_traits::no_type is_mutable_iterator_helper(...); Chris@16: Chris@16: template Chris@16: struct is_mutable_iterator_impl Chris@16: { Chris@16: static T t; Chris@16: Chris@16: BOOST_STATIC_CONSTANT( Chris@16: bool, value = sizeof( Chris@16: detail::is_mutable_iterator_helper( Chris@16: (T*)0 Chris@16: , mutable_iterator_lvalue_helper(*t) // like &*t Chris@16: )) Chris@16: == sizeof(type_traits::yes_type) Chris@16: ); Chris@16: }; Chris@16: Chris@16: BOOST_TT_AUX_BOOL_TRAIT_DEF1( Chris@16: is_mutable_iterator,T,::boost::detail::is_mutable_iterator_impl::value) Chris@16: Chris@16: Chris@16: // is_full_iterator_traits -- Chris@16: // Chris@16: // A metafunction returning true iff T has all the requisite nested Chris@16: // types to satisfy the requirements for a fully-conforming Chris@16: // iterator_traits implementation. Chris@16: template Chris@16: struct is_full_iterator_traits_impl Chris@16: { Chris@16: enum { value = Chris@16: has_value_type::value Chris@16: & has_reference::value Chris@16: & has_pointer::value Chris@16: & has_difference_type::value Chris@16: & has_iterator_category::value Chris@16: }; Chris@16: }; Chris@16: Chris@16: BOOST_TT_AUX_BOOL_TRAIT_DEF1( Chris@16: is_full_iterator_traits,T,::boost::detail::is_full_iterator_traits_impl::value) Chris@16: Chris@16: Chris@16: # ifdef BOOST_BAD_CONTAINER_ITERATOR_CATEGORY_TYPEDEF Chris@16: BOOST_MPL_HAS_XXX_TRAIT_DEF(_Iterator_category) Chris@16: Chris@16: // is_stlport_40_debug_iterator -- Chris@16: // Chris@16: // A metafunction returning true iff T has all the requisite nested Chris@16: // types to satisfy the requirements of an STLPort 4.0 debug iterator Chris@16: // iterator_traits implementation. Chris@16: template Chris@16: struct is_stlport_40_debug_iterator_impl Chris@16: { Chris@16: enum { value = Chris@16: has_value_type::value Chris@16: & has_reference::value Chris@16: & has_pointer::value Chris@16: & has_difference_type::value Chris@16: & has__Iterator_category::value Chris@16: }; Chris@16: }; Chris@16: Chris@16: BOOST_TT_AUX_BOOL_TRAIT_DEF1( Chris@16: is_stlport_40_debug_iterator,T,::boost::detail::is_stlport_40_debug_iterator_impl::value) Chris@16: Chris@16: template Chris@16: struct stlport_40_debug_iterator_traits Chris@16: { Chris@16: typedef typename T::value_type value_type; Chris@16: typedef typename T::reference reference; Chris@16: typedef typename T::pointer pointer; Chris@16: typedef typename T::difference_type difference_type; Chris@16: typedef typename T::_Iterator_category iterator_category; Chris@16: }; Chris@16: # endif // BOOST_BAD_CONTAINER_ITERATOR_CATEGORY_TYPEDEF Chris@16: Chris@16: template struct pointer_iterator_traits; Chris@16: Chris@16: # ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION Chris@16: template Chris@16: struct pointer_iterator_traits Chris@16: { Chris@16: typedef typename remove_const::type value_type; Chris@16: typedef T* pointer; Chris@16: typedef T& reference; Chris@16: typedef std::random_access_iterator_tag iterator_category; Chris@16: typedef std::ptrdiff_t difference_type; Chris@16: }; Chris@16: # else Chris@16: Chris@16: // In case of no template partial specialization, and if T is a Chris@16: // pointer, iterator_traits::value_type can still be computed. For Chris@16: // some basic types, remove_pointer is manually defined in Chris@16: // type_traits/broken_compiler_spec.hpp. For others, do it yourself. Chris@16: Chris@16: template class please_invoke_BOOST_TT_BROKEN_COMPILER_SPEC_on_cv_unqualified_pointee; Chris@16: Chris@16: template Chris@16: struct pointer_value_type Chris@16: : mpl::if_< Chris@16: is_same::type> Chris@16: , please_invoke_BOOST_TT_BROKEN_COMPILER_SPEC_on_cv_unqualified_pointee

Chris@16: , typename remove_const< Chris@16: typename remove_pointer

::type Chris@16: >::type Chris@16: > Chris@16: { Chris@16: }; Chris@16: Chris@16: Chris@16: template Chris@16: struct pointer_reference Chris@16: : mpl::if_< Chris@16: is_same::type> Chris@16: , please_invoke_BOOST_TT_BROKEN_COMPILER_SPEC_on_cv_unqualified_pointee

Chris@16: , typename remove_pointer

::type& Chris@16: > Chris@16: { Chris@16: }; Chris@16: Chris@16: template Chris@16: struct pointer_iterator_traits Chris@16: { Chris@16: typedef T pointer; Chris@16: typedef std::random_access_iterator_tag iterator_category; Chris@16: typedef std::ptrdiff_t difference_type; Chris@16: Chris@16: typedef typename pointer_value_type::type value_type; Chris@16: typedef typename pointer_reference::type reference; Chris@16: }; Chris@16: Chris@16: # endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION Chris@16: Chris@16: // We'll sort iterator types into one of these classifications, from which we Chris@16: // can determine the difference_type, pointer, reference, and value_type Chris@16: template Chris@16: struct standard_iterator_traits Chris@16: { Chris@16: typedef typename Iterator::difference_type difference_type; Chris@16: typedef typename Iterator::value_type value_type; Chris@16: typedef typename Iterator::pointer pointer; Chris@16: typedef typename Iterator::reference reference; Chris@16: typedef typename Iterator::iterator_category iterator_category; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct msvc_stdlib_mutable_traits Chris@16: : std::iterator_traits Chris@16: { Chris@16: typedef typename std::iterator_traits::distance_type difference_type; Chris@16: typedef typename std::iterator_traits::value_type* pointer; Chris@16: typedef typename std::iterator_traits::value_type& reference; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct msvc_stdlib_const_traits Chris@16: : std::iterator_traits Chris@16: { Chris@16: typedef typename std::iterator_traits::distance_type difference_type; Chris@16: typedef const typename std::iterator_traits::value_type* pointer; Chris@16: typedef const typename std::iterator_traits::value_type& reference; Chris@16: }; Chris@16: Chris@16: # ifdef BOOST_BAD_OUTPUT_ITERATOR_SPECIALIZATION Chris@16: template Chris@16: struct is_bad_output_iterator Chris@16: : is_base_and_derived< Chris@16: std::iterator Chris@16: , Iterator> Chris@16: { Chris@16: }; Chris@16: Chris@16: struct bad_output_iterator_traits Chris@16: { Chris@16: typedef void value_type; Chris@16: typedef void difference_type; Chris@16: typedef std::output_iterator_tag iterator_category; Chris@16: typedef void pointer; Chris@16: typedef void reference; Chris@16: }; Chris@16: # endif Chris@16: Chris@16: // If we're looking at an MSVC6 (old Dinkumware) ``standard'' Chris@16: // iterator, this will generate an appropriate traits class. Chris@16: template Chris@16: struct msvc_stdlib_iterator_traits Chris@16: : mpl::if_< Chris@16: is_mutable_iterator Chris@16: , msvc_stdlib_mutable_traits Chris@16: , msvc_stdlib_const_traits Chris@16: >::type Chris@16: {}; Chris@16: Chris@16: template Chris@16: struct non_pointer_iterator_traits Chris@16: : mpl::if_< Chris@16: // if the iterator contains all the right nested types... Chris@16: is_full_iterator_traits Chris@16: // Use a standard iterator_traits implementation Chris@16: , standard_iterator_traits Chris@16: # ifdef BOOST_BAD_CONTAINER_ITERATOR_CATEGORY_TYPEDEF Chris@16: // Check for STLPort 4.0 broken _Iterator_category type Chris@16: , mpl::if_< Chris@16: is_stlport_40_debug_iterator Chris@16: , stlport_40_debug_iterator_traits Chris@16: # endif Chris@16: // Otherwise, assume it's a Dinkum iterator Chris@16: , msvc_stdlib_iterator_traits Chris@16: # ifdef BOOST_BAD_CONTAINER_ITERATOR_CATEGORY_TYPEDEF Chris@16: >::type Chris@16: # endif Chris@16: >::type Chris@16: { Chris@16: }; Chris@16: Chris@16: template Chris@16: struct iterator_traits_aux Chris@16: : mpl::if_< Chris@16: is_pointer Chris@16: , pointer_iterator_traits Chris@16: , non_pointer_iterator_traits Chris@16: >::type Chris@16: { Chris@16: }; Chris@16: Chris@16: template Chris@16: struct iterator_traits Chris@16: { Chris@16: // Explicit forwarding from base class needed to keep MSVC6 happy Chris@16: // under some circumstances. Chris@16: private: Chris@16: # ifdef BOOST_BAD_OUTPUT_ITERATOR_SPECIALIZATION Chris@16: typedef Chris@16: typename mpl::if_< Chris@16: is_bad_output_iterator Chris@16: , bad_output_iterator_traits Chris@16: , iterator_traits_aux Chris@16: >::type base; Chris@16: # else Chris@16: typedef iterator_traits_aux base; Chris@16: # endif Chris@16: public: Chris@16: typedef typename base::value_type value_type; Chris@16: typedef typename base::pointer pointer; Chris@16: typedef typename base::reference reference; Chris@16: typedef typename base::difference_type difference_type; Chris@16: typedef typename base::iterator_category iterator_category; Chris@16: }; Chris@16: Chris@16: // This specialization cuts off ETI (Early Template Instantiation) for MSVC. Chris@16: template <> struct iterator_traits Chris@16: { Chris@16: typedef int value_type; Chris@16: typedef int pointer; Chris@16: typedef int reference; Chris@16: typedef int difference_type; Chris@16: typedef int iterator_category; Chris@16: }; Chris@16: Chris@16: }} // namespace boost::detail Chris@16: Chris@16: # endif // workarounds Chris@16: Chris@16: namespace boost { namespace detail { Chris@16: Chris@16: namespace iterator_traits_ Chris@16: { Chris@16: template Chris@16: struct distance_select Chris@16: { Chris@16: static Difference execute(Iterator i1, const Iterator i2, ...) Chris@16: { Chris@16: Difference result = 0; Chris@16: while (i1 != i2) Chris@16: { Chris@16: ++i1; Chris@16: ++result; Chris@16: } Chris@16: return result; Chris@16: } Chris@16: Chris@16: static Difference execute(Iterator i1, const Iterator i2, std::random_access_iterator_tag*) Chris@16: { Chris@16: return i2 - i1; Chris@16: } Chris@16: }; Chris@16: } // namespace boost::detail::iterator_traits_ Chris@16: Chris@16: template Chris@16: inline typename iterator_traits::difference_type Chris@16: distance(Iterator first, Iterator last) Chris@16: { Chris@16: typedef typename iterator_traits::difference_type diff_t; Chris@16: typedef typename ::boost::detail::iterator_traits::iterator_category iterator_category; Chris@16: Chris@16: return iterator_traits_::distance_select::execute( Chris@16: first, last, (iterator_category*)0); Chris@16: } Chris@16: Chris@16: }} Chris@16: Chris@16: # endif Chris@16: Chris@16: Chris@16: # undef BOOST_BAD_CONTAINER_ITERATOR_CATEGORY_TYPEDEF Chris@16: # undef BOOST_BAD_OUTPUT_ITERATOR_SPECIALIZATION Chris@16: Chris@16: #endif // ITERATOR_DWA122600_HPP_