indexing_suite.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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)
  5. #ifndef INDEXING_SUITE_JDG20036_HPP
  6. # define INDEXING_SUITE_JDG20036_HPP
  7. # include <boost/python/class.hpp>
  8. # include <boost/python/def_visitor.hpp>
  9. # include <boost/python/register_ptr_to_python.hpp>
  10. # include <boost/python/suite/indexing/detail/indexing_suite_detail.hpp>
  11. # include <boost/python/return_internal_reference.hpp>
  12. # include <boost/python/iterator.hpp>
  13. # include <boost/mpl/or.hpp>
  14. # include <boost/mpl/not.hpp>
  15. # include <boost/python/detail/type_traits.hpp>
  16. namespace boost { namespace python {
  17. // indexing_suite class. This class is the facade class for
  18. // the management of C++ containers intended to be integrated
  19. // to Python. The objective is make a C++ container look and
  20. // feel and behave exactly as we'd expect a Python container.
  21. // By default indexed elements are returned by proxy. This can be
  22. // disabled by supplying *true* in the NoProxy template parameter.
  23. //
  24. // Derived classes provide the hooks needed by the indexing_suite
  25. // to do its job:
  26. //
  27. // static data_type&
  28. // get_item(Container& container, index_type i);
  29. //
  30. // static object
  31. // get_slice(Container& container, index_type from, index_type to);
  32. //
  33. // static void
  34. // set_item(Container& container, index_type i, data_type const& v);
  35. //
  36. // static void
  37. // set_slice(
  38. // Container& container, index_type from,
  39. // index_type to, data_type const& v
  40. // );
  41. //
  42. // template <class Iter>
  43. // static void
  44. // set_slice(Container& container, index_type from,
  45. // index_type to, Iter first, Iter last
  46. // );
  47. //
  48. // static void
  49. // delete_item(Container& container, index_type i);
  50. //
  51. // static void
  52. // delete_slice(Container& container, index_type from, index_type to);
  53. //
  54. // static size_t
  55. // size(Container& container);
  56. //
  57. // template <class T>
  58. // static bool
  59. // contains(Container& container, T const& val);
  60. //
  61. // static index_type
  62. // convert_index(Container& container, PyObject* i);
  63. //
  64. // static index_type
  65. // adjust_index(index_type current, index_type from,
  66. // index_type to, size_type len
  67. // );
  68. //
  69. // Most of these policies are self explanatory. convert_index and
  70. // adjust_index, however, deserves some explanation.
  71. //
  72. // convert_index converts an Python index into a C++ index that the
  73. // container can handle. For instance, negative indexes in Python, by
  74. // convention, indexes from the right (e.g. C[-1] indexes the rightmost
  75. // element in C). convert_index should handle the necessary conversion
  76. // for the C++ container (e.g. convert -1 to C.size()-1). convert_index
  77. // should also be able to convert the type of the index (A dynamic Python
  78. // type) to the actual type that the C++ container expects.
  79. //
  80. // When a container expands or contracts, held indexes to its elements
  81. // must be adjusted to follow the movement of data. For instance, if
  82. // we erase 3 elements, starting from index 0 from a 5 element vector,
  83. // what used to be at index 4 will now be at index 1:
  84. //
  85. // [a][b][c][d][e] ---> [d][e]
  86. // ^ ^
  87. // 4 1
  88. //
  89. // adjust_index takes care of the adjustment. Given a current index,
  90. // the function should return the adjusted index when data in the
  91. // container at index from..to is replaced by *len* elements.
  92. //
  93. template <
  94. class Container
  95. , class DerivedPolicies
  96. , bool NoProxy = false
  97. , bool NoSlice = false
  98. , class Data = typename Container::value_type
  99. , class Index = typename Container::size_type
  100. , class Key = typename Container::value_type
  101. >
  102. class indexing_suite
  103. : public def_visitor<
  104. indexing_suite<
  105. Container
  106. , DerivedPolicies
  107. , NoProxy
  108. , NoSlice
  109. , Data
  110. , Index
  111. , Key
  112. > >
  113. {
  114. private:
  115. typedef mpl::or_<
  116. mpl::bool_<NoProxy>
  117. , mpl::not_<is_class<Data> >
  118. , typename mpl::or_<
  119. detail::is_same<Data, std::string>
  120. , detail::is_same<Data, std::complex<float> >
  121. , detail::is_same<Data, std::complex<double> >
  122. , detail::is_same<Data, std::complex<long double> > >::type>
  123. no_proxy;
  124. typedef detail::container_element<Container, Index, DerivedPolicies>
  125. container_element_t;
  126. typedef return_internal_reference<> return_policy;
  127. typedef typename mpl::if_<
  128. no_proxy
  129. , iterator<Container>
  130. , iterator<Container, return_policy> >::type
  131. def_iterator;
  132. typedef typename mpl::if_<
  133. no_proxy
  134. , detail::no_proxy_helper<
  135. Container
  136. , DerivedPolicies
  137. , container_element_t
  138. , Index>
  139. , detail::proxy_helper<
  140. Container
  141. , DerivedPolicies
  142. , container_element_t
  143. , Index> >::type
  144. proxy_handler;
  145. typedef typename mpl::if_<
  146. mpl::bool_<NoSlice>
  147. , detail::no_slice_helper<
  148. Container
  149. , DerivedPolicies
  150. , proxy_handler
  151. , Data
  152. , Index>
  153. , detail::slice_helper<
  154. Container
  155. , DerivedPolicies
  156. , proxy_handler
  157. , Data
  158. , Index> >::type
  159. slice_handler;
  160. public:
  161. template <class Class>
  162. void visit(Class& cl) const
  163. {
  164. // Hook into the class_ generic visitation .def function
  165. proxy_handler::register_container_element();
  166. cl
  167. .def("__len__", base_size)
  168. .def("__setitem__", &base_set_item)
  169. .def("__delitem__", &base_delete_item)
  170. .def("__getitem__", &base_get_item)
  171. .def("__contains__", &base_contains)
  172. .def("__iter__", def_iterator())
  173. ;
  174. DerivedPolicies::extension_def(cl);
  175. }
  176. template <class Class>
  177. static void
  178. extension_def(Class& cl)
  179. {
  180. // default.
  181. // no more extensions
  182. }
  183. private:
  184. static object
  185. base_get_item(back_reference<Container&> container, PyObject* i)
  186. {
  187. if (PySlice_Check(i))
  188. return slice_handler::base_get_slice(
  189. container.get(), static_cast<PySliceObject*>(static_cast<void*>(i)));
  190. return proxy_handler::base_get_item_(container, i);
  191. }
  192. static void
  193. base_set_item(Container& container, PyObject* i, PyObject* v)
  194. {
  195. if (PySlice_Check(i))
  196. {
  197. slice_handler::base_set_slice(container,
  198. static_cast<PySliceObject*>(static_cast<void*>(i)), v);
  199. }
  200. else
  201. {
  202. extract<Data&> elem(v);
  203. // try if elem is an exact Data
  204. if (elem.check())
  205. {
  206. DerivedPolicies::
  207. set_item(container,
  208. DerivedPolicies::
  209. convert_index(container, i), elem());
  210. }
  211. else
  212. {
  213. // try to convert elem to Data
  214. extract<Data> elem(v);
  215. if (elem.check())
  216. {
  217. DerivedPolicies::
  218. set_item(container,
  219. DerivedPolicies::
  220. convert_index(container, i), elem());
  221. }
  222. else
  223. {
  224. PyErr_SetString(PyExc_TypeError, "Invalid assignment");
  225. throw_error_already_set();
  226. }
  227. }
  228. }
  229. }
  230. static void
  231. base_delete_item(Container& container, PyObject* i)
  232. {
  233. if (PySlice_Check(i))
  234. {
  235. slice_handler::base_delete_slice(
  236. container, static_cast<PySliceObject*>(static_cast<void*>(i)));
  237. return;
  238. }
  239. Index index = DerivedPolicies::convert_index(container, i);
  240. proxy_handler::base_erase_index(container, index, mpl::bool_<NoSlice>());
  241. DerivedPolicies::delete_item(container, index);
  242. }
  243. static size_t
  244. base_size(Container& container)
  245. {
  246. return DerivedPolicies::size(container);
  247. }
  248. static bool
  249. base_contains(Container& container, PyObject* key)
  250. {
  251. extract<Key const&> x(key);
  252. // try if key is an exact Key type
  253. if (x.check())
  254. {
  255. return DerivedPolicies::contains(container, x());
  256. }
  257. else
  258. {
  259. // try to convert key to Key type
  260. extract<Key> x(key);
  261. if (x.check())
  262. return DerivedPolicies::contains(container, x());
  263. else
  264. return false;
  265. }
  266. }
  267. };
  268. }} // namespace boost::python
  269. #endif // INDEXING_SUITE_JDG20036_HPP