blob: 22ba16eaeb917954a79277ede10b6a2b3da47ce4 [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
Patrick Stewart54679792016-11-08 13:03:34 +000057struct VStruct {
58 bool w;
59 uint32_t x;
60 double y;
61 bool z;
62};
63
64struct VUndeclStruct { //dtype not declared for this version
65 bool w;
66 uint32_t x;
67 double y;
68 bool z;
69};
70
Jason Rhinelander52f4be82016-09-03 14:54:22 -040071test_initializer stl_binder_vector([](py::module &m) {
Jason Rhinelander540ae612016-08-28 13:46:25 -040072 py::class_<El>(m, "El")
73 .def(py::init<int>());
Sergey Lyskova95bde12016-05-08 19:31:55 -040074
Patrick Stewart54679792016-11-08 13:03:34 +000075 py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol());
76 py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
Wenzel Jakobfe342412016-09-06 13:02:29 +090077 py::bind_vector<std::vector<bool>>(m, "VectorBool");
Sergey Lyskova95bde12016-05-08 19:31:55 -040078
Wenzel Jakobfe342412016-09-06 13:02:29 +090079 py::bind_vector<std::vector<El>>(m, "VectorEl");
Wenzel Jakob25c03ce2016-05-15 20:50:38 +020080
Wenzel Jakobfe342412016-09-06 13:02:29 +090081 py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
Jason Rhinelander617fbcf2016-11-15 06:30:38 -050082
Patrick Stewart54679792016-11-08 13:03:34 +000083 m.def("create_undeclstruct", [m] () mutable {
84 py::bind_vector<std::vector<VUndeclStruct>>(m, "VectorUndeclStruct", py::buffer_protocol());
85 });
86
87 try {
88 py::module::import("numpy");
89 } catch (...) {
90 return;
91 }
92 PYBIND11_NUMPY_DTYPE(VStruct, w, x, y, z);
93 py::class_<VStruct>(m, "VStruct").def_readwrite("x", &VStruct::x);
94 py::bind_vector<std::vector<VStruct>>(m, "VectorStruct", py::buffer_protocol());
95 m.def("get_vectorstruct", [] {return std::vector<VStruct> {{0, 5, 3.0, 1}, {1, 30, -1e4, 0}};});
Sergey Lyskov75204182016-08-29 22:50:38 -040096});
97
98test_initializer stl_binder_map([](py::module &m) {
Wenzel Jakobfe342412016-09-06 13:02:29 +090099 py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
100 py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
Sergey Lyskov75204182016-08-29 22:50:38 -0400101
Wenzel Jakobfe342412016-09-06 13:02:29 +0900102 py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
103 py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst");
Jason Rhinelander617fbcf2016-11-15 06:30:38 -0500104
Jason Rhinelander52f4be82016-09-03 14:54:22 -0400105});
Jason Rhinelander617fbcf2016-11-15 06:30:38 -0500106
107test_initializer stl_binder_noncopyable([](py::module &m) {
108 py::class_<E_nc>(m, "ENC")
109 .def(py::init<int>())
110 .def_readwrite("value", &E_nc::value);
111
112 py::bind_vector<std::vector<E_nc>>(m, "VectorENC");
113 m.def("get_vnc", &one_to_n<std::vector<E_nc>>, py::return_value_policy::reference);
114
115 py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
116 m.def("get_dnc", &one_to_n<std::deque<E_nc>>, py::return_value_policy::reference);
117
118 py::bind_map<std::map<int, E_nc>>(m, "MapENC");
119 m.def("get_mnc", &times_ten<std::map<int, E_nc>>, py::return_value_policy::reference);
120
121 py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
122 m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference);
123});