Deleted Added
sdiff udiff text old ( 11986:c12e4625ab56 ) new ( 12037:d28054ac6ec9 )
full compact
1/*
2 tests/test_stl_binders.cpp -- Usage of stl_binders functions
3
4 Copyright (c) 2016 Sergey Lyskov
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
12#include <pybind11/stl_bind.h>
13#include <pybind11/numpy.h>
14#include <map>
15#include <deque>
16#include <unordered_map>
17
18#ifdef _MSC_VER
19// We get some really long type names here which causes MSVC to emit warnings
20# pragma warning(disable: 4503) // warning C4503: decorated name length exceeded, name was truncated
21#endif
22
23class El {
24public:
25 El() = delete;
26 El(int v) : a(v) { }
27
28 int a;
29};
30
31std::ostream & operator<<(std::ostream &s, El const&v) {
32 s << "El{" << v.a << '}';
33 return s;
34}
35
36/// Issue #487: binding std::vector<E> with E non-copyable
37class E_nc {
38public:
39 explicit E_nc(int i) : value{i} {}
40 E_nc(const E_nc &) = delete;
41 E_nc &operator=(const E_nc &) = delete;
42 E_nc(E_nc &&) = default;
43 E_nc &operator=(E_nc &&) = default;
44
45 int value;
46};
47
48template <class Container> Container *one_to_n(int n) {
49 auto v = new Container();
50 for (int i = 1; i <= n; i++)
51 v->emplace_back(i);
52 return v;
53}
54
55template <class Map> Map *times_ten(int n) {
56 auto m = new Map();
57 for (int i = 1; i <= n; i++)
58 m->emplace(int(i), E_nc(10*i));
59 return m;
60}
61
62struct VStruct {
63 bool w;
64 uint32_t x;
65 double y;
66 bool z;
67};
68
69struct VUndeclStruct { //dtype not declared for this version
70 bool w;
71 uint32_t x;
72 double y;
73 bool z;
74};
75
76test_initializer stl_binder_vector([](py::module &m) {
77 py::class_<El>(m, "El")
78 .def(py::init<int>());
79
80 py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol());
81 py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
82 py::bind_vector<std::vector<bool>>(m, "VectorBool");
83
84 py::bind_vector<std::vector<El>>(m, "VectorEl");
85
86 py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
87
88 m.def("create_undeclstruct", [m] () mutable {
89 py::bind_vector<std::vector<VUndeclStruct>>(m, "VectorUndeclStruct", py::buffer_protocol());
90 });
91
92 try {
93 py::module::import("numpy");
94 } catch (...) {
95 return;
96 }
97 PYBIND11_NUMPY_DTYPE(VStruct, w, x, y, z);
98 py::class_<VStruct>(m, "VStruct").def_readwrite("x", &VStruct::x);
99 py::bind_vector<std::vector<VStruct>>(m, "VectorStruct", py::buffer_protocol());
100 m.def("get_vectorstruct", [] {return std::vector<VStruct> {{0, 5, 3.0, 1}, {1, 30, -1e4, 0}};});
101});
102
103test_initializer stl_binder_map([](py::module &m) {
104 py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
105 py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
106
107 py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
108 py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst");
109
110});
111
112test_initializer stl_binder_noncopyable([](py::module &m) {
113 py::class_<E_nc>(m, "ENC")
114 .def(py::init<int>())
115 .def_readwrite("value", &E_nc::value);
116
117 py::bind_vector<std::vector<E_nc>>(m, "VectorENC");
118 m.def("get_vnc", &one_to_n<std::vector<E_nc>>, py::return_value_policy::reference);
119
120 py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
121 m.def("get_dnc", &one_to_n<std::deque<E_nc>>, py::return_value_policy::reference);
122
123 py::bind_map<std::map<int, E_nc>>(m, "MapENC");
124 m.def("get_mnc", &times_ten<std::map<int, E_nc>>, py::return_value_policy::reference);
125
126 py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
127 m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference);
128});