stl_bind.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. pybind11/std_bind.h: Binding generators for STL data types
  3. Copyright (c) 2016 Sergey Lyskov and Wenzel Jakob
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #pragma once
  8. #include "detail/common.h"
  9. #include "operators.h"
  10. #include <algorithm>
  11. #include <sstream>
  12. NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
  13. NAMESPACE_BEGIN(detail)
  14. /* SFINAE helper class used by 'is_comparable */
  15. template <typename T> struct container_traits {
  16. template <typename T2> static std::true_type test_comparable(decltype(std::declval<const T2 &>() == std::declval<const T2 &>())*);
  17. template <typename T2> static std::false_type test_comparable(...);
  18. template <typename T2> static std::true_type test_value(typename T2::value_type *);
  19. template <typename T2> static std::false_type test_value(...);
  20. template <typename T2> static std::true_type test_pair(typename T2::first_type *, typename T2::second_type *);
  21. template <typename T2> static std::false_type test_pair(...);
  22. static constexpr const bool is_comparable = std::is_same<std::true_type, decltype(test_comparable<T>(nullptr))>::value;
  23. static constexpr const bool is_pair = std::is_same<std::true_type, decltype(test_pair<T>(nullptr, nullptr))>::value;
  24. static constexpr const bool is_vector = std::is_same<std::true_type, decltype(test_value<T>(nullptr))>::value;
  25. static constexpr const bool is_element = !is_pair && !is_vector;
  26. };
  27. /* Default: is_comparable -> std::false_type */
  28. template <typename T, typename SFINAE = void>
  29. struct is_comparable : std::false_type { };
  30. /* For non-map data structures, check whether operator== can be instantiated */
  31. template <typename T>
  32. struct is_comparable<
  33. T, enable_if_t<container_traits<T>::is_element &&
  34. container_traits<T>::is_comparable>>
  35. : std::true_type { };
  36. /* For a vector/map data structure, recursively check the value type (which is std::pair for maps) */
  37. template <typename T>
  38. struct is_comparable<T, enable_if_t<container_traits<T>::is_vector>> {
  39. static constexpr const bool value =
  40. is_comparable<typename T::value_type>::value;
  41. };
  42. /* For pairs, recursively check the two data types */
  43. template <typename T>
  44. struct is_comparable<T, enable_if_t<container_traits<T>::is_pair>> {
  45. static constexpr const bool value =
  46. is_comparable<typename T::first_type>::value &&
  47. is_comparable<typename T::second_type>::value;
  48. };
  49. /* Fallback functions */
  50. template <typename, typename, typename... Args> void vector_if_copy_constructible(const Args &...) { }
  51. template <typename, typename, typename... Args> void vector_if_equal_operator(const Args &...) { }
  52. template <typename, typename, typename... Args> void vector_if_insertion_operator(const Args &...) { }
  53. template <typename, typename, typename... Args> void vector_modifiers(const Args &...) { }
  54. template<typename Vector, typename Class_>
  55. void vector_if_copy_constructible(enable_if_t<is_copy_constructible<Vector>::value, Class_> &cl) {
  56. cl.def(init<const Vector &>(), "Copy constructor");
  57. }
  58. template<typename Vector, typename Class_>
  59. void vector_if_equal_operator(enable_if_t<is_comparable<Vector>::value, Class_> &cl) {
  60. using T = typename Vector::value_type;
  61. cl.def(self == self);
  62. cl.def(self != self);
  63. cl.def("count",
  64. [](const Vector &v, const T &x) {
  65. return std::count(v.begin(), v.end(), x);
  66. },
  67. arg("x"),
  68. "Return the number of times ``x`` appears in the list"
  69. );
  70. cl.def("remove", [](Vector &v, const T &x) {
  71. auto p = std::find(v.begin(), v.end(), x);
  72. if (p != v.end())
  73. v.erase(p);
  74. else
  75. throw value_error();
  76. },
  77. arg("x"),
  78. "Remove the first item from the list whose value is x. "
  79. "It is an error if there is no such item."
  80. );
  81. cl.def("__contains__",
  82. [](const Vector &v, const T &x) {
  83. return std::find(v.begin(), v.end(), x) != v.end();
  84. },
  85. arg("x"),
  86. "Return true the container contains ``x``"
  87. );
  88. }
  89. // Vector modifiers -- requires a copyable vector_type:
  90. // (Technically, some of these (pop and __delitem__) don't actually require copyability, but it seems
  91. // silly to allow deletion but not insertion, so include them here too.)
  92. template <typename Vector, typename Class_>
  93. void vector_modifiers(enable_if_t<is_copy_constructible<typename Vector::value_type>::value, Class_> &cl) {
  94. using T = typename Vector::value_type;
  95. using SizeType = typename Vector::size_type;
  96. using DiffType = typename Vector::difference_type;
  97. cl.def("append",
  98. [](Vector &v, const T &value) { v.push_back(value); },
  99. arg("x"),
  100. "Add an item to the end of the list");
  101. cl.def(init([](iterable it) {
  102. auto v = std::unique_ptr<Vector>(new Vector());
  103. v->reserve(len(it));
  104. for (handle h : it)
  105. v->push_back(h.cast<T>());
  106. return v.release();
  107. }));
  108. cl.def("extend",
  109. [](Vector &v, const Vector &src) {
  110. v.insert(v.end(), src.begin(), src.end());
  111. },
  112. arg("L"),
  113. "Extend the list by appending all the items in the given list"
  114. );
  115. cl.def("insert",
  116. [](Vector &v, SizeType i, const T &x) {
  117. if (i > v.size())
  118. throw index_error();
  119. v.insert(v.begin() + (DiffType) i, x);
  120. },
  121. arg("i") , arg("x"),
  122. "Insert an item at a given position."
  123. );
  124. cl.def("pop",
  125. [](Vector &v) {
  126. if (v.empty())
  127. throw index_error();
  128. T t = v.back();
  129. v.pop_back();
  130. return t;
  131. },
  132. "Remove and return the last item"
  133. );
  134. cl.def("pop",
  135. [](Vector &v, SizeType i) {
  136. if (i >= v.size())
  137. throw index_error();
  138. T t = v[i];
  139. v.erase(v.begin() + (DiffType) i);
  140. return t;
  141. },
  142. arg("i"),
  143. "Remove and return the item at index ``i``"
  144. );
  145. cl.def("__setitem__",
  146. [](Vector &v, SizeType i, const T &t) {
  147. if (i >= v.size())
  148. throw index_error();
  149. v[i] = t;
  150. }
  151. );
  152. /// Slicing protocol
  153. cl.def("__getitem__",
  154. [](const Vector &v, slice slice) -> Vector * {
  155. size_t start, stop, step, slicelength;
  156. if (!slice.compute(v.size(), &start, &stop, &step, &slicelength))
  157. throw error_already_set();
  158. Vector *seq = new Vector();
  159. seq->reserve((size_t) slicelength);
  160. for (size_t i=0; i<slicelength; ++i) {
  161. seq->push_back(v[start]);
  162. start += step;
  163. }
  164. return seq;
  165. },
  166. arg("s"),
  167. "Retrieve list elements using a slice object"
  168. );
  169. cl.def("__setitem__",
  170. [](Vector &v, slice slice, const Vector &value) {
  171. size_t start, stop, step, slicelength;
  172. if (!slice.compute(v.size(), &start, &stop, &step, &slicelength))
  173. throw error_already_set();
  174. if (slicelength != value.size())
  175. throw std::runtime_error("Left and right hand size of slice assignment have different sizes!");
  176. for (size_t i=0; i<slicelength; ++i) {
  177. v[start] = value[i];
  178. start += step;
  179. }
  180. },
  181. "Assign list elements using a slice object"
  182. );
  183. cl.def("__delitem__",
  184. [](Vector &v, SizeType i) {
  185. if (i >= v.size())
  186. throw index_error();
  187. v.erase(v.begin() + DiffType(i));
  188. },
  189. "Delete the list elements at index ``i``"
  190. );
  191. cl.def("__delitem__",
  192. [](Vector &v, slice slice) {
  193. size_t start, stop, step, slicelength;
  194. if (!slice.compute(v.size(), &start, &stop, &step, &slicelength))
  195. throw error_already_set();
  196. if (step == 1 && false) {
  197. v.erase(v.begin() + (DiffType) start, v.begin() + DiffType(start + slicelength));
  198. } else {
  199. for (size_t i = 0; i < slicelength; ++i) {
  200. v.erase(v.begin() + DiffType(start));
  201. start += step - 1;
  202. }
  203. }
  204. },
  205. "Delete list elements using a slice object"
  206. );
  207. }
  208. // If the type has an operator[] that doesn't return a reference (most notably std::vector<bool>),
  209. // we have to access by copying; otherwise we return by reference.
  210. template <typename Vector> using vector_needs_copy = negation<
  211. std::is_same<decltype(std::declval<Vector>()[typename Vector::size_type()]), typename Vector::value_type &>>;
  212. // The usual case: access and iterate by reference
  213. template <typename Vector, typename Class_>
  214. void vector_accessor(enable_if_t<!vector_needs_copy<Vector>::value, Class_> &cl) {
  215. using T = typename Vector::value_type;
  216. using SizeType = typename Vector::size_type;
  217. using ItType = typename Vector::iterator;
  218. cl.def("__getitem__",
  219. [](Vector &v, SizeType i) -> T & {
  220. if (i >= v.size())
  221. throw index_error();
  222. return v[i];
  223. },
  224. return_value_policy::reference_internal // ref + keepalive
  225. );
  226. cl.def("__iter__",
  227. [](Vector &v) {
  228. return make_iterator<
  229. return_value_policy::reference_internal, ItType, ItType, T&>(
  230. v.begin(), v.end());
  231. },
  232. keep_alive<0, 1>() /* Essential: keep list alive while iterator exists */
  233. );
  234. }
  235. // The case for special objects, like std::vector<bool>, that have to be returned-by-copy:
  236. template <typename Vector, typename Class_>
  237. void vector_accessor(enable_if_t<vector_needs_copy<Vector>::value, Class_> &cl) {
  238. using T = typename Vector::value_type;
  239. using SizeType = typename Vector::size_type;
  240. using ItType = typename Vector::iterator;
  241. cl.def("__getitem__",
  242. [](const Vector &v, SizeType i) -> T {
  243. if (i >= v.size())
  244. throw index_error();
  245. return v[i];
  246. }
  247. );
  248. cl.def("__iter__",
  249. [](Vector &v) {
  250. return make_iterator<
  251. return_value_policy::copy, ItType, ItType, T>(
  252. v.begin(), v.end());
  253. },
  254. keep_alive<0, 1>() /* Essential: keep list alive while iterator exists */
  255. );
  256. }
  257. template <typename Vector, typename Class_> auto vector_if_insertion_operator(Class_ &cl, std::string const &name)
  258. -> decltype(std::declval<std::ostream&>() << std::declval<typename Vector::value_type>(), void()) {
  259. using size_type = typename Vector::size_type;
  260. cl.def("__repr__",
  261. [name](Vector &v) {
  262. std::ostringstream s;
  263. s << name << '[';
  264. for (size_type i=0; i < v.size(); ++i) {
  265. s << v[i];
  266. if (i != v.size() - 1)
  267. s << ", ";
  268. }
  269. s << ']';
  270. return s.str();
  271. },
  272. "Return the canonical string representation of this list."
  273. );
  274. }
  275. // Provide the buffer interface for vectors if we have data() and we have a format for it
  276. // GCC seems to have "void std::vector<bool>::data()" - doing SFINAE on the existence of data() is insufficient, we need to check it returns an appropriate pointer
  277. template <typename Vector, typename = void>
  278. struct vector_has_data_and_format : std::false_type {};
  279. template <typename Vector>
  280. struct vector_has_data_and_format<Vector, enable_if_t<std::is_same<decltype(format_descriptor<typename Vector::value_type>::format(), std::declval<Vector>().data()), typename Vector::value_type*>::value>> : std::true_type {};
  281. // Add the buffer interface to a vector
  282. template <typename Vector, typename Class_, typename... Args>
  283. enable_if_t<detail::any_of<std::is_same<Args, buffer_protocol>...>::value>
  284. vector_buffer(Class_& cl) {
  285. using T = typename Vector::value_type;
  286. static_assert(vector_has_data_and_format<Vector>::value, "There is not an appropriate format descriptor for this vector");
  287. // numpy.h declares this for arbitrary types, but it may raise an exception and crash hard at runtime if PYBIND11_NUMPY_DTYPE hasn't been called, so check here
  288. format_descriptor<T>::format();
  289. cl.def_buffer([](Vector& v) -> buffer_info {
  290. return buffer_info(v.data(), static_cast<ssize_t>(sizeof(T)), format_descriptor<T>::format(), 1, {v.size()}, {sizeof(T)});
  291. });
  292. cl.def(init([](buffer buf) {
  293. auto info = buf.request();
  294. if (info.ndim != 1 || info.strides[0] % static_cast<ssize_t>(sizeof(T)))
  295. throw type_error("Only valid 1D buffers can be copied to a vector");
  296. if (!detail::compare_buffer_info<T>::compare(info) || (ssize_t) sizeof(T) != info.itemsize)
  297. throw type_error("Format mismatch (Python: " + info.format + " C++: " + format_descriptor<T>::format() + ")");
  298. auto vec = std::unique_ptr<Vector>(new Vector());
  299. vec->reserve((size_t) info.shape[0]);
  300. T *p = static_cast<T*>(info.ptr);
  301. ssize_t step = info.strides[0] / static_cast<ssize_t>(sizeof(T));
  302. T *end = p + info.shape[0] * step;
  303. for (; p != end; p += step)
  304. vec->push_back(*p);
  305. return vec.release();
  306. }));
  307. return;
  308. }
  309. template <typename Vector, typename Class_, typename... Args>
  310. enable_if_t<!detail::any_of<std::is_same<Args, buffer_protocol>...>::value> vector_buffer(Class_&) {}
  311. NAMESPACE_END(detail)
  312. //
  313. // std::vector
  314. //
  315. template <typename Vector, typename holder_type = std::unique_ptr<Vector>, typename... Args>
  316. class_<Vector, holder_type> bind_vector(handle scope, std::string const &name, Args&&... args) {
  317. using Class_ = class_<Vector, holder_type>;
  318. // If the value_type is unregistered (e.g. a converting type) or is itself registered
  319. // module-local then make the vector binding module-local as well:
  320. using vtype = typename Vector::value_type;
  321. auto vtype_info = detail::get_type_info(typeid(vtype));
  322. bool local = !vtype_info || vtype_info->module_local;
  323. Class_ cl(scope, name.c_str(), pybind11::module_local(local), std::forward<Args>(args)...);
  324. // Declare the buffer interface if a buffer_protocol() is passed in
  325. detail::vector_buffer<Vector, Class_, Args...>(cl);
  326. cl.def(init<>());
  327. // Register copy constructor (if possible)
  328. detail::vector_if_copy_constructible<Vector, Class_>(cl);
  329. // Register comparison-related operators and functions (if possible)
  330. detail::vector_if_equal_operator<Vector, Class_>(cl);
  331. // Register stream insertion operator (if possible)
  332. detail::vector_if_insertion_operator<Vector, Class_>(cl, name);
  333. // Modifiers require copyable vector value type
  334. detail::vector_modifiers<Vector, Class_>(cl);
  335. // Accessor and iterator; return by value if copyable, otherwise we return by ref + keep-alive
  336. detail::vector_accessor<Vector, Class_>(cl);
  337. cl.def("__bool__",
  338. [](const Vector &v) -> bool {
  339. return !v.empty();
  340. },
  341. "Check whether the list is nonempty"
  342. );
  343. cl.def("__len__", &Vector::size);
  344. #if 0
  345. // C++ style functions deprecated, leaving it here as an example
  346. cl.def(init<size_type>());
  347. cl.def("resize",
  348. (void (Vector::*) (size_type count)) & Vector::resize,
  349. "changes the number of elements stored");
  350. cl.def("erase",
  351. [](Vector &v, SizeType i) {
  352. if (i >= v.size())
  353. throw index_error();
  354. v.erase(v.begin() + i);
  355. }, "erases element at index ``i``");
  356. cl.def("empty", &Vector::empty, "checks whether the container is empty");
  357. cl.def("size", &Vector::size, "returns the number of elements");
  358. cl.def("push_back", (void (Vector::*)(const T&)) &Vector::push_back, "adds an element to the end");
  359. cl.def("pop_back", &Vector::pop_back, "removes the last element");
  360. cl.def("max_size", &Vector::max_size, "returns the maximum possible number of elements");
  361. cl.def("reserve", &Vector::reserve, "reserves storage");
  362. cl.def("capacity", &Vector::capacity, "returns the number of elements that can be held in currently allocated storage");
  363. cl.def("shrink_to_fit", &Vector::shrink_to_fit, "reduces memory usage by freeing unused memory");
  364. cl.def("clear", &Vector::clear, "clears the contents");
  365. cl.def("swap", &Vector::swap, "swaps the contents");
  366. cl.def("front", [](Vector &v) {
  367. if (v.size()) return v.front();
  368. else throw index_error();
  369. }, "access the first element");
  370. cl.def("back", [](Vector &v) {
  371. if (v.size()) return v.back();
  372. else throw index_error();
  373. }, "access the last element ");
  374. #endif
  375. return cl;
  376. }
  377. //
  378. // std::map, std::unordered_map
  379. //
  380. NAMESPACE_BEGIN(detail)
  381. /* Fallback functions */
  382. template <typename, typename, typename... Args> void map_if_insertion_operator(const Args &...) { }
  383. template <typename, typename, typename... Args> void map_assignment(const Args &...) { }
  384. // Map assignment when copy-assignable: just copy the value
  385. template <typename Map, typename Class_>
  386. void map_assignment(enable_if_t<std::is_copy_assignable<typename Map::mapped_type>::value, Class_> &cl) {
  387. using KeyType = typename Map::key_type;
  388. using MappedType = typename Map::mapped_type;
  389. cl.def("__setitem__",
  390. [](Map &m, const KeyType &k, const MappedType &v) {
  391. auto it = m.find(k);
  392. if (it != m.end()) it->second = v;
  393. else m.emplace(k, v);
  394. }
  395. );
  396. }
  397. // Not copy-assignable, but still copy-constructible: we can update the value by erasing and reinserting
  398. template<typename Map, typename Class_>
  399. void map_assignment(enable_if_t<
  400. !std::is_copy_assignable<typename Map::mapped_type>::value &&
  401. is_copy_constructible<typename Map::mapped_type>::value,
  402. Class_> &cl) {
  403. using KeyType = typename Map::key_type;
  404. using MappedType = typename Map::mapped_type;
  405. cl.def("__setitem__",
  406. [](Map &m, const KeyType &k, const MappedType &v) {
  407. // We can't use m[k] = v; because value type might not be default constructable
  408. auto r = m.emplace(k, v);
  409. if (!r.second) {
  410. // value type is not copy assignable so the only way to insert it is to erase it first...
  411. m.erase(r.first);
  412. m.emplace(k, v);
  413. }
  414. }
  415. );
  416. }
  417. template <typename Map, typename Class_> auto map_if_insertion_operator(Class_ &cl, std::string const &name)
  418. -> decltype(std::declval<std::ostream&>() << std::declval<typename Map::key_type>() << std::declval<typename Map::mapped_type>(), void()) {
  419. cl.def("__repr__",
  420. [name](Map &m) {
  421. std::ostringstream s;
  422. s << name << '{';
  423. bool f = false;
  424. for (auto const &kv : m) {
  425. if (f)
  426. s << ", ";
  427. s << kv.first << ": " << kv.second;
  428. f = true;
  429. }
  430. s << '}';
  431. return s.str();
  432. },
  433. "Return the canonical string representation of this map."
  434. );
  435. }
  436. NAMESPACE_END(detail)
  437. template <typename Map, typename holder_type = std::unique_ptr<Map>, typename... Args>
  438. class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args&&... args) {
  439. using KeyType = typename Map::key_type;
  440. using MappedType = typename Map::mapped_type;
  441. using Class_ = class_<Map, holder_type>;
  442. // If either type is a non-module-local bound type then make the map binding non-local as well;
  443. // otherwise (e.g. both types are either module-local or converting) the map will be
  444. // module-local.
  445. auto tinfo = detail::get_type_info(typeid(MappedType));
  446. bool local = !tinfo || tinfo->module_local;
  447. if (local) {
  448. tinfo = detail::get_type_info(typeid(KeyType));
  449. local = !tinfo || tinfo->module_local;
  450. }
  451. Class_ cl(scope, name.c_str(), pybind11::module_local(local), std::forward<Args>(args)...);
  452. cl.def(init<>());
  453. // Register stream insertion operator (if possible)
  454. detail::map_if_insertion_operator<Map, Class_>(cl, name);
  455. cl.def("__bool__",
  456. [](const Map &m) -> bool { return !m.empty(); },
  457. "Check whether the map is nonempty"
  458. );
  459. cl.def("__iter__",
  460. [](Map &m) { return make_key_iterator(m.begin(), m.end()); },
  461. keep_alive<0, 1>() /* Essential: keep list alive while iterator exists */
  462. );
  463. cl.def("items",
  464. [](Map &m) { return make_iterator(m.begin(), m.end()); },
  465. keep_alive<0, 1>() /* Essential: keep list alive while iterator exists */
  466. );
  467. cl.def("__getitem__",
  468. [](Map &m, const KeyType &k) -> MappedType & {
  469. auto it = m.find(k);
  470. if (it == m.end())
  471. throw key_error();
  472. return it->second;
  473. },
  474. return_value_policy::reference_internal // ref + keepalive
  475. );
  476. // Assignment provided only if the type is copyable
  477. detail::map_assignment<Map, Class_>(cl);
  478. cl.def("__delitem__",
  479. [](Map &m, const KeyType &k) {
  480. auto it = m.find(k);
  481. if (it == m.end())
  482. throw key_error();
  483. m.erase(it);
  484. }
  485. );
  486. cl.def("__len__", &Map::size);
  487. return cl;
  488. }
  489. NAMESPACE_END(PYBIND11_NAMESPACE)