test_stl_binders.cpp
Go to the documentation of this file.
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;
21  El(int v) : a(v) { }
22 
23  int a;
24 };
25 
26 std::ostream & operator<<(std::ostream &s, El const&v) {
27  s << "El{" << v.a << '}';
28  return s;
29 }
30 
32 class E_nc {
33 public:
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 
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 
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 
57 template <class NestMap> NestMap *times_hundred(int n) {
58  auto m = new NestMap();
59  for (int i = 1; i <= n; i++)
60  for (int j = 1; j <= n; j++)
61  (*m)[i].emplace(int(j*10), E_nc(100*j));
62  return m;
63 }
64 
65 TEST_SUBMODULE(stl_binders, m) {
66  // test_vector_int
67  py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
68 
69  // test_vector_custom
70  py::class_<El>(m, "El")
71  .def(py::init<int>());
72  py::bind_vector<std::vector<El>>(m, "VectorEl");
73  py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
74 
75  // test_map_string_double
76  py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
77  py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
78 
79  // test_map_string_double_const
80  py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
81  py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst");
82 
83  py::class_<E_nc>(m, "ENC")
84  .def(py::init<int>())
85  .def_readwrite("value", &E_nc::value);
86 
87  // test_noncopyable_containers
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  py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
91  m.def("get_dnc", &one_to_n<std::deque<E_nc>>, py::return_value_policy::reference);
92  py::bind_map<std::map<int, E_nc>>(m, "MapENC");
93  m.def("get_mnc", &times_ten<std::map<int, E_nc>>, py::return_value_policy::reference);
94  py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
95  m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference);
96  // Issue #1885: binding nested std::map<X, Container<E>> with E non-copyable
97  py::bind_map<std::map<int, std::vector<E_nc>>>(m, "MapVecENC");
98  m.def("get_nvnc", [](int n)
99  {
100  auto m = new std::map<int, std::vector<E_nc>>();
101  for (int i = 1; i <= n; i++)
102  for (int j = 1; j <= n; j++)
103  (*m)[i].emplace_back(j);
104  return m;
105  }, py::return_value_policy::reference);
106  py::bind_map<std::map<int, std::map<int, E_nc>>>(m, "MapMapENC");
107  m.def("get_nmnc", &times_hundred<std::map<int, std::map<int, E_nc>>>, py::return_value_policy::reference);
108  py::bind_map<std::unordered_map<int, std::unordered_map<int, E_nc>>>(m, "UmapUmapENC");
109  m.def("get_numnc", &times_hundred<std::unordered_map<int, std::unordered_map<int, E_nc>>>, py::return_value_policy::reference);
110 
111  // test_vector_buffer
112  py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol());
113  // no dtype declared for this version:
114  struct VUndeclStruct { bool w; uint32_t x; double y; bool z; };
115  m.def("create_undeclstruct", [m] () mutable {
116  py::bind_vector<std::vector<VUndeclStruct>>(m, "VectorUndeclStruct", py::buffer_protocol());
117  });
118 
119  // The rest depends on numpy:
120  try { py::module::import("numpy"); }
121  catch (...) { return; }
122 
123  // test_vector_buffer_numpy
124  struct VStruct { bool w; uint32_t x; double y; bool z; };
125  PYBIND11_NUMPY_DTYPE(VStruct, w, x, y, z);
126  py::class_<VStruct>(m, "VStruct").def_readwrite("x", &VStruct::x);
127  py::bind_vector<std::vector<VStruct>>(m, "VectorStruct", py::buffer_protocol());
128  m.def("get_vectorstruct", [] {return std::vector<VStruct> {{0, 5, 3.0, 1}, {1, 30, -1e4, 0}};});
129 }
Matrix3f m
Issue #487: binding std::vector<E> with E non-copyable.
Map * times_ten(int n)
Scalar * y
ArrayXcf v
Definition: Cwise_arg.cpp:1
int n
std::ostream & operator<<(std::ostream &s, El const &v)
Container * one_to_n(int n)
#define PYBIND11_NUMPY_DTYPE(Type,...)
Definition: numpy.h:1244
El(int v)
El()=delete
unsigned int uint32_t
Definition: ms_stdint.h:85
RealScalar s
NestMap * times_hundred(int n)
RowVector3d w
TEST_SUBMODULE(stl_binders, m)
E_nc(int i)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
std::ptrdiff_t j


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:04