You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.7 KiB

3 months ago
  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. #ifdef _MSC_VER
  14. // We get some really long type names here which causes MSVC to emit warnings
  15. # pragma warning(disable: 4503) // warning C4503: decorated name length exceeded, name was truncated
  16. #endif
  17. class El {
  18. public:
  19. El() = delete;
  20. El(int v) : a(v) { }
  21. int a;
  22. };
  23. std::ostream & operator<<(std::ostream &s, El const&v) {
  24. s << "El{" << v.a << '}';
  25. return s;
  26. }
  27. /// Issue #487: binding std::vector<E> with E non-copyable
  28. class E_nc {
  29. public:
  30. explicit E_nc(int i) : value{i} {}
  31. E_nc(const E_nc &) = delete;
  32. E_nc &operator=(const E_nc &) = delete;
  33. E_nc(E_nc &&) = default;
  34. E_nc &operator=(E_nc &&) = default;
  35. int value;
  36. };
  37. template <class Container> Container *one_to_n(int n) {
  38. auto v = new Container();
  39. for (int i = 1; i <= n; i++)
  40. v->emplace_back(i);
  41. return v;
  42. }
  43. template <class Map> Map *times_ten(int n) {
  44. auto m = new Map();
  45. for (int i = 1; i <= n; i++)
  46. m->emplace(int(i), E_nc(10*i));
  47. return m;
  48. }
  49. struct VStruct {
  50. bool w;
  51. uint32_t x;
  52. double y;
  53. bool z;
  54. };
  55. struct VUndeclStruct { //dtype not declared for this version
  56. bool w;
  57. uint32_t x;
  58. double y;
  59. bool z;
  60. };
  61. test_initializer stl_binder_vector([](py::module &m) {
  62. py::class_<El>(m, "El")
  63. .def(py::init<int>());
  64. py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol());
  65. py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
  66. py::bind_vector<std::vector<bool>>(m, "VectorBool");
  67. py::bind_vector<std::vector<El>>(m, "VectorEl");
  68. py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
  69. m.def("create_undeclstruct", [m] () mutable {
  70. py::bind_vector<std::vector<VUndeclStruct>>(m, "VectorUndeclStruct", py::buffer_protocol());
  71. });
  72. try {
  73. py::module::import("numpy");
  74. } catch (...) {
  75. return;
  76. }
  77. PYBIND11_NUMPY_DTYPE(VStruct, w, x, y, z);
  78. py::class_<VStruct>(m, "VStruct").def_readwrite("x", &VStruct::x);
  79. py::bind_vector<std::vector<VStruct>>(m, "VectorStruct", py::buffer_protocol());
  80. m.def("get_vectorstruct", [] {return std::vector<VStruct> {{0, 5, 3.0, 1}, {1, 30, -1e4, 0}};});
  81. });
  82. test_initializer stl_binder_map([](py::module &m) {
  83. py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
  84. py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
  85. py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
  86. py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst");
  87. });
  88. test_initializer stl_binder_noncopyable([](py::module &m) {
  89. py::class_<E_nc>(m, "ENC")
  90. .def(py::init<int>())
  91. .def_readwrite("value", &E_nc::value);
  92. py::bind_vector<std::vector<E_nc>>(m, "VectorENC");
  93. m.def("get_vnc", &one_to_n<std::vector<E_nc>>, py::return_value_policy::reference);
  94. py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
  95. m.def("get_dnc", &one_to_n<std::deque<E_nc>>, py::return_value_policy::reference);
  96. py::bind_map<std::map<int, E_nc>>(m, "MapENC");
  97. m.def("get_mnc", &times_ten<std::map<int, E_nc>>, py::return_value_policy::reference);
  98. py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
  99. m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference);
  100. });