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
8 # include <boost/python/detail/prefix.hpp>
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>
15 # if defined(BOOST_MSVC) && (BOOST_MSVC == 1400) /*
16 > warning C4180: qualifier applied to function type has no meaning; ignored
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
22 # pragma warning(disable: 4180)
25 # include <boost/bind.hpp>
26 # include <boost/bind/protect.hpp>
28 namespace boost { namespace python {
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>
36 inline object make_iterator(
38 , Accessor2 get_finish
39 , NextPolicies next_policies
43 return objects::make_iterator_function<Target>(
44 boost::protect(boost::bind(get_start, _1))
45 , boost::protect(boost::bind(get_finish, _1))
50 // Guts of template class iterators<>, below.
51 template <bool const_ = false>
57 typedef typename T::iterator iterator;
58 static iterator begin(T& x) { return x.begin(); }
59 static iterator end(T& x) { return x.end(); }
64 struct iterators_impl<true>
69 typedef typename T::const_iterator iterator;
70 static iterator begin(T& x) { return x.begin(); }
71 static iterator end(T& x) { return x.end(); }
76 // An "ordinary function generator" which contains static begin(x) and
77 // end(x) functions that invoke T::begin() and T::end(), respectively.
80 : detail::iterators_impl<
81 detail::is_const<T>::value
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>
90 object range(Accessor1 start, Accessor2 finish)
92 return detail::make_iterator(
94 , objects::default_iterator_call_policies()
95 , detail::target(start)
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>
102 object range(Accessor1 start, Accessor2 finish, NextPolicies* = 0)
104 return detail::make_iterator(start, finish, NextPolicies(), detail::target(start));
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>
110 object range(Accessor1 start, Accessor2 finish, NextPolicies* = 0, boost::type<Target>* = 0)
112 // typedef typename add_reference<Target>::type target;
113 return detail::make_iterator(start, finish, NextPolicies(), (Target&(*)())0);
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
120 template <class Container
121 , class NextPolicies = objects::default_iterator_call_policies>
122 struct iterator : object
126 python::range<NextPolicies>(
127 &iterators<Container>::begin, &iterators<Container>::end
133 }} // namespace boost::python
135 #endif // ITERATOR_DWA2002512_HPP