1 // Copyright David Abrahams 2002.
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 ITERATOR_DWA2002512_HPP
6 # define ITERATOR_DWA2002512_HPP
7 
8 # include <boost/python/detail/prefix.hpp>
9 
10 # include <boost/python/detail/target.hpp>
11 # include <boost/python/detail/type_traits.hpp>
12 # include <boost/python/object/iterator.hpp>
13 # include <boost/python/object_core.hpp>
14 
15 # if defined(BOOST_MSVC) && (BOOST_MSVC == 1400) /*
16 > warning C4180: qualifier applied to function type has no meaning; ignored
17 Peter Dimov wrote:
18 This warning is caused by an overload resolution bug in VC8 that cannot be
19 worked around and will probably not be fixed by MS in the VC8 line. The
20 problematic overload is only instantiated and never called, and the code
21 works correctly. */
22 #  pragma warning(disable: 4180)
23 # endif
24 
25 # include <boost/bind.hpp>
26 # include <boost/bind/protect.hpp>
27 
28 namespace boost { namespace python {
29 
30 namespace detail
31 {
32   // Adds an additional layer of binding to
33   // objects::make_iterator(...), which allows us to pass member
34   // function and member data pointers.
35   template <class Target, class Accessor1, class Accessor2, class NextPolicies>
make_iterator(Accessor1 get_start,Accessor2 get_finish,NextPolicies next_policies,Target & (*)())36   inline object make_iterator(
37       Accessor1 get_start
38     , Accessor2 get_finish
39     , NextPolicies next_policies
40     , Target&(*)()
41   )
42   {
43       return objects::make_iterator_function<Target>(
44           boost::protect(boost::bind(get_start, _1))
45         , boost::protect(boost::bind(get_finish, _1))
46         , next_policies
47       );
48   }
49 
50   // Guts of template class iterators<>, below.
51   template <bool const_ = false>
52   struct iterators_impl
53   {
54       template <class T>
55       struct apply
56       {
57           typedef typename T::iterator iterator;
beginboost::python::detail::iterators_impl::apply58           static iterator begin(T& x) { return x.begin(); }
endboost::python::detail::iterators_impl::apply59           static iterator end(T& x) { return x.end(); }
60       };
61   };
62 
63   template <>
64   struct iterators_impl<true>
65   {
66       template <class T>
67       struct apply
68       {
69           typedef typename T::const_iterator iterator;
beginboost::python::detail::iterators_impl::apply70           static iterator begin(T& x) { return x.begin(); }
endboost::python::detail::iterators_impl::apply71           static iterator end(T& x) { return x.end(); }
72       };
73   };
74 }
75 
76 // An "ordinary function generator" which contains static begin(x) and
77 // end(x) functions that invoke T::begin() and T::end(), respectively.
78 template <class T>
79 struct iterators
80     : detail::iterators_impl<
81         detail::is_const<T>::value
82       >::template apply<T>
83 {
84 };
85 
86 // Create an iterator-building function which uses the given
87 // accessors. Deduce the Target type from the accessors. The iterator
88 // returns copies of the inderlying elements.
89 template <class Accessor1, class Accessor2>
range(Accessor1 start,Accessor2 finish)90 object range(Accessor1 start, Accessor2 finish)
91 {
92     return detail::make_iterator(
93         start, finish
94       , objects::default_iterator_call_policies()
95       , detail::target(start)
96     );
97 }
98 
99 // Create an iterator-building function which uses the given accessors
100 // and next() policies. Deduce the Target type.
101 template <class NextPolicies, class Accessor1, class Accessor2>
range(Accessor1 start,Accessor2 finish,NextPolicies * =0)102 object range(Accessor1 start, Accessor2 finish, NextPolicies* = 0)
103 {
104     return detail::make_iterator(start, finish, NextPolicies(), detail::target(start));
105 }
106 
107 // Create an iterator-building function which uses the given accessors
108 // and next() policies, operating on the given Target type
109 template <class NextPolicies, class Target, class Accessor1, class Accessor2>
range(Accessor1 start,Accessor2 finish,NextPolicies * =0,boost::type<Target> * =0)110 object range(Accessor1 start, Accessor2 finish, NextPolicies* = 0, boost::type<Target>* = 0)
111 {
112     // typedef typename add_reference<Target>::type target;
113     return detail::make_iterator(start, finish, NextPolicies(), (Target&(*)())0);
114 }
115 
116 // A Python callable object which produces an iterator traversing
117 // [x.begin(), x.end()), where x is an instance of the Container
118 // type. NextPolicies are used as the CallPolicies for the iterator's
119 // next() function.
120 template <class Container
121           , class NextPolicies = objects::default_iterator_call_policies>
122 struct iterator : object
123 {
iteratorboost::python::iterator124     iterator()
125         : object(
126             python::range<NextPolicies>(
127                 &iterators<Container>::begin, &iterators<Container>::end
128                 ))
129     {
130     }
131 };
132 
133 }} // namespace boost::python
134 
135 #endif // ITERATOR_DWA2002512_HPP
136