indexing_suite.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/type_traits/is_same.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. is_same<Data, std::string>
  120. , is_same<Data, std::complex<float> >
  121. , is_same<Data, std::complex<double> >
  122. , is_same<Data, std::complex<long double> > >::type>
  123. no_proxy;
  124. typedef detail::container_element<Container, Index, DerivedPolicies>
  125. container_element_t;
  126. #if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
  127. struct return_policy : return_internal_reference<> {};
  128. #else
  129. typedef return_internal_reference<> return_policy;
  130. #endif
  131. typedef typename mpl::if_<
  132. no_proxy
  133. , iterator<Container>
  134. , iterator<Container, return_policy> >::type
  135. def_iterator;
  136. typedef typename mpl::if_<
  137. no_proxy
  138. , detail::no_proxy_helper<
  139. Container
  140. , DerivedPolicies
  141. , container_element_t
  142. , Index>
  143. , detail::proxy_helper<
  144. Container
  145. , DerivedPolicies
  146. , container_element_t
  147. , Index> >::type
  148. proxy_handler;
  149. typedef typename mpl::if_<
  150. mpl::bool_<NoSlice>
  151. , detail::no_slice_helper<
  152. Container
  153. , DerivedPolicies
  154. , proxy_handler
  155. , Data
  156. , Index>
  157. , detail::slice_helper<
  158. Container
  159. , DerivedPolicies
  160. , proxy_handler
  161. , Data
  162. , Index> >::type
  163. slice_handler;
  164. public:
  165. template <class Class>
  166. void visit(Class& cl) const
  167. {
  168. // Hook into the class_ generic visitation .def function
  169. proxy_handler::register_container_element();
  170. cl
  171. .def("__len__", base_size)
  172. .def("__setitem__", &base_set_item)
  173. .def("__delitem__", &base_delete_item)
  174. .def("__getitem__", &base_get_item)
  175. .def("__contains__", &base_contains)
  176. .def("__iter__", def_iterator())
  177. ;
  178. DerivedPolicies::extension_def(cl);
  179. }
  180. template <class Class>
  181. static void
  182. extension_def(Class& cl)
  183. {
  184. // default.
  185. // no more extensions
  186. }
  187. private:
  188. static object
  189. base_get_item(back_reference<Container&> container, PyObject* i)
  190. {
  191. if (PySlice_Check(i))
  192. return slice_handler::base_get_slice(
  193. container.get(), static_cast<PySliceObject*>(static_cast<void*>(i)));
  194. return proxy_handler::base_get_item_(container, i);
  195. }
  196. static void
  197. base_set_item(Container& container, PyObject* i, PyObject* v)
  198. {
  199. if (PySlice_Check(i))
  200. {
  201. slice_handler::base_set_slice(container,
  202. static_cast<PySliceObject*>(static_cast<void*>(i)), v);
  203. }
  204. else
  205. {
  206. extract<Data&> elem(v);
  207. // try if elem is an exact Data
  208. if (elem.check())
  209. {
  210. DerivedPolicies::
  211. set_item(container,
  212. DerivedPolicies::
  213. convert_index(container, i), elem());
  214. }
  215. else
  216. {
  217. // try to convert elem to Data
  218. extract<Data> elem(v);
  219. if (elem.check())
  220. {
  221. DerivedPolicies::
  222. set_item(container,
  223. DerivedPolicies::
  224. convert_index(container, i), elem());
  225. }
  226. else
  227. {
  228. PyErr_SetString(PyExc_TypeError, "Invalid assignment");
  229. throw_error_already_set();
  230. }
  231. }
  232. }
  233. }
  234. static void
  235. base_delete_item(Container& container, PyObject* i)
  236. {
  237. if (PySlice_Check(i))
  238. {
  239. slice_handler::base_delete_slice(
  240. container, static_cast<PySliceObject*>(static_cast<void*>(i)));
  241. return;
  242. }
  243. Index index = DerivedPolicies::convert_index(container, i);
  244. proxy_handler::base_erase_index(container, index, mpl::bool_<NoSlice>());
  245. DerivedPolicies::delete_item(container, index);
  246. }
  247. static size_t
  248. base_size(Container& container)
  249. {
  250. return DerivedPolicies::size(container);
  251. }
  252. static bool
  253. base_contains(Container& container, PyObject* key)
  254. {
  255. extract<Key const&> x(key);
  256. // try if key is an exact Key type
  257. if (x.check())
  258. {
  259. return DerivedPolicies::contains(container, x());
  260. }
  261. else
  262. {
  263. // try to convert key to Key type
  264. extract<Key> x(key);
  265. if (x.check())
  266. return DerivedPolicies::contains(container, x());
  267. else
  268. return false;
  269. }
  270. }
  271. };
  272. }} // namespace boost::python
  273. #endif // INDEXING_SUITE_JDG20036_HPP