blob: ce0b33257d8a7e65ae841572242cd3067f4141e8 [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>
Wenzel Jakobfe342412016-09-06 13:02:29 +090013#include <map>
Jason Rhinelander617fbcf2016-11-15 06:30:38 -050014#include <deque>
Wenzel Jakobfe342412016-09-06 13:02:29 +090015#include <unordered_map>
Sergey Lyskoveae77442016-05-07 00:26:19 -040016
Jason Rhinelanderfa5d05e2016-12-12 18:11:49 -050017#ifdef _MSC_VER
18// We get some really long type names here which causes MSVC to emit warnings
19# pragma warning(disable: 4503) // warning C4503: decorated name length exceeded, name was truncated
20#endif
21
Wenzel Jakob25c03ce2016-05-15 20:50:38 +020022class El {
Sergey Lyskoveae77442016-05-07 00:26:19 -040023public:
Jason Rhinelander540ae612016-08-28 13:46:25 -040024 El() = delete;
25 El(int v) : a(v) { }
Sergey Lyskova95bde12016-05-08 19:31:55 -040026
Jason Rhinelander540ae612016-08-28 13:46:25 -040027 int a;
Sergey Lyskoveae77442016-05-07 00:26:19 -040028};
29
Wenzel Jakob25c03ce2016-05-15 20:50:38 +020030std::ostream & operator<<(std::ostream &s, El const&v) {
Jason Rhinelander540ae612016-08-28 13:46:25 -040031 s << "El{" << v.a << '}';
32 return s;
Sergey Lyskova95bde12016-05-08 19:31:55 -040033}
Sergey Lyskoveae77442016-05-07 00:26:19 -040034
Jason Rhinelander617fbcf2016-11-15 06:30:38 -050035/// Issue #487: binding std::vector<E> with E non-copyable
36class E_nc {
37public:
38 explicit E_nc(int i) : value{i} {}
39 E_nc(const E_nc &) = delete;
40 E_nc &operator=(const E_nc &) = delete;
41 E_nc(E_nc &&) = default;
42 E_nc &operator=(E_nc &&) = default;
43
44 int value;
45};
46
47template <class Container> Container *one_to_n(int n) {
48 auto v = new Container();
49 for (int i = 1; i <= n; i++)
50 v->emplace_back(i);
51 return v;
52}
53
54template <class Map> Map *times_ten(int n) {
55 auto m = new Map();
56 for (int i = 1; i <= n; i++)
57 m->emplace(int(i), E_nc(10*i));
58 return m;
59}
60
Jason Rhinelander52f4be82016-09-03 14:54:22 -040061test_initializer stl_binder_vector([](py::module &m) {
Jason Rhinelander540ae612016-08-28 13:46:25 -040062 py::class_<El>(m, "El")
63 .def(py::init<int>());
Sergey Lyskova95bde12016-05-08 19:31:55 -040064
Wenzel Jakobfe342412016-09-06 13:02:29 +090065 py::bind_vector<std::vector<unsigned int>>(m, "VectorInt");
66 py::bind_vector<std::vector<bool>>(m, "VectorBool");
Sergey Lyskova95bde12016-05-08 19:31:55 -040067
Wenzel Jakobfe342412016-09-06 13:02:29 +090068 py::bind_vector<std::vector<El>>(m, "VectorEl");
Wenzel Jakob25c03ce2016-05-15 20:50:38 +020069
Wenzel Jakobfe342412016-09-06 13:02:29 +090070 py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
Jason Rhinelander617fbcf2016-11-15 06:30:38 -050071
Sergey Lyskov75204182016-08-29 22:50:38 -040072});
73
74test_initializer stl_binder_map([](py::module &m) {
Wenzel Jakobfe342412016-09-06 13:02:29 +090075 py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
76 py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
Sergey Lyskov75204182016-08-29 22:50:38 -040077
Wenzel Jakobfe342412016-09-06 13:02:29 +090078 py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
79 py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst");
Jason Rhinelander617fbcf2016-11-15 06:30:38 -050080
Jason Rhinelander52f4be82016-09-03 14:54:22 -040081});
Jason Rhinelander617fbcf2016-11-15 06:30:38 -050082
83test_initializer stl_binder_noncopyable([](py::module &m) {
84 py::class_<E_nc>(m, "ENC")
85 .def(py::init<int>())
86 .def_readwrite("value", &E_nc::value);
87
88 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);
90
91 py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
92 m.def("get_dnc", &one_to_n<std::deque<E_nc>>, py::return_value_policy::reference);
93
94 py::bind_map<std::map<int, E_nc>>(m, "MapENC");
95 m.def("get_mnc", &times_ten<std::map<int, E_nc>>, py::return_value_policy::reference);
96
97 py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
98 m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference);
99});
100