Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | tests/test_stl.cpp -- STL type casters |
| 3 | |
| 4 | Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch> |
| 5 | |
| 6 | All rights reserved. Use of this source code is governed by a |
| 7 | BSD-style license that can be found in the LICENSE file. |
| 8 | */ |
| 9 | |
| 10 | #include "pybind11_tests.h" |
Wenzel Jakob | cbd16a8 | 2018-07-17 16:56:26 +0200 | [diff] [blame] | 11 | #include "constructor_stats.h" |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 12 | #include <pybind11/stl.h> |
| 13 | |
Allan Leal | e76dff7 | 2018-10-11 10:28:12 +0200 | [diff] [blame] | 14 | #include <vector> |
| 15 | #include <string> |
| 16 | |
Dean Moldovan | 7918bcc | 2017-08-08 16:02:31 +0200 | [diff] [blame] | 17 | // Test with `std::variant` in C++17 mode, or with `boost::variant` in C++11/14 |
| 18 | #if PYBIND11_HAS_VARIANT |
| 19 | using std::variant; |
Dean Moldovan | c40ef61 | 2017-08-25 21:11:36 +0200 | [diff] [blame] | 20 | #elif defined(PYBIND11_TEST_BOOST) && (!defined(_MSC_VER) || _MSC_VER >= 1910) |
Dean Moldovan | 7918bcc | 2017-08-08 16:02:31 +0200 | [diff] [blame] | 21 | # include <boost/variant.hpp> |
| 22 | # define PYBIND11_HAS_VARIANT 1 |
| 23 | using boost::variant; |
| 24 | |
| 25 | namespace pybind11 { namespace detail { |
| 26 | template <typename... Ts> |
| 27 | struct type_caster<boost::variant<Ts...>> : variant_caster<boost::variant<Ts...>> {}; |
| 28 | |
| 29 | template <> |
| 30 | struct visit_helper<boost::variant> { |
| 31 | template <typename... Args> |
| 32 | static auto call(Args &&...args) -> decltype(boost::apply_visitor(args...)) { |
| 33 | return boost::apply_visitor(args...); |
| 34 | } |
| 35 | }; |
| 36 | }} // namespace pybind11::detail |
| 37 | #endif |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 38 | |
Allan Leal | e76dff7 | 2018-10-11 10:28:12 +0200 | [diff] [blame] | 39 | PYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>); |
| 40 | |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 41 | /// Issue #528: templated constructor |
| 42 | struct TplCtorClass { |
| 43 | template <typename T> TplCtorClass(const T &) { } |
| 44 | bool operator==(const TplCtorClass &) const { return true; } |
| 45 | }; |
| 46 | |
| 47 | namespace std { |
| 48 | template <> |
| 49 | struct hash<TplCtorClass> { size_t operator()(const TplCtorClass &) const { return 0; } }; |
| 50 | } |
| 51 | |
| 52 | |
fatvlady | 8c06b95 | 2020-03-14 16:10:14 +0200 | [diff] [blame] | 53 | template <template <typename> class OptionalImpl, typename T> |
fatvlady | a3daf87 | 2020-03-14 15:15:12 +0200 | [diff] [blame] | 54 | struct OptionalHolder |
| 55 | { |
| 56 | OptionalHolder() = default; |
| 57 | bool member_initialized() const { |
| 58 | return member && member->initialized; |
| 59 | } |
| 60 | OptionalImpl<T> member = T{}; |
| 61 | }; |
| 62 | |
| 63 | |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 64 | TEST_SUBMODULE(stl, m) { |
| 65 | // test_vector |
| 66 | m.def("cast_vector", []() { return std::vector<int>{1}; }); |
| 67 | m.def("load_vector", [](const std::vector<int> &v) { return v.at(0) == 1 && v.at(1) == 2; }); |
Dean Moldovan | 3c4933c | 2017-09-01 21:42:20 +0200 | [diff] [blame] | 68 | // `std::vector<bool>` is special because it returns proxy objects instead of references |
| 69 | m.def("cast_bool_vector", []() { return std::vector<bool>{true, false}; }); |
| 70 | m.def("load_bool_vector", [](const std::vector<bool> &v) { |
| 71 | return v.at(0) == true && v.at(1) == false; |
| 72 | }); |
Jason Rhinelander | 67a0cc4 | 2017-07-06 20:41:52 -0400 | [diff] [blame] | 73 | // Unnumbered regression (caused by #936): pointers to stl containers aren't castable |
| 74 | static std::vector<RValueCaster> lvv{2}; |
| 75 | m.def("cast_ptr_vector", []() { return &lvv; }); |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 76 | |
voxmea | 17983e7 | 2018-11-16 00:45:19 -0500 | [diff] [blame] | 77 | // test_deque |
| 78 | m.def("cast_deque", []() { return std::deque<int>{1}; }); |
| 79 | m.def("load_deque", [](const std::deque<int> &v) { return v.at(0) == 1 && v.at(1) == 2; }); |
| 80 | |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 81 | // test_array |
| 82 | m.def("cast_array", []() { return std::array<int, 2> {{1 , 2}}; }); |
| 83 | m.def("load_array", [](const std::array<int, 2> &a) { return a[0] == 1 && a[1] == 2; }); |
| 84 | |
| 85 | // test_valarray |
| 86 | m.def("cast_valarray", []() { return std::valarray<int>{1, 4, 9}; }); |
| 87 | m.def("load_valarray", [](const std::valarray<int>& v) { |
| 88 | return v.size() == 3 && v[0] == 1 && v[1] == 4 && v[2] == 9; |
| 89 | }); |
| 90 | |
| 91 | // test_map |
| 92 | m.def("cast_map", []() { return std::map<std::string, std::string>{{"key", "value"}}; }); |
| 93 | m.def("load_map", [](const std::map<std::string, std::string> &map) { |
| 94 | return map.at("key") == "value" && map.at("key2") == "value2"; |
| 95 | }); |
| 96 | |
| 97 | // test_set |
| 98 | m.def("cast_set", []() { return std::set<std::string>{"key1", "key2"}; }); |
| 99 | m.def("load_set", [](const std::set<std::string> &set) { |
| 100 | return set.count("key1") && set.count("key2") && set.count("key3"); |
| 101 | }); |
| 102 | |
Jason Rhinelander | b57281b | 2017-07-03 19:12:09 -0400 | [diff] [blame] | 103 | // test_recursive_casting |
| 104 | m.def("cast_rv_vector", []() { return std::vector<RValueCaster>{2}; }); |
| 105 | m.def("cast_rv_array", []() { return std::array<RValueCaster, 3>(); }); |
| 106 | // NB: map and set keys are `const`, so while we technically do move them (as `const Type &&`), |
| 107 | // casters don't typically do anything with that, which means they fall to the `const Type &` |
| 108 | // caster. |
| 109 | m.def("cast_rv_map", []() { return std::unordered_map<std::string, RValueCaster>{{"a", RValueCaster{}}}; }); |
| 110 | m.def("cast_rv_nested", []() { |
| 111 | std::vector<std::array<std::list<std::unordered_map<std::string, RValueCaster>>, 2>> v; |
| 112 | v.emplace_back(); // add an array |
| 113 | v.back()[0].emplace_back(); // add a map to the array |
| 114 | v.back()[0].back().emplace("b", RValueCaster{}); |
| 115 | v.back()[0].back().emplace("c", RValueCaster{}); |
| 116 | v.back()[1].emplace_back(); // add a map to the array |
| 117 | v.back()[1].back().emplace("a", RValueCaster{}); |
| 118 | return v; |
| 119 | }); |
Jason Rhinelander | b57281b | 2017-07-03 19:12:09 -0400 | [diff] [blame] | 120 | static std::array<RValueCaster, 2> lva; |
| 121 | static std::unordered_map<std::string, RValueCaster> lvm{{"a", RValueCaster{}}, {"b", RValueCaster{}}}; |
| 122 | static std::unordered_map<std::string, std::vector<std::list<std::array<RValueCaster, 2>>>> lvn; |
| 123 | lvn["a"].emplace_back(); // add a list |
| 124 | lvn["a"].back().emplace_back(); // add an array |
| 125 | lvn["a"].emplace_back(); // another list |
| 126 | lvn["a"].back().emplace_back(); // add an array |
| 127 | lvn["b"].emplace_back(); // add a list |
| 128 | lvn["b"].back().emplace_back(); // add an array |
| 129 | lvn["b"].back().emplace_back(); // add another array |
| 130 | m.def("cast_lv_vector", []() -> const decltype(lvv) & { return lvv; }); |
| 131 | m.def("cast_lv_array", []() -> const decltype(lva) & { return lva; }); |
| 132 | m.def("cast_lv_map", []() -> const decltype(lvm) & { return lvm; }); |
| 133 | m.def("cast_lv_nested", []() -> const decltype(lvn) & { return lvn; }); |
| 134 | // #853: |
| 135 | m.def("cast_unique_ptr_vector", []() { |
| 136 | std::vector<std::unique_ptr<UserType>> v; |
| 137 | v.emplace_back(new UserType{7}); |
| 138 | v.emplace_back(new UserType{42}); |
| 139 | return v; |
| 140 | }); |
| 141 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 142 | // test_move_out_container |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 143 | struct MoveOutContainer { |
| 144 | struct Value { int value; }; |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 145 | std::list<Value> move_list() const { return {{0}, {1}, {2}}; } |
| 146 | }; |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 147 | py::class_<MoveOutContainer::Value>(m, "MoveOutContainerValue") |
| 148 | .def_readonly("value", &MoveOutContainer::Value::value); |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 149 | py::class_<MoveOutContainer>(m, "MoveOutContainer") |
| 150 | .def(py::init<>()) |
| 151 | .def_property_readonly("move_list", &MoveOutContainer::move_list); |
| 152 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 153 | // Class that can be move- and copy-constructed, but not assigned |
| 154 | struct NoAssign { |
| 155 | int value; |
| 156 | |
| 157 | explicit NoAssign(int value = 0) : value(value) { } |
| 158 | NoAssign(const NoAssign &) = default; |
| 159 | NoAssign(NoAssign &&) = default; |
| 160 | |
| 161 | NoAssign &operator=(const NoAssign &) = delete; |
| 162 | NoAssign &operator=(NoAssign &&) = delete; |
| 163 | }; |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 164 | py::class_<NoAssign>(m, "NoAssign", "Class with no C++ assignment operators") |
| 165 | .def(py::init<>()) |
| 166 | .def(py::init<int>()); |
| 167 | |
fatvlady | a3daf87 | 2020-03-14 15:15:12 +0200 | [diff] [blame] | 168 | |
| 169 | struct MoveOutDetector |
| 170 | { |
| 171 | MoveOutDetector() = default; |
| 172 | MoveOutDetector(const MoveOutDetector&) = default; |
| 173 | MoveOutDetector(MoveOutDetector&& other) noexcept |
| 174 | : initialized(other.initialized) { |
| 175 | // steal underlying resource |
| 176 | other.initialized = false; |
| 177 | } |
| 178 | bool initialized = true; |
| 179 | }; |
| 180 | py::class_<MoveOutDetector>(m, "MoveOutDetector", "Class with move tracking") |
| 181 | .def(py::init<>()) |
| 182 | .def_readonly("initialized", &MoveOutDetector::initialized); |
| 183 | |
| 184 | |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 185 | #ifdef PYBIND11_HAS_OPTIONAL |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 186 | // test_optional |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 187 | m.attr("has_optional") = true; |
| 188 | |
| 189 | using opt_int = std::optional<int>; |
| 190 | using opt_no_assign = std::optional<NoAssign>; |
| 191 | m.def("double_or_zero", [](const opt_int& x) -> int { |
| 192 | return x.value_or(0) * 2; |
| 193 | }); |
| 194 | m.def("half_or_none", [](int x) -> opt_int { |
| 195 | return x ? opt_int(x / 2) : opt_int(); |
| 196 | }); |
| 197 | m.def("test_nullopt", [](opt_int x) { |
| 198 | return x.value_or(42); |
| 199 | }, py::arg_v("x", std::nullopt, "None")); |
| 200 | m.def("test_no_assign", [](const opt_no_assign &x) { |
| 201 | return x ? x->value : 42; |
| 202 | }, py::arg_v("x", std::nullopt, "None")); |
| 203 | |
| 204 | m.def("nodefer_none_optional", [](std::optional<int>) { return true; }); |
| 205 | m.def("nodefer_none_optional", [](py::none) { return false; }); |
fatvlady | a3daf87 | 2020-03-14 15:15:12 +0200 | [diff] [blame] | 206 | |
| 207 | using opt_holder = OptionalHolder<std::optional, MoveOutDetector>; |
| 208 | py::class_<opt_holder>(m, "OptionalHolder", "Class with optional member") |
| 209 | .def(py::init<>()) |
| 210 | .def_readonly("member", &opt_holder::member) |
| 211 | .def("member_initialized", &opt_holder::member_initialized); |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 212 | #endif |
| 213 | |
| 214 | #ifdef PYBIND11_HAS_EXP_OPTIONAL |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 215 | // test_exp_optional |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 216 | m.attr("has_exp_optional") = true; |
| 217 | |
| 218 | using exp_opt_int = std::experimental::optional<int>; |
| 219 | using exp_opt_no_assign = std::experimental::optional<NoAssign>; |
| 220 | m.def("double_or_zero_exp", [](const exp_opt_int& x) -> int { |
| 221 | return x.value_or(0) * 2; |
| 222 | }); |
| 223 | m.def("half_or_none_exp", [](int x) -> exp_opt_int { |
| 224 | return x ? exp_opt_int(x / 2) : exp_opt_int(); |
| 225 | }); |
| 226 | m.def("test_nullopt_exp", [](exp_opt_int x) { |
| 227 | return x.value_or(42); |
| 228 | }, py::arg_v("x", std::experimental::nullopt, "None")); |
| 229 | m.def("test_no_assign_exp", [](const exp_opt_no_assign &x) { |
| 230 | return x ? x->value : 42; |
| 231 | }, py::arg_v("x", std::experimental::nullopt, "None")); |
fatvlady | a3daf87 | 2020-03-14 15:15:12 +0200 | [diff] [blame] | 232 | |
| 233 | using opt_exp_holder = OptionalHolder<std::experimental::optional, MoveOutDetector>; |
| 234 | py::class_<opt_exp_holder>(m, "OptionalExpHolder", "Class with optional member") |
| 235 | .def(py::init<>()) |
| 236 | .def_readonly("member", &opt_exp_holder::member) |
| 237 | .def("member_initialized", &opt_exp_holder::member_initialized); |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 238 | #endif |
| 239 | |
| 240 | #ifdef PYBIND11_HAS_VARIANT |
Dean Moldovan | 7918bcc | 2017-08-08 16:02:31 +0200 | [diff] [blame] | 241 | static_assert(std::is_same<py::detail::variant_caster_visitor::result_type, py::handle>::value, |
| 242 | "visitor::result_type is required by boost::variant in C++11 mode"); |
| 243 | |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 244 | struct visitor { |
Dean Moldovan | 7918bcc | 2017-08-08 16:02:31 +0200 | [diff] [blame] | 245 | using result_type = const char *; |
| 246 | |
| 247 | result_type operator()(int) { return "int"; } |
| 248 | result_type operator()(std::string) { return "std::string"; } |
| 249 | result_type operator()(double) { return "double"; } |
| 250 | result_type operator()(std::nullptr_t) { return "std::nullptr_t"; } |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 251 | }; |
| 252 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 253 | // test_variant |
Dean Moldovan | 7918bcc | 2017-08-08 16:02:31 +0200 | [diff] [blame] | 254 | m.def("load_variant", [](variant<int, std::string, double, std::nullptr_t> v) { |
| 255 | return py::detail::visit_helper<variant>::call(visitor(), v); |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 256 | }); |
Dean Moldovan | 7918bcc | 2017-08-08 16:02:31 +0200 | [diff] [blame] | 257 | m.def("load_variant_2pass", [](variant<double, int> v) { |
| 258 | return py::detail::visit_helper<variant>::call(visitor(), v); |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 259 | }); |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 260 | m.def("cast_variant", []() { |
Dean Moldovan | 7918bcc | 2017-08-08 16:02:31 +0200 | [diff] [blame] | 261 | using V = variant<int, std::string>; |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 262 | return py::make_tuple(V(5), V("Hello")); |
| 263 | }); |
| 264 | #endif |
| 265 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 266 | // #528: templated constructor |
| 267 | // (no python tests: the test here is that this compiles) |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 268 | m.def("tpl_ctor_vector", [](std::vector<TplCtorClass> &) {}); |
| 269 | m.def("tpl_ctor_map", [](std::unordered_map<TplCtorClass, TplCtorClass> &) {}); |
| 270 | m.def("tpl_ctor_set", [](std::unordered_set<TplCtorClass> &) {}); |
| 271 | #if defined(PYBIND11_HAS_OPTIONAL) |
| 272 | m.def("tpl_constr_optional", [](std::optional<TplCtorClass> &) {}); |
| 273 | #elif defined(PYBIND11_HAS_EXP_OPTIONAL) |
| 274 | m.def("tpl_constr_optional", [](std::experimental::optional<TplCtorClass> &) {}); |
| 275 | #endif |
| 276 | |
| 277 | // test_vec_of_reference_wrapper |
| 278 | // #171: Can't return STL structures containing reference wrapper |
| 279 | m.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<UserType> p4) { |
| 280 | static UserType p1{1}, p2{2}, p3{3}; |
| 281 | return std::vector<std::reference_wrapper<UserType>> { |
| 282 | std::ref(p1), std::ref(p2), std::ref(p3), p4 |
| 283 | }; |
| 284 | }); |
| 285 | |
Andreas Bergmeier | 34b7b54 | 2017-05-09 15:01:22 +0200 | [diff] [blame] | 286 | // test_stl_pass_by_pointer |
| 287 | m.def("stl_pass_by_pointer", [](std::vector<int>* v) { return *v; }, "v"_a=nullptr); |
Wenzel Jakob | cbd16a8 | 2018-07-17 16:56:26 +0200 | [diff] [blame] | 288 | |
Allan Leal | e76dff7 | 2018-10-11 10:28:12 +0200 | [diff] [blame] | 289 | // #1258: pybind11/stl.h converts string to vector<string> |
| 290 | m.def("func_with_string_or_vector_string_arg_overload", [](std::vector<std::string>) { return 1; }); |
| 291 | m.def("func_with_string_or_vector_string_arg_overload", [](std::list<std::string>) { return 2; }); |
| 292 | m.def("func_with_string_or_vector_string_arg_overload", [](std::string) { return 3; }); |
| 293 | |
Wenzel Jakob | cbd16a8 | 2018-07-17 16:56:26 +0200 | [diff] [blame] | 294 | class Placeholder { |
| 295 | public: |
| 296 | Placeholder() { print_created(this); } |
| 297 | Placeholder(const Placeholder &) = delete; |
| 298 | ~Placeholder() { print_destroyed(this); } |
| 299 | }; |
| 300 | py::class_<Placeholder>(m, "Placeholder"); |
| 301 | |
| 302 | /// test_stl_vector_ownership |
| 303 | m.def("test_stl_ownership", |
| 304 | []() { |
| 305 | std::vector<Placeholder *> result; |
| 306 | result.push_back(new Placeholder()); |
| 307 | return result; |
| 308 | }, |
| 309 | py::return_value_policy::take_ownership); |
Wenzel Jakob | 9f73060 | 2018-11-09 12:32:48 +0100 | [diff] [blame] | 310 | |
| 311 | m.def("array_cast_sequence", [](std::array<int, 3> x) { return x; }); |
Wenzel Jakob | adc2cdd | 2018-11-09 20:12:46 +0100 | [diff] [blame] | 312 | |
| 313 | /// test_issue_1561 |
| 314 | struct Issue1561Inner { std::string data; }; |
| 315 | struct Issue1561Outer { std::vector<Issue1561Inner> list; }; |
| 316 | |
| 317 | py::class_<Issue1561Inner>(m, "Issue1561Inner") |
| 318 | .def(py::init<std::string>()) |
| 319 | .def_readwrite("data", &Issue1561Inner::data); |
| 320 | |
| 321 | py::class_<Issue1561Outer>(m, "Issue1561Outer") |
| 322 | .def(py::init<>()) |
| 323 | .def_readwrite("list", &Issue1561Outer::list); |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 324 | } |