1 // Copyright David Abrahams 2003.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 #ifndef COUNTING_ITERATOR_DWA200348_HPP
6 # define COUNTING_ITERATOR_DWA200348_HPP
8 # include <boost/iterator/iterator_adaptor.hpp>
9 # include <boost/detail/numeric_traits.hpp>
10 # include <boost/mpl/bool.hpp>
11 # include <boost/mpl/if.hpp>
12 # include <boost/mpl/identity.hpp>
13 # include <boost/mpl/eval_if.hpp>
20 , class CategoryOrTraversal
23 class counting_iterator;
27 // Try to detect numeric types at compile time in ways compatible
28 // with the limitations of the compiler and library.
30 struct is_numeric_impl
32 // For a while, this wasn't true, but we rely on it below. This is a regression assert.
33 BOOST_STATIC_ASSERT(::boost::is_integral<char>::value);
35 # ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
37 BOOST_STATIC_CONSTANT(bool, value = std::numeric_limits<T>::is_specialized);
41 # if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
42 BOOST_STATIC_CONSTANT(
44 boost::is_convertible<int,T>::value
45 && boost::is_convertible<T,int>::value
48 BOOST_STATIC_CONSTANT(bool, value = ::boost::is_arithmetic<T>::value);
56 : mpl::bool_<(::boost::iterators::detail::is_numeric_impl<T>::value)>
59 # if defined(BOOST_HAS_LONG_LONG)
61 struct is_numeric< ::boost::long_long_type>
65 struct is_numeric< ::boost::ulong_long_type>
69 // Some compilers fail to have a numeric_limits specialization
71 struct is_numeric<wchar_t>
75 struct numeric_difference
77 typedef typename boost::detail::numeric_traits<T>::difference_type type;
80 BOOST_STATIC_ASSERT(is_numeric<int>::value);
82 template <class Incrementable, class CategoryOrTraversal, class Difference>
83 struct counting_iterator_base
85 typedef typename detail::ia_dflt_help<
88 is_numeric<Incrementable>
89 , mpl::identity<random_access_traversal_tag>
90 , iterator_traversal<Incrementable>
94 typedef typename detail::ia_dflt_help<
97 is_numeric<Incrementable>
98 , numeric_difference<Incrementable>
99 , iterator_difference<Incrementable>
103 typedef iterator_adaptor<
104 counting_iterator<Incrementable, CategoryOrTraversal, Difference> // self
105 , Incrementable // Base
106 , Incrementable // Value
107 # ifndef BOOST_ITERATOR_REF_CONSTNESS_KILLS_WRITABILITY
108 const // MSVC won't strip this. Instead we enable Thomas'
109 // criterion (see boost/iterator/detail/facade_iterator_category.hpp)
112 , Incrementable const& // reference
117 // Template class distance_policy_select -- choose a policy for computing the
118 // distance between counting_iterators at compile-time based on whether or not
119 // the iterator wraps an integer or an iterator, using "poor man's partial
122 template <bool is_integer> struct distance_policy_select;
124 // A policy for wrapped iterators
125 template <class Difference, class Incrementable1, class Incrementable2>
126 struct iterator_distance
128 static Difference distance(Incrementable1 x, Incrementable2 y)
134 // A policy for wrapped numbers
135 template <class Difference, class Incrementable1, class Incrementable2>
136 struct number_distance
138 static Difference distance(Incrementable1 x, Incrementable2 y)
140 return boost::detail::numeric_distance(x, y);
147 , class CategoryOrTraversal = use_default
148 , class Difference = use_default
150 class counting_iterator
151 : public detail::counting_iterator_base<
152 Incrementable, CategoryOrTraversal, Difference
155 typedef typename detail::counting_iterator_base<
156 Incrementable, CategoryOrTraversal, Difference
159 friend class iterator_core_access;
162 typedef typename super_t::difference_type difference_type;
164 counting_iterator() { }
166 counting_iterator(counting_iterator const& rhs) : super_t(rhs.base()) {}
168 counting_iterator(Incrementable x)
174 template<class OtherIncrementable>
176 counting_iterator<OtherIncrementable, CategoryOrTraversal, Difference> const& t
177 , typename enable_if_convertible<OtherIncrementable, Incrementable>::type* = 0
185 typename super_t::reference dereference() const
187 return this->base_reference();
190 template <class OtherIncrementable>
192 distance_to(counting_iterator<OtherIncrementable, CategoryOrTraversal, Difference> const& y) const
194 typedef typename mpl::if_<
195 detail::is_numeric<Incrementable>
196 , detail::number_distance<difference_type, Incrementable, OtherIncrementable>
197 , detail::iterator_distance<difference_type, Incrementable, OtherIncrementable>
200 return d::distance(this->base(), y.base());
204 // Manufacture a counting iterator for an arbitrary incrementable type
205 template <class Incrementable>
206 inline counting_iterator<Incrementable>
207 make_counting_iterator(Incrementable x)
209 typedef counting_iterator<Incrementable> result_t;
213 } // namespace iterators
215 using iterators::counting_iterator;
216 using iterators::make_counting_iterator;
220 #endif // COUNTING_ITERATOR_DWA200348_HPP