// Copyright David Abrahams 2003. // 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) #ifndef ACTIV_BOOST_COUNTING_ITERATOR_DWA200348_HPP # define ACTIV_BOOST_COUNTING_ITERATOR_DWA200348_HPP # include # include # ifndef ACTIV_BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS # include # elif !ACTIV_BOOST_WORKAROUND(ACTIV_BOOST_BORLANDC, ACTIV_BOOST_TESTED_AT(0x551)) # include # else # include # endif # include # include # include # include # include # include namespace Boost { namespace iterators { template < class Incrementable , class CategoryOrTraversal , class Difference > class counting_iterator; namespace detail { // Try to detect numeric types at compile time in ways compatible // with the limitations of the compiler and library. template struct is_numeric_impl { // For a while, this wasn't true, but we rely on it below. This is a regression assert. ACTIV_BOOST_STATIC_ASSERT(::Boost::is_integral::value); # ifndef ACTIV_BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS ACTIV_BOOST_STATIC_CONSTANT(bool, value = std::numeric_limits::is_specialized); # else # if !ACTIV_BOOST_WORKAROUND(ACTIV_BOOST_BORLANDC, ACTIV_BOOST_TESTED_AT(0x551)) ACTIV_BOOST_STATIC_CONSTANT( bool, value = ( Boost::is_convertible::value && Boost::is_convertible::value )); # else ACTIV_BOOST_STATIC_CONSTANT(bool, value = ::Boost::is_arithmetic::value); # endif # endif }; template struct is_numeric : Boost::integral_constant::value> {}; # if defined(ACTIV_BOOST_HAS_LONG_LONG) template <> struct is_numeric : Boost::true_type {}; template <> struct is_numeric : Boost::true_type {}; # endif # if defined(ACTIV_BOOST_HAS_INT128) template <> struct is_numeric : Boost::true_type {}; template <> struct is_numeric : Boost::true_type {}; # endif // Some compilers fail to have a numeric_limits specialization template <> struct is_numeric : true_type {}; template struct numeric_difference { typedef typename Boost::detail::numeric_traits::difference_type type; }; # if defined(ACTIV_BOOST_HAS_INT128) // std::numeric_limits, which is used by numeric_traits, is not specialized for __int128 in some standard libraries template <> struct numeric_difference { typedef Boost::int128_type type; }; template <> struct numeric_difference { typedef Boost::int128_type type; }; # endif template struct counting_iterator_base { typedef typename detail::ia_dflt_help< CategoryOrTraversal , typename Boost::conditional< is_numeric::value , Boost::type_identity , iterator_traversal >::type >::type traversal; typedef typename detail::ia_dflt_help< Difference , typename Boost::conditional< is_numeric::value , numeric_difference , iterator_difference >::type >::type difference; typedef iterator_adaptor< counting_iterator // self , Incrementable // Base , Incrementable // Value # ifndef ACTIV_BOOST_ITERATOR_REF_CONSTNESS_KILLS_WRITABILITY const // MSVC won't strip this. Instead we enable Thomas' // criterion (see ActivBoost/iterator/detail/facade_iterator_category.hpp) # endif , traversal , Incrementable const& // reference , difference > type; }; // Template class distance_policy_select -- choose a policy for computing the // distance between counting_iterators at compile-time based on whether or not // the iterator wraps an integer or an iterator, using "poor man's partial // specialization". template struct distance_policy_select; // A policy for wrapped iterators template struct iterator_distance { static Difference distance(Incrementable1 x, Incrementable2 y) { return y - x; } }; // A policy for wrapped numbers template struct number_distance { static Difference distance(Incrementable1 x, Incrementable2 y) { return Boost::detail::numeric_distance(x, y); } }; } template < class Incrementable , class CategoryOrTraversal = use_default , class Difference = use_default > class counting_iterator : public detail::counting_iterator_base< Incrementable, CategoryOrTraversal, Difference >::type { typedef typename detail::counting_iterator_base< Incrementable, CategoryOrTraversal, Difference >::type super_t; friend class iterator_core_access; public: typedef typename super_t::difference_type difference_type; ACTIV_BOOST_DEFAULTED_FUNCTION(counting_iterator(), {}) ACTIV_BOOST_DEFAULTED_FUNCTION(counting_iterator(counting_iterator const& rhs), : super_t(rhs.base()) {}) counting_iterator(Incrementable x) : super_t(x) { } # if 0 template counting_iterator( counting_iterator const& t , typename enable_if_convertible::type* = 0 ) : super_t(t.base()) {} # endif ACTIV_BOOST_DEFAULTED_FUNCTION(counting_iterator& operator=(counting_iterator const& rhs), { *static_cast< super_t* >(this) = static_cast< super_t const& >(rhs); return *this; }) private: typename super_t::reference dereference() const { return this->base_reference(); } template difference_type distance_to(counting_iterator const& y) const { typedef typename Boost::conditional< detail::is_numeric::value , detail::number_distance , detail::iterator_distance >::type d; return d::distance(this->base(), y.base()); } }; // Manufacture a counting iterator for an arbitrary incrementable type template inline counting_iterator make_counting_iterator(Incrementable x) { typedef counting_iterator result_t; return result_t(x); } } // namespace iterators using iterators::counting_iterator; using iterators::make_counting_iterator; } // namespace Boost #endif // ACTIV_BOOST_COUNTING_ITERATOR_DWA200348_HPP