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

sl@0: , typename remove_const< sl@0: typename remove_pointer

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

sl@0: , typename remove_pointer

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