blob: 6ba9cb9451176fdbd4e807713a9b0745772d9503 [file] [log] [blame]
Dean Moldovan83e328f2017-06-09 00:44:49 +02001/*
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 <pybind11/stl.h>
12
13// Class that can be move- and copy-constructed, but not assigned
14struct NoAssign {
15 int value;
16
17 explicit NoAssign(int value = 0) : value(value) { }
18 NoAssign(const NoAssign &) = default;
19 NoAssign(NoAssign &&) = default;
20
21 NoAssign &operator=(const NoAssign &) = delete;
22 NoAssign &operator=(NoAssign &&) = delete;
23};
24
25/// Issue #528: templated constructor
26struct TplCtorClass {
27 template <typename T> TplCtorClass(const T &) { }
28 bool operator==(const TplCtorClass &) const { return true; }
29};
30
31namespace std {
32 template <>
33 struct hash<TplCtorClass> { size_t operator()(const TplCtorClass &) const { return 0; } };
34}
35
36
37TEST_SUBMODULE(stl, m) {
38 // test_vector
39 m.def("cast_vector", []() { return std::vector<int>{1}; });
40 m.def("load_vector", [](const std::vector<int> &v) { return v.at(0) == 1 && v.at(1) == 2; });
Jason Rhinelander67a0cc42017-07-06 20:41:52 -040041 // Unnumbered regression (caused by #936): pointers to stl containers aren't castable
42 static std::vector<RValueCaster> lvv{2};
43 m.def("cast_ptr_vector", []() { return &lvv; });
Dean Moldovan83e328f2017-06-09 00:44:49 +020044
45 // test_array
46 m.def("cast_array", []() { return std::array<int, 2> {{1 , 2}}; });
47 m.def("load_array", [](const std::array<int, 2> &a) { return a[0] == 1 && a[1] == 2; });
48
49 // test_valarray
50 m.def("cast_valarray", []() { return std::valarray<int>{1, 4, 9}; });
51 m.def("load_valarray", [](const std::valarray<int>& v) {
52 return v.size() == 3 && v[0] == 1 && v[1] == 4 && v[2] == 9;
53 });
54
55 // test_map
56 m.def("cast_map", []() { return std::map<std::string, std::string>{{"key", "value"}}; });
57 m.def("load_map", [](const std::map<std::string, std::string> &map) {
58 return map.at("key") == "value" && map.at("key2") == "value2";
59 });
60
61 // test_set
62 m.def("cast_set", []() { return std::set<std::string>{"key1", "key2"}; });
63 m.def("load_set", [](const std::set<std::string> &set) {
64 return set.count("key1") && set.count("key2") && set.count("key3");
65 });
66
Jason Rhinelanderb57281b2017-07-03 19:12:09 -040067 // test_recursive_casting
68 m.def("cast_rv_vector", []() { return std::vector<RValueCaster>{2}; });
69 m.def("cast_rv_array", []() { return std::array<RValueCaster, 3>(); });
70 // NB: map and set keys are `const`, so while we technically do move them (as `const Type &&`),
71 // casters don't typically do anything with that, which means they fall to the `const Type &`
72 // caster.
73 m.def("cast_rv_map", []() { return std::unordered_map<std::string, RValueCaster>{{"a", RValueCaster{}}}; });
74 m.def("cast_rv_nested", []() {
75 std::vector<std::array<std::list<std::unordered_map<std::string, RValueCaster>>, 2>> v;
76 v.emplace_back(); // add an array
77 v.back()[0].emplace_back(); // add a map to the array
78 v.back()[0].back().emplace("b", RValueCaster{});
79 v.back()[0].back().emplace("c", RValueCaster{});
80 v.back()[1].emplace_back(); // add a map to the array
81 v.back()[1].back().emplace("a", RValueCaster{});
82 return v;
83 });
Jason Rhinelanderb57281b2017-07-03 19:12:09 -040084 static std::array<RValueCaster, 2> lva;
85 static std::unordered_map<std::string, RValueCaster> lvm{{"a", RValueCaster{}}, {"b", RValueCaster{}}};
86 static std::unordered_map<std::string, std::vector<std::list<std::array<RValueCaster, 2>>>> lvn;
87 lvn["a"].emplace_back(); // add a list
88 lvn["a"].back().emplace_back(); // add an array
89 lvn["a"].emplace_back(); // another list
90 lvn["a"].back().emplace_back(); // add an array
91 lvn["b"].emplace_back(); // add a list
92 lvn["b"].back().emplace_back(); // add an array
93 lvn["b"].back().emplace_back(); // add another array
94 m.def("cast_lv_vector", []() -> const decltype(lvv) & { return lvv; });
95 m.def("cast_lv_array", []() -> const decltype(lva) & { return lva; });
96 m.def("cast_lv_map", []() -> const decltype(lvm) & { return lvm; });
97 m.def("cast_lv_nested", []() -> const decltype(lvn) & { return lvn; });
98 // #853:
99 m.def("cast_unique_ptr_vector", []() {
100 std::vector<std::unique_ptr<UserType>> v;
101 v.emplace_back(new UserType{7});
102 v.emplace_back(new UserType{42});
103 return v;
104 });
105
Dean Moldovan83e328f2017-06-09 00:44:49 +0200106 struct MoveOutContainer {
107 struct Value { int value; };
108
109 std::list<Value> move_list() const { return {{0}, {1}, {2}}; }
110 };
111
112 py::class_<MoveOutContainer::Value>(m, "MoveOutContainerValue")
113 .def_readonly("value", &MoveOutContainer::Value::value);
114
115 py::class_<MoveOutContainer>(m, "MoveOutContainer")
116 .def(py::init<>())
117 .def_property_readonly("move_list", &MoveOutContainer::move_list);
118
119 py::class_<NoAssign>(m, "NoAssign", "Class with no C++ assignment operators")
120 .def(py::init<>())
121 .def(py::init<int>());
122
123#ifdef PYBIND11_HAS_OPTIONAL
124 m.attr("has_optional") = true;
125
126 using opt_int = std::optional<int>;
127 using opt_no_assign = std::optional<NoAssign>;
128 m.def("double_or_zero", [](const opt_int& x) -> int {
129 return x.value_or(0) * 2;
130 });
131 m.def("half_or_none", [](int x) -> opt_int {
132 return x ? opt_int(x / 2) : opt_int();
133 });
134 m.def("test_nullopt", [](opt_int x) {
135 return x.value_or(42);
136 }, py::arg_v("x", std::nullopt, "None"));
137 m.def("test_no_assign", [](const opt_no_assign &x) {
138 return x ? x->value : 42;
139 }, py::arg_v("x", std::nullopt, "None"));
140
141 m.def("nodefer_none_optional", [](std::optional<int>) { return true; });
142 m.def("nodefer_none_optional", [](py::none) { return false; });
143#endif
144
145#ifdef PYBIND11_HAS_EXP_OPTIONAL
146 m.attr("has_exp_optional") = true;
147
148 using exp_opt_int = std::experimental::optional<int>;
149 using exp_opt_no_assign = std::experimental::optional<NoAssign>;
150 m.def("double_or_zero_exp", [](const exp_opt_int& x) -> int {
151 return x.value_or(0) * 2;
152 });
153 m.def("half_or_none_exp", [](int x) -> exp_opt_int {
154 return x ? exp_opt_int(x / 2) : exp_opt_int();
155 });
156 m.def("test_nullopt_exp", [](exp_opt_int x) {
157 return x.value_or(42);
158 }, py::arg_v("x", std::experimental::nullopt, "None"));
159 m.def("test_no_assign_exp", [](const exp_opt_no_assign &x) {
160 return x ? x->value : 42;
161 }, py::arg_v("x", std::experimental::nullopt, "None"));
162#endif
163
164#ifdef PYBIND11_HAS_VARIANT
165 struct visitor {
166 const char *operator()(int) { return "int"; }
167 const char *operator()(std::string) { return "std::string"; }
168 const char *operator()(double) { return "double"; }
169 const char *operator()(std::nullptr_t) { return "std::nullptr_t"; }
170 };
171
172 m.def("load_variant", [](std::variant<int, std::string, double, std::nullptr_t> v) {
173 return std::visit(visitor(), v);
174 });
175
176 m.def("load_variant_2pass", [](std::variant<double, int> v) {
177 return std::visit(visitor(), v);
178 });
179
180 m.def("cast_variant", []() {
181 using V = std::variant<int, std::string>;
182 return py::make_tuple(V(5), V("Hello"));
183 });
184#endif
185
186 /// #528: templated constructor
187 m.def("tpl_ctor_vector", [](std::vector<TplCtorClass> &) {});
188 m.def("tpl_ctor_map", [](std::unordered_map<TplCtorClass, TplCtorClass> &) {});
189 m.def("tpl_ctor_set", [](std::unordered_set<TplCtorClass> &) {});
190#if defined(PYBIND11_HAS_OPTIONAL)
191 m.def("tpl_constr_optional", [](std::optional<TplCtorClass> &) {});
192#elif defined(PYBIND11_HAS_EXP_OPTIONAL)
193 m.def("tpl_constr_optional", [](std::experimental::optional<TplCtorClass> &) {});
194#endif
195
196 // test_vec_of_reference_wrapper
197 // #171: Can't return STL structures containing reference wrapper
198 m.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<UserType> p4) {
199 static UserType p1{1}, p2{2}, p3{3};
200 return std::vector<std::reference_wrapper<UserType>> {
201 std::ref(p1), std::ref(p2), std::ref(p3), p4
202 };
203 });
204
Andreas Bergmeier34b7b542017-05-09 15:01:22 +0200205 // test_stl_pass_by_pointer
206 m.def("stl_pass_by_pointer", [](std::vector<int>* v) { return *v; }, "v"_a=nullptr);
Dean Moldovan83e328f2017-06-09 00:44:49 +0200207}