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