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 class El {
19 public:
20     El() = delete;
El(int v)21     El(int v) : a(v) { }
22 
23     int a;
24 };
25 
operator <<(std::ostream & s,El const & v)26 std::ostream & operator<<(std::ostream &s, El const&v) {
27     s << "El{" << v.a << '}';
28     return s;
29 }
30 
31 /// Issue #487: binding std::vector<E> with E non-copyable
32 class E_nc {
33 public:
E_nc(int i)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 
one_to_n(int n)43 template <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 
times_ten(int n)50 template <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 
TEST_SUBMODULE(stl_binders,m)57 TEST_SUBMODULE(stl_binders, m) {
58     // test_vector_int
59     py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
60 
61     // test_vector_custom
62     py::class_<El>(m, "El")
63         .def(py::init<int>());
64     py::bind_vector<std::vector<El>>(m, "VectorEl");
65     py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
66 
67     // test_map_string_double
68     py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
69     py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
70 
71     // test_map_string_double_const
72     py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
73     py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst");
74 
75     py::class_<E_nc>(m, "ENC")
76         .def(py::init<int>())
77         .def_readwrite("value", &E_nc::value);
78 
79     // test_noncopyable_containers
80     py::bind_vector<std::vector<E_nc>>(m, "VectorENC");
81     m.def("get_vnc", &one_to_n<std::vector<E_nc>>, py::return_value_policy::reference);
82     py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
83     m.def("get_dnc", &one_to_n<std::deque<E_nc>>, py::return_value_policy::reference);
84     py::bind_map<std::map<int, E_nc>>(m, "MapENC");
85     m.def("get_mnc", &times_ten<std::map<int, E_nc>>, py::return_value_policy::reference);
86     py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
87     m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference);
88 
89     // test_vector_buffer
90     py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol());
91     // no dtype declared for this version:
92     struct VUndeclStruct { bool w; uint32_t x; double y; bool z; };
93     m.def("create_undeclstruct", [m] () mutable {
94         py::bind_vector<std::vector<VUndeclStruct>>(m, "VectorUndeclStruct", py::buffer_protocol());
95     });
96 
97     // The rest depends on numpy:
98     try { py::module::import("numpy"); }
99     catch (...) { return; }
100 
101     // test_vector_buffer_numpy
102     struct VStruct { bool w; uint32_t x; double y; bool z; };
103     PYBIND11_NUMPY_DTYPE(VStruct, w, x, y, z);
104     py::class_<VStruct>(m, "VStruct").def_readwrite("x", &VStruct::x);
105     py::bind_vector<std::vector<VStruct>>(m, "VectorStruct", py::buffer_protocol());
106     m.def("get_vectorstruct", [] {return std::vector<VStruct> {{0, 5, 3.0, 1}, {1, 30, -1e4, 0}};});
107 }
108