1 // Copyright David Abrahams 2003.
\r
2 // Distributed under the Boost Software License, Version 1.0. (See
\r
3 // accompanying file LICENSE_1_0.txt or copy at
\r
4 // http://www.boost.org/LICENSE_1_0.txt)
\r
5 #ifndef COUNTING_ITERATOR_DWA200348_HPP
\r
6 # define COUNTING_ITERATOR_DWA200348_HPP
\r
8 # include <boost/iterator/iterator_adaptor.hpp>
\r
9 # include <boost/detail/numeric_traits.hpp>
\r
10 # include <boost/mpl/bool.hpp>
\r
11 # include <boost/mpl/if.hpp>
\r
12 # include <boost/mpl/identity.hpp>
\r
13 # include <boost/mpl/eval_if.hpp>
\r
19 , class CategoryOrTraversal
\r
22 class counting_iterator;
\r
26 // Try to detect numeric types at compile time in ways compatible
\r
27 // with the limitations of the compiler and library.
\r
29 struct is_numeric_impl
\r
31 // For a while, this wasn't true, but we rely on it below. This is a regression assert.
\r
32 BOOST_STATIC_ASSERT(::boost::is_integral<char>::value);
\r
34 # ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
\r
36 BOOST_STATIC_CONSTANT(bool, value = std::numeric_limits<T>::is_specialized);
\r
40 # if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
\r
41 BOOST_STATIC_CONSTANT(
\r
43 boost::is_convertible<int,T>::value
\r
44 && boost::is_convertible<T,int>::value
\r
47 BOOST_STATIC_CONSTANT(bool, value = ::boost::is_arithmetic<T>::value);
\r
55 : mpl::bool_<(::boost::detail::is_numeric_impl<T>::value)>
\r
58 # if defined(BOOST_HAS_LONG_LONG)
\r
60 struct is_numeric< ::boost::long_long_type>
\r
64 struct is_numeric< ::boost::ulong_long_type>
\r
68 // Some compilers fail to have a numeric_limits specialization
\r
70 struct is_numeric<wchar_t>
\r
74 struct numeric_difference
\r
76 typedef typename boost::detail::numeric_traits<T>::difference_type type;
\r
79 BOOST_STATIC_ASSERT(is_numeric<int>::value);
\r
81 template <class Incrementable, class CategoryOrTraversal, class Difference>
\r
82 struct counting_iterator_base
\r
84 typedef typename detail::ia_dflt_help<
\r
87 is_numeric<Incrementable>
\r
88 , mpl::identity<random_access_traversal_tag>
\r
89 , iterator_traversal<Incrementable>
\r
93 typedef typename detail::ia_dflt_help<
\r
96 is_numeric<Incrementable>
\r
97 , numeric_difference<Incrementable>
\r
98 , iterator_difference<Incrementable>
\r
100 >::type difference;
\r
102 typedef iterator_adaptor<
\r
103 counting_iterator<Incrementable, CategoryOrTraversal, Difference> // self
\r
104 , Incrementable // Base
\r
105 , Incrementable // Value
\r
106 # ifndef BOOST_ITERATOR_REF_CONSTNESS_KILLS_WRITABILITY
\r
107 const // MSVC won't strip this. Instead we enable Thomas'
\r
108 // criterion (see boost/iterator/detail/facade_iterator_category.hpp)
\r
111 , Incrementable const& // reference
\r
116 // Template class distance_policy_select -- choose a policy for computing the
\r
117 // distance between counting_iterators at compile-time based on whether or not
\r
118 // the iterator wraps an integer or an iterator, using "poor man's partial
\r
119 // specialization".
\r
121 template <bool is_integer> struct distance_policy_select;
\r
123 // A policy for wrapped iterators
\r
124 template <class Difference, class Incrementable1, class Incrementable2>
\r
125 struct iterator_distance
\r
127 static Difference distance(Incrementable1 x, Incrementable2 y)
\r
133 // A policy for wrapped numbers
\r
134 template <class Difference, class Incrementable1, class Incrementable2>
\r
135 struct number_distance
\r
137 static Difference distance(Incrementable1 x, Incrementable2 y)
\r
139 return numeric_distance(x, y);
\r
145 class Incrementable
\r
146 , class CategoryOrTraversal = use_default
\r
147 , class Difference = use_default
\r
149 class counting_iterator
\r
150 : public detail::counting_iterator_base<
\r
151 Incrementable, CategoryOrTraversal, Difference
\r
154 typedef typename detail::counting_iterator_base<
\r
155 Incrementable, CategoryOrTraversal, Difference
\r
158 friend class iterator_core_access;
\r
161 typedef typename super_t::difference_type difference_type;
\r
163 counting_iterator() { }
\r
165 counting_iterator(counting_iterator const& rhs) : super_t(rhs.base()) {}
\r
167 counting_iterator(Incrementable x)
\r
173 template<class OtherIncrementable>
\r
175 counting_iterator<OtherIncrementable, CategoryOrTraversal, Difference> const& t
\r
176 , typename enable_if_convertible<OtherIncrementable, Incrementable>::type* = 0
\r
178 : super_t(t.base())
\r
184 typename super_t::reference dereference() const
\r
186 return this->base_reference();
\r
189 template <class OtherIncrementable>
\r
191 distance_to(counting_iterator<OtherIncrementable, CategoryOrTraversal, Difference> const& y) const
\r
193 typedef typename mpl::if_<
\r
194 detail::is_numeric<Incrementable>
\r
195 , detail::number_distance<difference_type, Incrementable, OtherIncrementable>
\r
196 , detail::iterator_distance<difference_type, Incrementable, OtherIncrementable>
\r
199 return d::distance(this->base(), y.base());
\r
203 // Manufacture a counting iterator for an arbitrary incrementable type
\r
204 template <class Incrementable>
\r
205 inline counting_iterator<Incrementable>
\r
206 make_counting_iterator(Incrementable x)
\r
208 typedef counting_iterator<Incrementable> result_t;
\r
209 return result_t(x);
\r
213 } // namespace boost::iterator
\r
215 #endif // COUNTING_ITERATOR_DWA200348_HPP
\r