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.

165 lines
5.7 KiB

3 months ago
  1. /*
  2. tests/test_stl.cpp -- STL type casters
  3. Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch>
  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.h>
  9. // Class that can be move- and copy-constructed, but not assigned
  10. struct NoAssign {
  11. int value;
  12. explicit NoAssign(int value = 0) : value(value) { }
  13. NoAssign(const NoAssign &) = default;
  14. NoAssign(NoAssign &&) = default;
  15. NoAssign &operator=(const NoAssign &) = delete;
  16. NoAssign &operator=(NoAssign &&) = delete;
  17. };
  18. /// Issue #528: templated constructor
  19. struct TplCtorClass {
  20. template <typename T> TplCtorClass(const T &) { }
  21. bool operator==(const TplCtorClass &) const { return true; }
  22. };
  23. namespace std {
  24. template <>
  25. struct hash<TplCtorClass> { size_t operator()(const TplCtorClass &) const { return 0; } };
  26. }
  27. TEST_SUBMODULE(stl, m) {
  28. // test_vector
  29. m.def("cast_vector", []() { return std::vector<int>{1}; });
  30. m.def("load_vector", [](const std::vector<int> &v) { return v.at(0) == 1 && v.at(1) == 2; });
  31. // test_array
  32. m.def("cast_array", []() { return std::array<int, 2> {{1 , 2}}; });
  33. m.def("load_array", [](const std::array<int, 2> &a) { return a[0] == 1 && a[1] == 2; });
  34. // test_valarray
  35. m.def("cast_valarray", []() { return std::valarray<int>{1, 4, 9}; });
  36. m.def("load_valarray", [](const std::valarray<int>& v) {
  37. return v.size() == 3 && v[0] == 1 && v[1] == 4 && v[2] == 9;
  38. });
  39. // test_map
  40. m.def("cast_map", []() { return std::map<std::string, std::string>{{"key", "value"}}; });
  41. m.def("load_map", [](const std::map<std::string, std::string> &map) {
  42. return map.at("key") == "value" && map.at("key2") == "value2";
  43. });
  44. // test_set
  45. m.def("cast_set", []() { return std::set<std::string>{"key1", "key2"}; });
  46. m.def("load_set", [](const std::set<std::string> &set) {
  47. return set.count("key1") && set.count("key2") && set.count("key3");
  48. });
  49. struct MoveOutContainer {
  50. struct Value { int value; };
  51. std::list<Value> move_list() const { return {{0}, {1}, {2}}; }
  52. };
  53. py::class_<MoveOutContainer::Value>(m, "MoveOutContainerValue")
  54. .def_readonly("value", &MoveOutContainer::Value::value);
  55. py::class_<MoveOutContainer>(m, "MoveOutContainer")
  56. .def(py::init<>())
  57. .def_property_readonly("move_list", &MoveOutContainer::move_list);
  58. py::class_<NoAssign>(m, "NoAssign", "Class with no C++ assignment operators")
  59. .def(py::init<>())
  60. .def(py::init<int>());
  61. #ifdef PYBIND11_HAS_OPTIONAL
  62. m.attr("has_optional") = true;
  63. using opt_int = std::optional<int>;
  64. using opt_no_assign = std::optional<NoAssign>;
  65. m.def("double_or_zero", [](const opt_int& x) -> int {
  66. return x.value_or(0) * 2;
  67. });
  68. m.def("half_or_none", [](int x) -> opt_int {
  69. return x ? opt_int(x / 2) : opt_int();
  70. });
  71. m.def("test_nullopt", [](opt_int x) {
  72. return x.value_or(42);
  73. }, py::arg_v("x", std::nullopt, "None"));
  74. m.def("test_no_assign", [](const opt_no_assign &x) {
  75. return x ? x->value : 42;
  76. }, py::arg_v("x", std::nullopt, "None"));
  77. m.def("nodefer_none_optional", [](std::optional<int>) { return true; });
  78. m.def("nodefer_none_optional", [](py::none) { return false; });
  79. #endif
  80. #ifdef PYBIND11_HAS_EXP_OPTIONAL
  81. m.attr("has_exp_optional") = true;
  82. using exp_opt_int = std::experimental::optional<int>;
  83. using exp_opt_no_assign = std::experimental::optional<NoAssign>;
  84. m.def("double_or_zero_exp", [](const exp_opt_int& x) -> int {
  85. return x.value_or(0) * 2;
  86. });
  87. m.def("half_or_none_exp", [](int x) -> exp_opt_int {
  88. return x ? exp_opt_int(x / 2) : exp_opt_int();
  89. });
  90. m.def("test_nullopt_exp", [](exp_opt_int x) {
  91. return x.value_or(42);
  92. }, py::arg_v("x", std::experimental::nullopt, "None"));
  93. m.def("test_no_assign_exp", [](const exp_opt_no_assign &x) {
  94. return x ? x->value : 42;
  95. }, py::arg_v("x", std::experimental::nullopt, "None"));
  96. #endif
  97. #ifdef PYBIND11_HAS_VARIANT
  98. struct visitor {
  99. const char *operator()(int) { return "int"; }
  100. const char *operator()(std::string) { return "std::string"; }
  101. const char *operator()(double) { return "double"; }
  102. const char *operator()(std::nullptr_t) { return "std::nullptr_t"; }
  103. };
  104. m.def("load_variant", [](std::variant<int, std::string, double, std::nullptr_t> v) {
  105. return std::visit(visitor(), v);
  106. });
  107. m.def("load_variant_2pass", [](std::variant<double, int> v) {
  108. return std::visit(visitor(), v);
  109. });
  110. m.def("cast_variant", []() {
  111. using V = std::variant<int, std::string>;
  112. return py::make_tuple(V(5), V("Hello"));
  113. });
  114. #endif
  115. /// #528: templated constructor
  116. m.def("tpl_ctor_vector", [](std::vector<TplCtorClass> &) {});
  117. m.def("tpl_ctor_map", [](std::unordered_map<TplCtorClass, TplCtorClass> &) {});
  118. m.def("tpl_ctor_set", [](std::unordered_set<TplCtorClass> &) {});
  119. #if defined(PYBIND11_HAS_OPTIONAL)
  120. m.def("tpl_constr_optional", [](std::optional<TplCtorClass> &) {});
  121. #elif defined(PYBIND11_HAS_EXP_OPTIONAL)
  122. m.def("tpl_constr_optional", [](std::experimental::optional<TplCtorClass> &) {});
  123. #endif
  124. // test_vec_of_reference_wrapper
  125. // #171: Can't return STL structures containing reference wrapper
  126. m.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<UserType> p4) {
  127. static UserType p1{1}, p2{2}, p3{3};
  128. return std::vector<std::reference_wrapper<UserType>> {
  129. std::ref(p1), std::ref(p2), std::ref(p3), p4
  130. };
  131. });
  132. // test_stl_pass_by_pointer
  133. m.def("stl_pass_by_pointer", [](std::vector<int>* v) { return *v; }, "v"_a=nullptr);
  134. }