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/numpy.h>
11 #include <pybind11/stl_bind.h>
12 
13 #include "pybind11_tests.h"
14 
15 #include <deque>
16 #include <map>
17 #include <unordered_map>
18 
19 class El {
20 public:
21  El() = delete;
22  explicit El(int v) : a(v) {}
23 
24  int a;
25 };
26 
27 std::ostream &operator<<(std::ostream &s, El const &v) {
28  s << "El{" << v.a << '}';
29  return s;
30 }
31 
33 class E_nc {
34 public:
35  explicit E_nc(int i) : value{i} {}
36  E_nc(const E_nc &) = delete;
37  E_nc &operator=(const E_nc &) = delete;
38  E_nc(E_nc &&) = default;
39  E_nc &operator=(E_nc &&) = default;
40 
41  int value;
42 };
43 
44 template <class Container>
45 Container *one_to_n(int n) {
46  auto *v = new Container();
47  for (int i = 1; i <= n; i++) {
48  v->emplace_back(i);
49  }
50  return v;
51 }
52 
53 template <class Map>
54 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  }
59  return m;
60 }
61 
62 template <class NestMap>
63 NestMap *times_hundred(int n) {
64  auto *m = new NestMap();
65  for (int i = 1; i <= n; i++) {
66  for (int j = 1; j <= n; j++) {
67  (*m)[i].emplace(int(j * 10), E_nc(100 * j));
68  }
69  }
70  return m;
71 }
72 
73 /*
74  * Recursive data structures as test for issue #4623
75  */
76 struct RecursiveVector : std::vector<RecursiveVector> {
77  using Parent = std::vector<RecursiveVector>;
78  using Parent::Parent;
79 };
80 
81 struct RecursiveMap : std::map<int, RecursiveMap> {
82  using Parent = std::map<int, RecursiveMap>;
83  using Parent::Parent;
84 };
85 
86 /*
87  * Pybind11 does not catch more complicated recursion schemes, such as mutual
88  * recursion.
89  * In that case custom recursive_container_traits specializations need to be added,
90  * thus manually telling pybind11 about the recursion.
91  */
94 
95 struct MutuallyRecursiveContainerPairMV : std::map<int, MutuallyRecursiveContainerPairVM> {};
96 struct MutuallyRecursiveContainerPairVM : std::vector<MutuallyRecursiveContainerPairMV> {};
97 
98 namespace pybind11 {
99 namespace detail {
100 template <typename SFINAE>
103 };
104 template <typename SFINAE>
107 };
108 } // namespace detail
109 } // namespace pybind11
110 
111 TEST_SUBMODULE(stl_binders, m) {
112  // test_vector_int
113  py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
114 
115  // test_vector_custom
116  py::class_<El>(m, "El").def(py::init<int>());
117  py::bind_vector<std::vector<El>>(m, "VectorEl");
118  py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
119 
120  // test_map_string_double
121  py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
122  py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
123 
124  // test_map_string_double_const
125  py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
126  py::bind_map<std::unordered_map<std::string, double const>>(m,
127  "UnorderedMapStringDoubleConst");
128 
129  py::class_<E_nc>(m, "ENC").def(py::init<int>()).def_readwrite("value", &E_nc::value);
130 
131  // test_noncopyable_containers
132  py::bind_vector<std::vector<E_nc>>(m, "VectorENC");
133  m.def("get_vnc", &one_to_n<std::vector<E_nc>>);
134  py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
135  m.def("get_dnc", &one_to_n<std::deque<E_nc>>);
136  py::bind_map<std::map<int, E_nc>>(m, "MapENC");
137  m.def("get_mnc", &times_ten<std::map<int, E_nc>>);
138  py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
139  m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>);
140  // Issue #1885: binding nested std::map<X, Container<E>> with E non-copyable
141  py::bind_map<std::map<int, std::vector<E_nc>>>(m, "MapVecENC");
142  m.def("get_nvnc", [](int n) {
143  auto *m = new std::map<int, std::vector<E_nc>>();
144  for (int i = 1; i <= n; i++) {
145  for (int j = 1; j <= n; j++) {
146  (*m)[i].emplace_back(j);
147  }
148  }
149  return m;
150  });
151  py::bind_map<std::map<int, std::map<int, E_nc>>>(m, "MapMapENC");
152  m.def("get_nmnc", &times_hundred<std::map<int, std::map<int, E_nc>>>);
153  py::bind_map<std::unordered_map<int, std::unordered_map<int, E_nc>>>(m, "UmapUmapENC");
154  m.def("get_numnc", &times_hundred<std::unordered_map<int, std::unordered_map<int, E_nc>>>);
155 
156  // test_vector_buffer
157  py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol());
158  // no dtype declared for this version:
159  struct VUndeclStruct {
160  bool w;
161  uint32_t x;
162  double y;
163  bool z;
164  };
165  m.def("create_undeclstruct", [m]() mutable {
166  py::bind_vector<std::vector<VUndeclStruct>>(
167  m, "VectorUndeclStruct", py::buffer_protocol());
168  });
169 
170  // Bind recursive container types
171  py::bind_vector<RecursiveVector>(m, "RecursiveVector");
172  py::bind_map<RecursiveMap>(m, "RecursiveMap");
173  py::bind_map<MutuallyRecursiveContainerPairMV>(m, "MutuallyRecursiveContainerPairMV");
174  py::bind_vector<MutuallyRecursiveContainerPairVM>(m, "MutuallyRecursiveContainerPairVM");
175 
176  // The rest depends on numpy:
177  try {
178  py::module_::import("numpy");
179  } catch (...) {
180  return;
181  }
182 
183  // test_vector_buffer_numpy
184  struct VStruct {
185  bool w;
186  uint32_t x;
187  double y;
188  bool z;
189  };
190  PYBIND11_NUMPY_DTYPE(VStruct, w, x, y, z);
191  py::class_<VStruct>(m, "VStruct").def_readwrite("x", &VStruct::x);
192  py::bind_vector<std::vector<VStruct>>(m, "VectorStruct", py::buffer_protocol());
193  m.def("get_vectorstruct", [] {
194  return std::vector<VStruct>{{false, 5, 3.0, true}, {true, 30, -1e4, false}};
195  });
196 }
w
RowVector3d w
Definition: Matrix_resize_int.cpp:3
E_nc::value
int value
Definition: test_stl_binders.cpp:41
s
RealScalar s
Definition: level1_cplx_impl.h:126
MutuallyRecursiveContainerPairVM
Definition: test_stl_binders.cpp:96
uint32_t
unsigned int uint32_t
Definition: ms_stdint.h:85
E_nc::operator=
E_nc & operator=(const E_nc &)=delete
x
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
Definition: gnuplot_common_settings.hh:12
El::a
int a
Definition: test_stl_binders.cpp:24
detail
Definition: testSerializationNonlinear.cpp:70
RecursiveMap::Parent
std::map< int, RecursiveMap > Parent
Definition: test_stl_binders.cpp:82
Eigen::DenseBase::operator<<
std::ostream & operator<<(std::ostream &s, const DenseBase< Derived > &m)
Definition: IO.h:250
El
Definition: test_stl_binders.cpp:19
times_hundred
NestMap * times_hundred(int n)
Definition: test_stl_binders.cpp:63
El::El
El()=delete
RecursiveVector::Parent
std::vector< RecursiveVector > Parent
Definition: test_stl_binders.cpp:77
n
int n
Definition: BiCGSTAB_simple.cpp:1
recursive_container_traits
Definition: type_caster_base.h:969
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
PYBIND11_NUMPY_DTYPE
#define PYBIND11_NUMPY_DTYPE(Type,...)
Definition: numpy.h:1515
RecursiveMap
Definition: test_copy_move.cpp:516
MutuallyRecursiveContainerPairMV
Definition: test_stl_binders.cpp:95
one_to_n
Container * one_to_n(int n)
Definition: test_stl_binders.cpp:45
El::El
El(int v)
Definition: test_stl_binders.cpp:22
numpy.h
pybind_wrapper_test_script.z
z
Definition: pybind_wrapper_test_script.py:61
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
RecursiveVector
Definition: test_copy_move.cpp:497
y
Scalar * y
Definition: level1_cplx_impl.h:124
E_nc
Issue #487: binding std::vector<E> with E non-copyable.
Definition: test_stl_binders.cpp:33
times_ten
Map * times_ten(int n)
Definition: test_stl_binders.cpp:54
pybind11_tests.h
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
pybind11
Definition: wrap/pybind11/pybind11/__init__.py:1
recursive_bottom
Definition: type_caster_base.h:881
TEST_SUBMODULE
TEST_SUBMODULE(stl_binders, m)
Definition: test_stl_binders.cpp:111
stl_bind.h
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
E_nc::E_nc
E_nc(int i)
Definition: test_stl_binders.cpp:35


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:05:31