1 // (C) Copyright Joel de Guzman 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)
6 #ifndef INDEXING_SUITE_JDG20036_HPP
7 # define INDEXING_SUITE_JDG20036_HPP
9 # include <boost/python/class.hpp>
10 # include <boost/python/def_visitor.hpp>
11 # include <boost/python/register_ptr_to_python.hpp>
12 # include <boost/python/suite/indexing/detail/indexing_suite_detail.hpp>
13 # include <boost/python/return_internal_reference.hpp>
14 # include <boost/python/iterator.hpp>
15 # include <boost/mpl/or.hpp>
16 # include <boost/mpl/not.hpp>
17 # include <boost/type_traits/is_same.hpp>
19 namespace boost { namespace python {
21 // indexing_suite class. This class is the facade class for
22 // the management of C++ containers intended to be integrated
23 // to Python. The objective is make a C++ container look and
24 // feel and behave exactly as we'd expect a Python container.
25 // By default indexed elements are returned by proxy. This can be
26 // disabled by supplying *true* in the NoProxy template parameter.
28 // Derived classes provide the hooks needed by the indexing_suite
32 // get_item(Container& container, index_type i);
35 // get_slice(Container& container, index_type from, index_type to);
38 // set_item(Container& container, index_type i, data_type const& v);
42 // Container& container, index_type from,
43 // index_type to, data_type const& v
46 // template <class Iter>
48 // set_slice(Container& container, index_type from,
49 // index_type to, Iter first, Iter last
53 // delete_item(Container& container, index_type i);
56 // delete_slice(Container& container, index_type from, index_type to);
59 // size(Container& container);
63 // contains(Container& container, T const& val);
66 // convert_index(Container& container, PyObject* i);
69 // adjust_index(index_type current, index_type from,
70 // index_type to, size_type len
73 // Most of these policies are self explanatory. convert_index and
74 // adjust_index, however, deserves some explanation.
76 // convert_index converts an Python index into a C++ index that the
77 // container can handle. For instance, negative indexes in Python, by
78 // convention, indexes from the right (e.g. C[-1] indexes the rightmost
79 // element in C). convert_index should handle the necessary conversion
80 // for the C++ container (e.g. convert -1 to C.size()-1). convert_index
81 // should also be able to convert the type of the index (A dynamic Python
82 // type) to the actual type that the C++ container expects.
84 // When a container expands or contracts, held indexes to its elements
85 // must be adjusted to follow the movement of data. For instance, if
86 // we erase 3 elements, starting from index 0 from a 5 element vector,
87 // what used to be at index 4 will now be at index 1:
89 // [a][b][c][d][e] ---> [d][e]
93 // adjust_index takes care of the adjustment. Given a current index,
94 // the function should return the adjusted index when data in the
95 // container at index from..to is replaced by *len* elements.
100 , class DerivedPolicies
101 , bool NoProxy = false
102 , bool NoSlice = false
103 , class Data = typename Container::value_type
104 , class Index = typename Container::size_type
105 , class Key = typename Container::value_type
108 : public def_visitor<
123 , mpl::not_<is_class<Data> >
125 is_same<Data, std::string>
126 , is_same<Data, std::complex<float> >
127 , is_same<Data, std::complex<double> >
128 , is_same<Data, std::complex<long double> > >::type>
131 typedef detail::container_element<Container, Index, DerivedPolicies>
134 typedef return_internal_reference<> return_policy;
136 typedef typename mpl::if_<
138 , iterator<Container>
139 , iterator<Container, return_policy> >::type
142 typedef typename mpl::if_<
144 , detail::no_proxy_helper<
147 , container_element_t
149 , detail::proxy_helper<
152 , container_element_t
156 typedef typename mpl::if_<
158 , detail::no_slice_helper<
164 , detail::slice_helper<
174 template <class Class>
175 void visit(Class& cl) const
177 // Hook into the class_ generic visitation .def function
178 proxy_handler::register_container_element();
181 .def("__len__", base_size)
182 .def("__setitem__", &base_set_item)
183 .def("__delitem__", &base_delete_item)
184 .def("__getitem__", &base_get_item)
185 .def("__contains__", &base_contains)
186 .def("__iter__", def_iterator())
189 DerivedPolicies::extension_def(cl);
192 template <class Class>
194 extension_def(Class& cl)
197 // no more extensions
203 base_get_item(back_reference<Container&> container, PyObject* i)
205 if (PySlice_Check(i))
206 return slice_handler::base_get_slice(
207 container.get(), static_cast<PySliceObject*>(static_cast<void*>(i)));
209 return proxy_handler::base_get_item_(container, i);
213 base_set_item(Container& container, PyObject* i, PyObject* v)
215 if (PySlice_Check(i))
217 slice_handler::base_set_slice(container,
218 static_cast<PySliceObject*>(static_cast<void*>(i)), v);
222 extract<Data&> elem(v);
223 // try if elem is an exact Data
229 convert_index(container, i), elem());
233 // try to convert elem to Data
234 extract<Data> elem(v);
240 convert_index(container, i), elem());
244 PyErr_SetString(PyExc_TypeError, "Invalid assignment");
245 throw_error_already_set();
252 base_delete_item(Container& container, PyObject* i)
254 if (PySlice_Check(i))
256 slice_handler::base_delete_slice(
257 container, static_cast<PySliceObject*>(static_cast<void*>(i)));
261 Index index = DerivedPolicies::convert_index(container, i);
262 proxy_handler::base_erase_index(container, index, mpl::bool_<NoSlice>());
263 DerivedPolicies::delete_item(container, index);
267 base_size(Container& container)
269 return DerivedPolicies::size(container);
273 base_contains(Container& container, PyObject* key)
275 extract<Key const&> x(key);
276 // try if key is an exact Key type
279 return DerivedPolicies::contains(container, x());
283 // try to convert key to Key type
286 return DerivedPolicies::contains(container, x());
293 }} // namespace boost::python
295 #endif // INDEXING_SUITE_JDG20036_HPP