blob: 9762fb9a739fe168d86bb85b723f2c97488cdb50 [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
64 struct MoveOutContainer {
65 struct Value { int value; };
66
67 std::list<Value> move_list() const { return {{0}, {1}, {2}}; }
68 };
69
70 py::class_<MoveOutContainer::Value>(m, "MoveOutContainerValue")
71 .def_readonly("value", &MoveOutContainer::Value::value);
72
73 py::class_<MoveOutContainer>(m, "MoveOutContainer")
74 .def(py::init<>())
75 .def_property_readonly("move_list", &MoveOutContainer::move_list);
76
77 py::class_<NoAssign>(m, "NoAssign", "Class with no C++ assignment operators")
78 .def(py::init<>())
79 .def(py::init<int>());
80
81#ifdef PYBIND11_HAS_OPTIONAL
82 m.attr("has_optional") = true;
83
84 using opt_int = std::optional<int>;
85 using opt_no_assign = std::optional<NoAssign>;
86 m.def("double_or_zero", [](const opt_int& x) -> int {
87 return x.value_or(0) * 2;
88 });
89 m.def("half_or_none", [](int x) -> opt_int {
90 return x ? opt_int(x / 2) : opt_int();
91 });
92 m.def("test_nullopt", [](opt_int x) {
93 return x.value_or(42);
94 }, py::arg_v("x", std::nullopt, "None"));
95 m.def("test_no_assign", [](const opt_no_assign &x) {
96 return x ? x->value : 42;
97 }, py::arg_v("x", std::nullopt, "None"));
98
99 m.def("nodefer_none_optional", [](std::optional<int>) { return true; });
100 m.def("nodefer_none_optional", [](py::none) { return false; });
101#endif
102
103#ifdef PYBIND11_HAS_EXP_OPTIONAL
104 m.attr("has_exp_optional") = true;
105
106 using exp_opt_int = std::experimental::optional<int>;
107 using exp_opt_no_assign = std::experimental::optional<NoAssign>;
108 m.def("double_or_zero_exp", [](const exp_opt_int& x) -> int {
109 return x.value_or(0) * 2;
110 });
111 m.def("half_or_none_exp", [](int x) -> exp_opt_int {
112 return x ? exp_opt_int(x / 2) : exp_opt_int();
113 });
114 m.def("test_nullopt_exp", [](exp_opt_int x) {
115 return x.value_or(42);
116 }, py::arg_v("x", std::experimental::nullopt, "None"));
117 m.def("test_no_assign_exp", [](const exp_opt_no_assign &x) {
118 return x ? x->value : 42;
119 }, py::arg_v("x", std::experimental::nullopt, "None"));
120#endif
121
122#ifdef PYBIND11_HAS_VARIANT
123 struct visitor {
124 const char *operator()(int) { return "int"; }
125 const char *operator()(std::string) { return "std::string"; }
126 const char *operator()(double) { return "double"; }
127 const char *operator()(std::nullptr_t) { return "std::nullptr_t"; }
128 };
129
130 m.def("load_variant", [](std::variant<int, std::string, double, std::nullptr_t> v) {
131 return std::visit(visitor(), v);
132 });
133
134 m.def("load_variant_2pass", [](std::variant<double, int> v) {
135 return std::visit(visitor(), v);
136 });
137
138 m.def("cast_variant", []() {
139 using V = std::variant<int, std::string>;
140 return py::make_tuple(V(5), V("Hello"));
141 });
142#endif
143
144 /// #528: templated constructor
145 m.def("tpl_ctor_vector", [](std::vector<TplCtorClass> &) {});
146 m.def("tpl_ctor_map", [](std::unordered_map<TplCtorClass, TplCtorClass> &) {});
147 m.def("tpl_ctor_set", [](std::unordered_set<TplCtorClass> &) {});
148#if defined(PYBIND11_HAS_OPTIONAL)
149 m.def("tpl_constr_optional", [](std::optional<TplCtorClass> &) {});
150#elif defined(PYBIND11_HAS_EXP_OPTIONAL)
151 m.def("tpl_constr_optional", [](std::experimental::optional<TplCtorClass> &) {});
152#endif
153
154 // test_vec_of_reference_wrapper
155 // #171: Can't return STL structures containing reference wrapper
156 m.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<UserType> p4) {
157 static UserType p1{1}, p2{2}, p3{3};
158 return std::vector<std::reference_wrapper<UserType>> {
159 std::ref(p1), std::ref(p2), std::ref(p3), p4
160 };
161 });
162
Andreas Bergmeier34b7b542017-05-09 15:01:22 +0200163 // test_stl_pass_by_pointer
164 m.def("stl_pass_by_pointer", [](std::vector<int>* v) { return *v; }, "v"_a=nullptr);
Dean Moldovan83e328f2017-06-09 00:44:49 +0200165}