blob: 8688874091219f5a5035f5eb46e976e7408080b8 [file] [log] [blame]
Sergey Lyskoveae77442016-05-07 00:26:19 -04001/*
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002 tests/test_stl_binders.cpp -- Usage of stl_binders functions
Sergey Lyskoveae77442016-05-07 00:26:19 -04003
Wenzel Jakob25c03ce2016-05-15 20:50:38 +02004 Copyright (c) 2016 Sergey Lyskov
Sergey Lyskoveae77442016-05-07 00:26:19 -04005
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
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020010#include "pybind11_tests.h"
Sergey Lyskoveae77442016-05-07 00:26:19 -040011
Wenzel Jakob25c03ce2016-05-15 20:50:38 +020012#include <pybind11/stl_bind.h>
Patrick Stewart54679792016-11-08 13:03:34 +000013#include <pybind11/numpy.h>
Wenzel Jakobfe342412016-09-06 13:02:29 +090014#include <map>
Jason Rhinelander617fbcf2016-11-15 06:30:38 -050015#include <deque>
Wenzel Jakobfe342412016-09-06 13:02:29 +090016#include <unordered_map>
Sergey Lyskoveae77442016-05-07 00:26:19 -040017
Wenzel Jakob25c03ce2016-05-15 20:50:38 +020018class El {
Sergey Lyskoveae77442016-05-07 00:26:19 -040019public:
Jason Rhinelander540ae612016-08-28 13:46:25 -040020 El() = delete;
21 El(int v) : a(v) { }
Sergey Lyskova95bde12016-05-08 19:31:55 -040022
Jason Rhinelander540ae612016-08-28 13:46:25 -040023 int a;
Sergey Lyskoveae77442016-05-07 00:26:19 -040024};
25
Wenzel Jakob25c03ce2016-05-15 20:50:38 +020026std::ostream & operator<<(std::ostream &s, El const&v) {
Jason Rhinelander540ae612016-08-28 13:46:25 -040027 s << "El{" << v.a << '}';
28 return s;
Sergey Lyskova95bde12016-05-08 19:31:55 -040029}
Sergey Lyskoveae77442016-05-07 00:26:19 -040030
Jason Rhinelander617fbcf2016-11-15 06:30:38 -050031/// Issue #487: binding std::vector<E> with E non-copyable
32class E_nc {
33public:
34 explicit E_nc(int i) : value{i} {}
35 E_nc(const E_nc &) = delete;
36 E_nc &operator=(const E_nc &) = delete;
37 E_nc(E_nc &&) = default;
38 E_nc &operator=(E_nc &&) = default;
39
40 int value;
41};
42
43template <class Container> Container *one_to_n(int n) {
44 auto v = new Container();
45 for (int i = 1; i <= n; i++)
46 v->emplace_back(i);
47 return v;
48}
49
50template <class Map> Map *times_ten(int n) {
51 auto m = new Map();
52 for (int i = 1; i <= n; i++)
53 m->emplace(int(i), E_nc(10*i));
54 return m;
55}
56
Sebastian Gsängera83d69e2019-10-31 12:38:24 +010057template <class NestMap> NestMap *times_hundred(int n) {
58 auto m = new NestMap();
59 for (int i = 1; i <= n; i++)
60 for (int j = 1; j <= n; j++)
61 (*m)[i].emplace(int(j*10), E_nc(100*j));
62 return m;
63}
64
Jason Rhinelander391c7542017-07-25 16:47:36 -040065TEST_SUBMODULE(stl_binders, m) {
Jason Rhinelander391c7542017-07-25 16:47:36 -040066 // test_vector_int
Patrick Stewart54679792016-11-08 13:03:34 +000067 py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
Jason Rhinelander391c7542017-07-25 16:47:36 -040068
Jason Rhinelander391c7542017-07-25 16:47:36 -040069 // test_vector_custom
70 py::class_<El>(m, "El")
71 .def(py::init<int>());
Wenzel Jakobfe342412016-09-06 13:02:29 +090072 py::bind_vector<std::vector<El>>(m, "VectorEl");
Wenzel Jakobfe342412016-09-06 13:02:29 +090073 py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
Jason Rhinelander617fbcf2016-11-15 06:30:38 -050074
Jason Rhinelander391c7542017-07-25 16:47:36 -040075 // test_map_string_double
Wenzel Jakobfe342412016-09-06 13:02:29 +090076 py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
77 py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
Sergey Lyskov75204182016-08-29 22:50:38 -040078
Jason Rhinelander391c7542017-07-25 16:47:36 -040079 // test_map_string_double_const
Wenzel Jakobfe342412016-09-06 13:02:29 +090080 py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
81 py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst");
Jason Rhinelander617fbcf2016-11-15 06:30:38 -050082
Jason Rhinelander617fbcf2016-11-15 06:30:38 -050083 py::class_<E_nc>(m, "ENC")
84 .def(py::init<int>())
85 .def_readwrite("value", &E_nc::value);
86
Jason Rhinelander391c7542017-07-25 16:47:36 -040087 // test_noncopyable_containers
Jason Rhinelander617fbcf2016-11-15 06:30:38 -050088 py::bind_vector<std::vector<E_nc>>(m, "VectorENC");
89 m.def("get_vnc", &one_to_n<std::vector<E_nc>>, py::return_value_policy::reference);
Jason Rhinelander617fbcf2016-11-15 06:30:38 -050090 py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
91 m.def("get_dnc", &one_to_n<std::deque<E_nc>>, py::return_value_policy::reference);
Jason Rhinelander617fbcf2016-11-15 06:30:38 -050092 py::bind_map<std::map<int, E_nc>>(m, "MapENC");
93 m.def("get_mnc", &times_ten<std::map<int, E_nc>>, py::return_value_policy::reference);
Jason Rhinelander617fbcf2016-11-15 06:30:38 -050094 py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
95 m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference);
Sebastian Gsängera83d69e2019-10-31 12:38:24 +010096 // Issue #1885: binding nested std::map<X, Container<E>> with E non-copyable
97 py::bind_map<std::map<int, std::vector<E_nc>>>(m, "MapVecENC");
98 m.def("get_nvnc", [](int n)
99 {
100 auto m = new std::map<int, std::vector<E_nc>>();
101 for (int i = 1; i <= n; i++)
102 for (int j = 1; j <= n; j++)
103 (*m)[i].emplace_back(j);
104 return m;
105 }, py::return_value_policy::reference);
106 py::bind_map<std::map<int, std::map<int, E_nc>>>(m, "MapMapENC");
107 m.def("get_nmnc", &times_hundred<std::map<int, std::map<int, E_nc>>>, py::return_value_policy::reference);
108 py::bind_map<std::unordered_map<int, std::unordered_map<int, E_nc>>>(m, "UmapUmapENC");
109 m.def("get_numnc", &times_hundred<std::unordered_map<int, std::unordered_map<int, E_nc>>>, py::return_value_policy::reference);
Jason Rhinelander391c7542017-07-25 16:47:36 -0400110
111 // test_vector_buffer
112 py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol());
113 // no dtype declared for this version:
114 struct VUndeclStruct { bool w; uint32_t x; double y; bool z; };
115 m.def("create_undeclstruct", [m] () mutable {
116 py::bind_vector<std::vector<VUndeclStruct>>(m, "VectorUndeclStruct", py::buffer_protocol());
117 });
118
119 // The rest depends on numpy:
120 try { py::module::import("numpy"); }
121 catch (...) { return; }
122
123 // test_vector_buffer_numpy
124 struct VStruct { bool w; uint32_t x; double y; bool z; };
125 PYBIND11_NUMPY_DTYPE(VStruct, w, x, y, z);
126 py::class_<VStruct>(m, "VStruct").def_readwrite("x", &VStruct::x);
127 py::bind_vector<std::vector<VStruct>>(m, "VectorStruct", py::buffer_protocol());
128 m.def("get_vectorstruct", [] {return std::vector<VStruct> {{0, 5, 3.0, 1}, {1, 30, -1e4, 0}};});
129}