test_stl_binders.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. tests/test_stl_binders.cpp -- Usage of stl_binders functions
  3. Copyright (c) 2016 Sergey Lyskov
  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. #include "pybind11_tests.h"
  8. #include <pybind11/stl_bind.h>
  9. #include <pybind11/numpy.h>
  10. #include <map>
  11. #include <deque>
  12. #include <unordered_map>
  13. class El {
  14. public:
  15. El() = delete;
  16. El(int v) : a(v) { }
  17. int a;
  18. };
  19. std::ostream & operator<<(std::ostream &s, El const&v) {
  20. s << "El{" << v.a << '}';
  21. return s;
  22. }
  23. /// Issue #487: binding std::vector<E> with E non-copyable
  24. class E_nc {
  25. public:
  26. explicit E_nc(int i) : value{i} {}
  27. E_nc(const E_nc &) = delete;
  28. E_nc &operator=(const E_nc &) = delete;
  29. E_nc(E_nc &&) = default;
  30. E_nc &operator=(E_nc &&) = default;
  31. int value;
  32. };
  33. template <class Container> Container *one_to_n(int n) {
  34. auto v = new Container();
  35. for (int i = 1; i <= n; i++)
  36. v->emplace_back(i);
  37. return v;
  38. }
  39. template <class Map> Map *times_ten(int n) {
  40. auto m = new Map();
  41. for (int i = 1; i <= n; i++)
  42. m->emplace(int(i), E_nc(10*i));
  43. return m;
  44. }
  45. TEST_SUBMODULE(stl_binders, m) {
  46. // test_vector_int
  47. py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
  48. // test_vector_custom
  49. py::class_<El>(m, "El")
  50. .def(py::init<int>());
  51. py::bind_vector<std::vector<El>>(m, "VectorEl");
  52. py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
  53. // test_map_string_double
  54. py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
  55. py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
  56. // test_map_string_double_const
  57. py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
  58. py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst");
  59. py::class_<E_nc>(m, "ENC")
  60. .def(py::init<int>())
  61. .def_readwrite("value", &E_nc::value);
  62. // test_noncopyable_containers
  63. py::bind_vector<std::vector<E_nc>>(m, "VectorENC");
  64. m.def("get_vnc", &one_to_n<std::vector<E_nc>>, py::return_value_policy::reference);
  65. py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
  66. m.def("get_dnc", &one_to_n<std::deque<E_nc>>, py::return_value_policy::reference);
  67. py::bind_map<std::map<int, E_nc>>(m, "MapENC");
  68. m.def("get_mnc", &times_ten<std::map<int, E_nc>>, py::return_value_policy::reference);
  69. py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
  70. m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference);
  71. // test_vector_buffer
  72. py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol());
  73. // no dtype declared for this version:
  74. struct VUndeclStruct { bool w; uint32_t x; double y; bool z; };
  75. m.def("create_undeclstruct", [m] () mutable {
  76. py::bind_vector<std::vector<VUndeclStruct>>(m, "VectorUndeclStruct", py::buffer_protocol());
  77. });
  78. // The rest depends on numpy:
  79. try { py::module::import("numpy"); }
  80. catch (...) { return; }
  81. // test_vector_buffer_numpy
  82. struct VStruct { bool w; uint32_t x; double y; bool z; };
  83. PYBIND11_NUMPY_DTYPE(VStruct, w, x, y, z);
  84. py::class_<VStruct>(m, "VStruct").def_readwrite("x", &VStruct::x);
  85. py::bind_vector<std::vector<VStruct>>(m, "VectorStruct", py::buffer_protocol());
  86. m.def("get_vectorstruct", [] {return std::vector<VStruct> {{0, 5, 3.0, 1}, {1, 30, -1e4, 0}};});
  87. }