stl.h
Go to the documentation of this file.
1 /*
2  pybind11/stl.h: Transparent conversion for STL data types
3 
4  Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
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 #pragma once
11 
12 #include "pybind11.h"
13 #include "detail/common.h"
14 
15 #include <deque>
16 #include <list>
17 #include <map>
18 #include <ostream>
19 #include <set>
20 #include <unordered_map>
21 #include <unordered_set>
22 #include <valarray>
23 
24 // See `detail/common.h` for implementation of these guards.
25 #if defined(PYBIND11_HAS_OPTIONAL)
26 # include <optional>
27 #elif defined(PYBIND11_HAS_EXP_OPTIONAL)
28 # include <experimental/optional>
29 #endif
30 
31 #if defined(PYBIND11_HAS_VARIANT)
32 # include <variant>
33 #endif
34 
37 
38 template <typename T, typename U>
44 
47 template <typename T, typename U>
49  return std::forward<detail::forwarded_type<T, U>>(std::forward<U>(u));
50 }
51 
52 template <typename Type, typename Key>
53 struct set_caster {
54  using type = Type;
56 
57  bool load(handle src, bool convert) {
58  if (!isinstance<anyset>(src)) {
59  return false;
60  }
61  auto s = reinterpret_borrow<anyset>(src);
62  value.clear();
63  for (auto entry : s) {
64  key_conv conv;
65  if (!conv.load(entry, convert)) {
66  return false;
67  }
68  value.insert(cast_op<Key &&>(std::move(conv)));
69  }
70  return true;
71  }
72 
73  template <typename T>
74  static handle cast(T &&src, return_value_policy policy, handle parent) {
77  }
79  for (auto &&value : src) {
80  auto value_ = reinterpret_steal<object>(
81  key_conv::cast(forward_like<T>(value), policy, parent));
82  if (!value_ || !s.add(std::move(value_))) {
83  return handle();
84  }
85  }
86  return s.release();
87  }
88 
90 };
91 
92 template <typename Type, typename Key, typename Value>
93 struct map_caster {
96 
97  bool load(handle src, bool convert) {
98  if (!isinstance<dict>(src)) {
99  return false;
100  }
101  auto d = reinterpret_borrow<dict>(src);
102  value.clear();
103  for (auto it : d) {
104  key_conv kconv;
105  value_conv vconv;
106  if (!kconv.load(it.first.ptr(), convert) || !vconv.load(it.second.ptr(), convert)) {
107  return false;
108  }
109  value.emplace(cast_op<Key &&>(std::move(kconv)), cast_op<Value &&>(std::move(vconv)));
110  }
111  return true;
112  }
113 
114  template <typename T>
115  static handle cast(T &&src, return_value_policy policy, handle parent) {
116  dict d;
117  return_value_policy policy_key = policy;
118  return_value_policy policy_value = policy;
120  policy_key = return_value_policy_override<Key>::policy(policy_key);
121  policy_value = return_value_policy_override<Value>::policy(policy_value);
122  }
123  for (auto &&kv : src) {
124  auto key = reinterpret_steal<object>(
125  key_conv::cast(forward_like<T>(kv.first), policy_key, parent));
126  auto value = reinterpret_steal<object>(
127  value_conv::cast(forward_like<T>(kv.second), policy_value, parent));
128  if (!key || !value) {
129  return handle();
130  }
131  d[std::move(key)] = std::move(value);
132  }
133  return d.release();
134  }
135 
138  + const_name("]"));
139 };
140 
141 template <typename Type, typename Value>
142 struct list_caster {
144 
145  bool load(handle src, bool convert) {
146  if (!isinstance<sequence>(src) || isinstance<bytes>(src) || isinstance<str>(src)) {
147  return false;
148  }
149  auto s = reinterpret_borrow<sequence>(src);
150  value.clear();
151  reserve_maybe(s, &value);
152  for (auto it : s) {
154  if (!conv.load(it, convert)) {
155  return false;
156  }
157  value.push_back(cast_op<Value &&>(std::move(conv)));
158  }
159  return true;
160  }
161 
162 private:
163  template <
164  typename T = Type,
165  enable_if_t<std::is_same<decltype(std::declval<T>().reserve(0)), void>::value, int> = 0>
166  void reserve_maybe(const sequence &s, Type *) {
167  value.reserve(s.size());
168  }
169  void reserve_maybe(const sequence &, void *) {}
170 
171 public:
172  template <typename T>
173  static handle cast(T &&src, return_value_policy policy, handle parent) {
176  }
177  list l(src.size());
178  ssize_t index = 0;
179  for (auto &&value : src) {
180  auto value_ = reinterpret_steal<object>(
181  value_conv::cast(forward_like<T>(value), policy, parent));
182  if (!value_) {
183  return handle();
184  }
185  PyList_SET_ITEM(l.ptr(), index++, value_.release().ptr()); // steals a reference
186  }
187  return l.release();
188  }
189 
191 };
192 
193 template <typename Type, typename Alloc>
195 
196 template <typename Type, typename Alloc>
197 struct type_caster<std::deque<Type, Alloc>> : list_caster<std::deque<Type, Alloc>, Type> {};
198 
199 template <typename Type, typename Alloc>
200 struct type_caster<std::list<Type, Alloc>> : list_caster<std::list<Type, Alloc>, Type> {};
201 
202 template <typename ArrayType, typename Value, bool Resizable, size_t Size = 0>
203 struct array_caster {
205 
206 private:
207  template <bool R = Resizable>
209  if (value.size() != size) {
210  value.resize(size);
211  }
212  return true;
213  }
214  template <bool R = Resizable>
216  return size == Size;
217  }
218 
219 public:
220  bool load(handle src, bool convert) {
221  if (!isinstance<sequence>(src)) {
222  return false;
223  }
224  auto l = reinterpret_borrow<sequence>(src);
225  if (!require_size(l.size())) {
226  return false;
227  }
228  size_t ctr = 0;
229  for (auto it : l) {
231  if (!conv.load(it, convert)) {
232  return false;
233  }
234  value[ctr++] = cast_op<Value &&>(std::move(conv));
235  }
236  return true;
237  }
238 
239  template <typename T>
240  static handle cast(T &&src, return_value_policy policy, handle parent) {
241  list l(src.size());
242  ssize_t index = 0;
243  for (auto &&value : src) {
244  auto value_ = reinterpret_steal<object>(
245  value_conv::cast(forward_like<T>(value), policy, parent));
246  if (!value_) {
247  return handle();
248  }
249  PyList_SET_ITEM(l.ptr(), index++, value_.release().ptr()); // steals a reference
250  }
251  return l.release();
252  }
253 
254  PYBIND11_TYPE_CASTER(ArrayType,
255  const_name("List[") + value_conv::name
256  + const_name<Resizable>(const_name(""),
257  const_name("[") + const_name<Size>()
258  + const_name("]"))
259  + const_name("]"));
260 };
261 
262 template <typename Type, size_t Size>
263 struct type_caster<std::array<Type, Size>>
264  : array_caster<std::array<Type, Size>, Type, false, Size> {};
265 
266 template <typename Type>
267 struct type_caster<std::valarray<Type>> : array_caster<std::valarray<Type>, Type, true> {};
268 
269 template <typename Key, typename Compare, typename Alloc>
270 struct type_caster<std::set<Key, Compare, Alloc>>
271  : set_caster<std::set<Key, Compare, Alloc>, Key> {};
272 
273 template <typename Key, typename Hash, typename Equal, typename Alloc>
274 struct type_caster<std::unordered_set<Key, Hash, Equal, Alloc>>
275  : set_caster<std::unordered_set<Key, Hash, Equal, Alloc>, Key> {};
276 
277 template <typename Key, typename Value, typename Compare, typename Alloc>
278 struct type_caster<std::map<Key, Value, Compare, Alloc>>
279  : map_caster<std::map<Key, Value, Compare, Alloc>, Key, Value> {};
280 
281 template <typename Key, typename Value, typename Hash, typename Equal, typename Alloc>
282 struct type_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>>
283  : map_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>, Key, Value> {};
284 
285 // This type caster is intended to be used for std::optional and std::experimental::optional
286 template <typename Type, typename Value = typename Type::value_type>
289 
290  template <typename T>
291  static handle cast(T &&src, return_value_policy policy, handle parent) {
292  if (!src) {
293  return none().inc_ref();
294  }
297  }
298  return value_conv::cast(*std::forward<T>(src), policy, parent);
299  }
300 
301  bool load(handle src, bool convert) {
302  if (!src) {
303  return false;
304  }
305  if (src.is_none()) {
306  return true; // default-constructed value is already empty
307  }
308  value_conv inner_caster;
309  if (!inner_caster.load(src, convert)) {
310  return false;
311  }
312 
313  value.emplace(cast_op<Value &&>(std::move(inner_caster)));
314  return true;
315  }
316 
318 };
319 
320 #if defined(PYBIND11_HAS_OPTIONAL)
321 template <typename T>
322 struct type_caster<std::optional<T>> : public optional_caster<std::optional<T>> {};
323 
324 template <>
325 struct type_caster<std::nullopt_t> : public void_caster<std::nullopt_t> {};
326 #endif
327 
328 #if defined(PYBIND11_HAS_EXP_OPTIONAL)
329 template <typename T>
330 struct type_caster<std::experimental::optional<T>>
331  : public optional_caster<std::experimental::optional<T>> {};
332 
333 template <>
334 struct type_caster<std::experimental::nullopt_t>
335  : public void_caster<std::experimental::nullopt_t> {};
336 #endif
337 
342 
343  using result_type = handle; // required by boost::variant in C++11
344 
345  template <typename T>
346  result_type operator()(T &&src) const {
347  return make_caster<T>::cast(std::forward<T>(src), policy, parent);
348  }
349 };
350 
355 template <template <typename...> class Variant>
356 struct visit_helper {
357  template <typename... Args>
358  static auto call(Args &&...args) -> decltype(visit(std::forward<Args>(args)...)) {
359  return visit(std::forward<Args>(args)...);
360  }
361 };
362 
364 template <typename Variant>
366 
367 template <template <typename...> class V, typename... Ts>
368 struct variant_caster<V<Ts...>> {
369  static_assert(sizeof...(Ts) > 0, "Variant must consist of at least one alternative.");
370 
371  template <typename U, typename... Us>
373  auto caster = make_caster<U>();
374  if (caster.load(src, convert)) {
375  value = cast_op<U>(std::move(caster));
376  return true;
377  }
378  return load_alternative(src, convert, type_list<Us...>{});
379  }
380 
381  bool load_alternative(handle, bool, type_list<>) { return false; }
382 
383  bool load(handle src, bool convert) {
384  // Do a first pass without conversions to improve constructor resolution.
385  // E.g. `py::int_(1).cast<variant<double, int>>()` needs to fill the `int`
386  // slot of the variant. Without two-pass loading `double` would be filled
387  // because it appears first and a conversion is possible.
388  if (convert && load_alternative(src, false, type_list<Ts...>{})) {
389  return true;
390  }
391  return load_alternative(src, convert, type_list<Ts...>{});
392  }
393 
394  template <typename Variant>
395  static handle cast(Variant &&src, return_value_policy policy, handle parent) {
396  return visit_helper<V>::call(variant_caster_visitor{policy, parent},
397  std::forward<Variant>(src));
398  }
399 
400  using Type = V<Ts...>;
403  + const_name("]"));
404 };
405 
406 #if defined(PYBIND11_HAS_VARIANT)
407 template <typename... Ts>
408 struct type_caster<std::variant<Ts...>> : variant_caster<std::variant<Ts...>> {};
409 
410 template <>
411 struct type_caster<std::monostate> : public void_caster<std::monostate> {};
412 #endif
413 
415 
416 inline std::ostream &operator<<(std::ostream &os, const handle &obj) {
417 #ifdef PYBIND11_HAS_STRING_VIEW
418  os << str(obj).cast<std::string_view>();
419 #else
420  os << (std::string) str(obj);
421 #endif
422  return os;
423 }
424 
typename std::conditional< B, T, F >::type conditional_t
const gtsam::Symbol key('X', 0)
Helper template which holds a list of types.
void reserve_maybe(const sequence &, void *)
Definition: stl.h:169
PyObject * conv(PyObject *o)
bool load(handle src, bool convert)
Definition: pytypes.h:2012
bool load(handle src, bool convert)
Definition: stl.h:97
Definition: numpy.h:680
result_type operator()(T &&src) const
Definition: stl.h:346
bool load(handle src, bool convert)
Definition: stl.h:220
Definition: BFloat16.h:88
void reserve_maybe(const sequence &s, Type *)
Definition: stl.h:166
bool load_alternative(handle, bool, type_list<>)
Definition: stl.h:381
Definition: pytypes.h:1614
const handle & inc_ref() const &
Definition: pytypes.h:246
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
bool load(handle src, bool convert)
Definition: stl.h:145
static handle cast(T &&src, return_value_policy policy, handle parent)
Definition: stl.h:115
static const Line3 l(Rot3(), 1, 1)
static handle cast(const itype &src, return_value_policy policy, handle parent)
return_value_policy policy
Definition: stl.h:340
bool require_size(enable_if_t< R, size_t > size)
Definition: stl.h:208
static handle cast(T &&src, return_value_policy policy, handle parent)
Definition: stl.h:173
Definition: stl.h:53
EIGEN_DEVICE_FUNC NewType cast(const OldType &x)
static return_value_policy policy(return_value_policy p)
Definition: cast.h:995
RealScalar s
typename std::remove_reference< T >::type remove_reference_t
constexpr descr< 0 > concat()
Definition: descr.h:139
Definition: stl.h:93
void set(Container &c, Position position, const Value &value)
static handle cast(T &&src, return_value_policy policy, handle parent)
Definition: stl.h:291
conditional_t< std::is_lvalue_reference< T >::value, remove_reference_t< U > &, remove_reference_t< U > && > forwarded_type
Definition: stl.h:43
Definition: pytypes.h:1979
bool require_size(enable_if_t<!R, size_t > size)
Definition: stl.h:215
handle release()
Definition: pytypes.h:330
Generic variant caster.
Definition: stl.h:365
static auto call(Args &&...args) -> decltype(visit(std::forward< Args >(args)...))
Definition: stl.h:358
ofstream os("timeSchurFactors.csv")
static handle cast(T &&src, return_value_policy policy, handle parent)
Definition: stl.h:74
bool load(handle src, bool convert)
Definition: stl.h:57
bool load_alternative(handle src, bool convert, type_list< U, Us... >)
Definition: stl.h:372
bool load(handle src, bool convert)
Definition: stl.h:383
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
static constexpr auto name
Definition: pytypes.h:1924
forwarded_type< T, U > forward_like(U &&u)
Definition: stl.h:48
static handle cast(Variant &&src, return_value_policy policy, handle parent)
Definition: stl.h:395
constexpr descr< N - 1 > const_name(char const (&text)[N])
Definition: descr.h:60
bool load(handle src, bool convert)
Definition: stl.h:301
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
Definition: pytypes.h:2030
PYBIND11_TYPE_CASTER(type, const_name("Set[")+key_conv::name+const_name("]"))
Visit a variant and cast any found type to Python.
Definition: stl.h:339
#define PYBIND11_NAMESPACE_END(name)
static BinaryMeasurement< Rot3 > convert(const BetweenFactor< Pose3 >::shared_ptr &f)
static handle cast(T &&src, return_value_policy policy, handle parent)
Definition: stl.h:240
size_t size() const
Definition: pytypes.h:1962
Definition: pytypes.h:1370
#define PYBIND11_NAMESPACE_BEGIN(name)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:36:20