test_stl_binders.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. template <class NestMap> NestMap *times_hundred(int n) {
  46. auto m = new NestMap();
  47. for (int i = 1; i <= n; i++)
  48. for (int j = 1; j <= n; j++)
  49. (*m)[i].emplace(int(j*10), E_nc(100*j));
  50. return m;
  51. }
  52. TEST_SUBMODULE(stl_binders, m) {
  53. // test_vector_int
  54. py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
  55. // test_vector_custom
  56. py::class_<El>(m, "El")
  57. .def(py::init<int>());
  58. py::bind_vector<std::vector<El>>(m, "VectorEl");
  59. py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
  60. // test_map_string_double
  61. py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
  62. py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
  63. // test_map_string_double_const
  64. py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
  65. py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst");
  66. py::class_<E_nc>(m, "ENC")
  67. .def(py::init<int>())
  68. .def_readwrite("value", &E_nc::value);
  69. // test_noncopyable_containers
  70. py::bind_vector<std::vector<E_nc>>(m, "VectorENC");
  71. m.def("get_vnc", &one_to_n<std::vector<E_nc>>);
  72. py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
  73. m.def("get_dnc", &one_to_n<std::deque<E_nc>>);
  74. py::bind_map<std::map<int, E_nc>>(m, "MapENC");
  75. m.def("get_mnc", &times_ten<std::map<int, E_nc>>);
  76. py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
  77. m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>);
  78. // Issue #1885: binding nested std::map<X, Container<E>> with E non-copyable
  79. py::bind_map<std::map<int, std::vector<E_nc>>>(m, "MapVecENC");
  80. m.def("get_nvnc", [](int n)
  81. {
  82. auto m = new std::map<int, std::vector<E_nc>>();
  83. for (int i = 1; i <= n; i++)
  84. for (int j = 1; j <= n; j++)
  85. (*m)[i].emplace_back(j);
  86. return m;
  87. });
  88. py::bind_map<std::map<int, std::map<int, E_nc>>>(m, "MapMapENC");
  89. m.def("get_nmnc", &times_hundred<std::map<int, std::map<int, E_nc>>>);
  90. py::bind_map<std::unordered_map<int, std::unordered_map<int, E_nc>>>(m, "UmapUmapENC");
  91. m.def("get_numnc", &times_hundred<std::unordered_map<int, std::unordered_map<int, E_nc>>>);
  92. // test_vector_buffer
  93. py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol());
  94. // no dtype declared for this version:
  95. struct VUndeclStruct { bool w; uint32_t x; double y; bool z; };
  96. m.def("create_undeclstruct", [m] () mutable {
  97. py::bind_vector<std::vector<VUndeclStruct>>(m, "VectorUndeclStruct", py::buffer_protocol());
  98. });
  99. // The rest depends on numpy:
  100. try { py::module_::import("numpy"); }
  101. catch (...) { return; }
  102. // test_vector_buffer_numpy
  103. struct VStruct { bool w; uint32_t x; double y; bool z; };
  104. PYBIND11_NUMPY_DTYPE(VStruct, w, x, y, z);
  105. py::class_<VStruct>(m, "VStruct").def_readwrite("x", &VStruct::x);
  106. py::bind_vector<std::vector<VStruct>>(m, "VectorStruct", py::buffer_protocol());
  107. m.def("get_vectorstruct", [] {return std::vector<VStruct> {{0, 5, 3.0, 1}, {1, 30, -1e4, 0}};});
  108. }