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 TEST_SUBMODULE(stl_binders, m) {
74  // test_vector_int
75  py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
76 
77  // test_vector_custom
78  py::class_<El>(m, "El").def(py::init<int>());
79  py::bind_vector<std::vector<El>>(m, "VectorEl");
80  py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
81 
82  // test_map_string_double
83  py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
84  py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
85 
86  // test_map_string_double_const
87  py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
88  py::bind_map<std::unordered_map<std::string, double const>>(m,
89  "UnorderedMapStringDoubleConst");
90 
91  py::class_<E_nc>(m, "ENC").def(py::init<int>()).def_readwrite("value", &E_nc::value);
92 
93  // test_noncopyable_containers
94  py::bind_vector<std::vector<E_nc>>(m, "VectorENC");
95  m.def("get_vnc", &one_to_n<std::vector<E_nc>>);
96  py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
97  m.def("get_dnc", &one_to_n<std::deque<E_nc>>);
98  py::bind_map<std::map<int, E_nc>>(m, "MapENC");
99  m.def("get_mnc", &times_ten<std::map<int, E_nc>>);
100  py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
101  m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>);
102  // Issue #1885: binding nested std::map<X, Container<E>> with E non-copyable
103  py::bind_map<std::map<int, std::vector<E_nc>>>(m, "MapVecENC");
104  m.def("get_nvnc", [](int n) {
105  auto *m = new std::map<int, std::vector<E_nc>>();
106  for (int i = 1; i <= n; i++) {
107  for (int j = 1; j <= n; j++) {
108  (*m)[i].emplace_back(j);
109  }
110  }
111  return m;
112  });
113  py::bind_map<std::map<int, std::map<int, E_nc>>>(m, "MapMapENC");
114  m.def("get_nmnc", &times_hundred<std::map<int, std::map<int, E_nc>>>);
115  py::bind_map<std::unordered_map<int, std::unordered_map<int, E_nc>>>(m, "UmapUmapENC");
116  m.def("get_numnc", &times_hundred<std::unordered_map<int, std::unordered_map<int, E_nc>>>);
117 
118  // test_vector_buffer
119  py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol());
120  // no dtype declared for this version:
121  struct VUndeclStruct {
122  bool w;
123  uint32_t x;
124  double y;
125  bool z;
126  };
127  m.def("create_undeclstruct", [m]() mutable {
128  py::bind_vector<std::vector<VUndeclStruct>>(
129  m, "VectorUndeclStruct", py::buffer_protocol());
130  });
131 
132  // The rest depends on numpy:
133  try {
134  py::module_::import("numpy");
135  } catch (...) {
136  return;
137  }
138 
139  // test_vector_buffer_numpy
140  struct VStruct {
141  bool w;
142  uint32_t x;
143  double y;
144  bool z;
145  };
146  PYBIND11_NUMPY_DTYPE(VStruct, w, x, y, z);
147  py::class_<VStruct>(m, "VStruct").def_readwrite("x", &VStruct::x);
148  py::bind_vector<std::vector<VStruct>>(m, "VectorStruct", py::buffer_protocol());
149  m.def("get_vectorstruct", [] {
150  return std::vector<VStruct>{{false, 5, 3.0, true}, {true, 30, -1e4, false}};
151  });
152 }
Matrix3f m
Issue #487: binding std::vector<E> with E non-copyable.
Map * times_ten(int n)
Scalar * y
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:1507
El(int v)
Array< int, Dynamic, 1 > 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 Tue Jul 4 2023 02:37:46