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.

95 lines
2.6 KiB

  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 <map>
  10. #include <deque>
  11. #include <unordered_map>
  12. class El {
  13. public:
  14. El() = delete;
  15. El(int v) : a(v) { }
  16. int a;
  17. };
  18. std::ostream & operator<<(std::ostream &s, El const&v) {
  19. s << "El{" << v.a << '}';
  20. return s;
  21. }
  22. /// Issue #487: binding std::vector<E> with E non-copyable
  23. class E_nc {
  24. public:
  25. explicit E_nc(int i) : value{i} {}
  26. E_nc(const E_nc &) = delete;
  27. E_nc &operator=(const E_nc &) = delete;
  28. E_nc(E_nc &&) = default;
  29. E_nc &operator=(E_nc &&) = default;
  30. int value;
  31. };
  32. template <class Container> Container *one_to_n(int n) {
  33. auto v = new Container();
  34. for (int i = 1; i <= n; i++)
  35. v->emplace_back(i);
  36. return v;
  37. }
  38. template <class Map> Map *times_ten(int n) {
  39. auto m = new Map();
  40. for (int i = 1; i <= n; i++)
  41. m->emplace(int(i), E_nc(10*i));
  42. return m;
  43. }
  44. test_initializer stl_binder_vector([](py::module &m) {
  45. py::class_<El>(m, "El")
  46. .def(py::init<int>());
  47. py::bind_vector<std::vector<unsigned int>>(m, "VectorInt");
  48. py::bind_vector<std::vector<bool>>(m, "VectorBool");
  49. py::bind_vector<std::vector<El>>(m, "VectorEl");
  50. py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
  51. });
  52. test_initializer stl_binder_map([](py::module &m) {
  53. py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
  54. py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
  55. py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
  56. py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst");
  57. });
  58. test_initializer stl_binder_noncopyable([](py::module &m) {
  59. py::class_<E_nc>(m, "ENC")
  60. .def(py::init<int>())
  61. .def_readwrite("value", &E_nc::value);
  62. py::bind_vector<std::vector<E_nc>>(m, "VectorENC");
  63. m.def("get_vnc", &one_to_n<std::vector<E_nc>>, py::return_value_policy::reference);
  64. py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
  65. m.def("get_dnc", &one_to_n<std::deque<E_nc>>, py::return_value_policy::reference);
  66. py::bind_map<std::map<int, E_nc>>(m, "MapENC");
  67. m.def("get_mnc", &times_ten<std::map<int, E_nc>>, py::return_value_policy::reference);
  68. py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
  69. m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference);
  70. });