test_stl_binders.cpp (11986:c12e4625ab56) test_stl_binders.cpp (12037:d28054ac6ec9)
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>
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>
13#include <map>
14#include <deque>
15#include <unordered_map>
16
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
17class El {
18public:
19 El() = delete;
20 El(int v) : a(v) { }
21
22 int a;
23};
24

--- 23 unchanged lines hidden (view full) ---

48
49template <class Map> Map *times_ten(int n) {
50 auto m = new Map();
51 for (int i = 1; i <= n; i++)
52 m->emplace(int(i), E_nc(10*i));
53 return m;
54}
55
23class El {
24public:
25 El() = delete;
26 El(int v) : a(v) { }
27
28 int a;
29};
30

--- 23 unchanged lines hidden (view full) ---

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
56test_initializer stl_binder_vector([](py::module &m) {
57 py::class_<El>(m, "El")
58 .def(py::init<int>());
59
76test_initializer stl_binder_vector([](py::module &m) {
77 py::class_<El>(m, "El")
78 .def(py::init<int>());
79
60 py::bind_vector<std::vector<unsigned int>>(m, "VectorInt");
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());
61 py::bind_vector<std::vector<bool>>(m, "VectorBool");
62
63 py::bind_vector<std::vector<El>>(m, "VectorEl");
64
65 py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
66
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}};});
67});
68
69test_initializer stl_binder_map([](py::module &m) {
70 py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
71 py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
72
73 py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
74 py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst");

--- 12 unchanged lines hidden (view full) ---

87 m.def("get_dnc", &one_to_n<std::deque<E_nc>>, py::return_value_policy::reference);
88
89 py::bind_map<std::map<int, E_nc>>(m, "MapENC");
90 m.def("get_mnc", &times_ten<std::map<int, E_nc>>, py::return_value_policy::reference);
91
92 py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
93 m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference);
94});
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");

--- 12 unchanged lines hidden (view full) ---

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});
95