test_stl.cpp
Go to the documentation of this file.
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"
11 #include "constructor_stats.h"
12 #include <pybind11/stl.h>
13 
14 #include <vector>
15 #include <string>
16 
17 // Test with `std::variant` in C++17 mode, or with `boost::variant` in C++11/14
18 #if defined(PYBIND11_HAS_VARIANT)
19 using std::variant;
20 #elif defined(PYBIND11_TEST_BOOST) && (!defined(_MSC_VER) || _MSC_VER >= 1910)
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
38 
39 PYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>);
40 
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 } // namespace std
51 
52 
53 template <template <typename> class OptionalImpl, typename T>
55 {
56  OptionalHolder() = default;
57  bool member_initialized() const {
58  return member && member->initialized;
59  }
60  OptionalImpl<T> member = T{};
61 };
62 
63 
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; });
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  });
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; });
76 
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 
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 
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  });
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 
142  // test_move_out_container
143  struct MoveOutContainer {
144  struct Value { int value; };
145  std::list<Value> move_list() const { return {{0}, {1}, {2}}; }
146  };
147  py::class_<MoveOutContainer::Value>(m, "MoveOutContainerValue")
148  .def_readonly("value", &MoveOutContainer::Value::value);
149  py::class_<MoveOutContainer>(m, "MoveOutContainer")
150  .def(py::init<>())
151  .def_property_readonly("move_list", &MoveOutContainer::move_list);
152 
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  };
164  py::class_<NoAssign>(m, "NoAssign", "Class with no C++ assignment operators")
165  .def(py::init<>())
166  .def(py::init<int>());
167 
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 
185 #ifdef PYBIND11_HAS_OPTIONAL
186  // test_optional
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; });
206 
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);
212 #endif
213 
214 #ifdef PYBIND11_HAS_EXP_OPTIONAL
215  // test_exp_optional
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"));
232 
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);
238 #endif
239 
240 #ifdef PYBIND11_HAS_VARIANT
242  "visitor::result_type is required by boost::variant in C++11 mode");
243 
244  struct visitor {
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"; }
251  };
252 
253  // test_variant
254  m.def("load_variant", [](variant<int, std::string, double, std::nullptr_t> v) {
255  return py::detail::visit_helper<variant>::call(visitor(), v);
256  });
257  m.def("load_variant_2pass", [](variant<double, int> v) {
258  return py::detail::visit_helper<variant>::call(visitor(), v);
259  });
260  m.def("cast_variant", []() {
261  using V = variant<int, std::string>;
262  return py::make_tuple(V(5), V("Hello"));
263  });
264 #endif
265 
266  // #528: templated constructor
267  // (no python tests: the test here is that this compiles)
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 
286  // test_stl_pass_by_pointer
287  m.def("stl_pass_by_pointer", [](std::vector<int>* v) { return *v; }, "v"_a=nullptr);
288 
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 
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 
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);
310 
311  m.def("array_cast_sequence", [](std::array<int, 3> x) { return x; });
312 
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);
324 }
Matrix3f m
ssize_t hash(handle obj)
Definition: pytypes.h:457
TplCtorClass(const T &)
Definition: test_stl.cpp:43
static Point3 p3
Vector3f p1
KeyInt p4(x4, 4)
ArrayXcf v
Definition: Cwise_arg.cpp:1
Definition: pytypes.h:1322
void print_destroyed(T *inst, Values &&...values)
bool member_initialized() const
Definition: test_stl.cpp:57
PYBIND11_MAKE_OPAQUE(std::vector< std::string, std::allocator< std::string >>)
Definition: Half.h:150
TEST_SUBMODULE(stl, m)
Definition: test_stl.cpp:64
Array33i a
Tuple< Args... > make_tuple(Args...args)
Creates a tuple object, deducing the target type from the types of arguments.
Values result
int data[]
Generic variant caster.
Definition: stl.h:334
void print_created(T *inst, Values &&...values)
static Point3 p2
Issue #528: templated constructor.
Definition: test_stl.cpp:42
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
bool operator==(const TplCtorClass &) const
Definition: test_stl.cpp:44
size_t operator()(const TplCtorClass &) const
Definition: test_stl.cpp:49


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:04