stl_bind.h
Go to the documentation of this file.
1 /*
2  pybind11/std_bind.h: Binding generators for STL data types
3 
4  Copyright (c) 2016 Sergey Lyskov and Wenzel Jakob
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 "detail/common.h"
14 #include "cast.h"
15 #include "operators.h"
16 
17 #include <algorithm>
18 #include <sstream>
19 #include <type_traits>
20 
23 
24 /* SFINAE helper class used by 'is_comparable */
25 template <typename T>
27  template <typename T2>
28  static std::true_type
29  test_comparable(decltype(std::declval<const T2 &>() == std::declval<const T2 &>()) *);
30  template <typename T2>
31  static std::false_type test_comparable(...);
32  template <typename T2>
33  static std::true_type test_value(typename T2::value_type *);
34  template <typename T2>
35  static std::false_type test_value(...);
36  template <typename T2>
37  static std::true_type test_pair(typename T2::first_type *, typename T2::second_type *);
38  template <typename T2>
39  static std::false_type test_pair(...);
40 
41  static constexpr const bool is_comparable
42  = std::is_same<std::true_type, decltype(test_comparable<T>(nullptr))>::value;
43  static constexpr const bool is_pair
44  = std::is_same<std::true_type, decltype(test_pair<T>(nullptr, nullptr))>::value;
45  static constexpr const bool is_vector
46  = std::is_same<std::true_type, decltype(test_value<T>(nullptr))>::value;
47  static constexpr const bool is_element = !is_pair && !is_vector;
48 };
49 
50 /* Default: is_comparable -> std::false_type */
51 template <typename T, typename SFINAE = void>
52 struct is_comparable : std::false_type {};
53 
54 /* For non-map data structures, check whether operator== can be instantiated */
55 template <typename T>
57  T,
59  : std::true_type {};
60 
61 /* For a vector/map data structure, recursively check the value type
62  (which is std::pair for maps) */
63 template <typename T>
65  : is_comparable<typename recursive_container_traits<T>::type_to_check_recursively> {};
66 
67 template <>
68 struct is_comparable<recursive_bottom> : std::true_type {};
69 
70 /* For pairs, recursively check the two data types */
71 template <typename T>
75 };
76 
77 /* Fallback functions */
78 template <typename, typename, typename... Args>
79 void vector_if_copy_constructible(const Args &...) {}
80 template <typename, typename, typename... Args>
81 void vector_if_equal_operator(const Args &...) {}
82 template <typename, typename, typename... Args>
83 void vector_if_insertion_operator(const Args &...) {}
84 template <typename, typename, typename... Args>
85 void vector_modifiers(const Args &...) {}
86 
87 template <typename Vector, typename Class_>
89  cl.def(init<const Vector &>(), "Copy constructor");
90 }
91 
92 template <typename Vector, typename Class_>
94  using T = typename Vector::value_type;
95 
96  cl.def(self == self);
97  cl.def(self != self);
98 
99  cl.def(
100  "count",
101  [](const Vector &v, const T &x) { return std::count(v.begin(), v.end(), x); },
102  arg("x"),
103  "Return the number of times ``x`` appears in the list");
104 
105  cl.def(
106  "remove",
107  [](Vector &v, const T &x) {
108  auto p = std::find(v.begin(), v.end(), x);
109  if (p != v.end()) {
110  v.erase(p);
111  } else {
112  throw value_error();
113  }
114  },
115  arg("x"),
116  "Remove the first item from the list whose value is x. "
117  "It is an error if there is no such item.");
118 
119  cl.def(
120  "__contains__",
121  [](const Vector &v, const T &x) { return std::find(v.begin(), v.end(), x) != v.end(); },
122  arg("x"),
123  "Return true the container contains ``x``");
124 }
125 
126 // Vector modifiers -- requires a copyable vector_type:
127 // (Technically, some of these (pop and __delitem__) don't actually require copyability, but it
128 // seems silly to allow deletion but not insertion, so include them here too.)
129 template <typename Vector, typename Class_>
132  using T = typename Vector::value_type;
133  using SizeType = typename Vector::size_type;
134  using DiffType = typename Vector::difference_type;
135 
136  auto wrap_i = [](DiffType i, SizeType n) {
137  if (i < 0) {
138  i += n;
139  }
140  if (i < 0 || (SizeType) i >= n) {
141  throw index_error();
142  }
143  return i;
144  };
145 
146  cl.def(
147  "append",
148  [](Vector &v, const T &value) { v.push_back(value); },
149  arg("x"),
150  "Add an item to the end of the list");
151 
152  cl.def(init([](const iterable &it) {
153  auto v = std::unique_ptr<Vector>(new Vector());
154  v->reserve(len_hint(it));
155  for (handle h : it) {
156  v->push_back(h.cast<T>());
157  }
158  return v.release();
159  }));
160 
161  cl.def(
162  "clear", [](Vector &v) { v.clear(); }, "Clear the contents");
163 
164  cl.def(
165  "extend",
166  [](Vector &v, const Vector &src) { v.insert(v.end(), src.begin(), src.end()); },
167  arg("L"),
168  "Extend the list by appending all the items in the given list");
169 
170  cl.def(
171  "extend",
172  [](Vector &v, const iterable &it) {
173  const size_t old_size = v.size();
174  v.reserve(old_size + len_hint(it));
175  try {
176  for (handle h : it) {
177  v.push_back(h.cast<T>());
178  }
179  } catch (const cast_error &) {
180  v.erase(v.begin() + static_cast<typename Vector::difference_type>(old_size),
181  v.end());
182  try {
183  v.shrink_to_fit();
184  } catch (const std::exception &) {
185  // Do nothing
186  }
187  throw;
188  }
189  },
190  arg("L"),
191  "Extend the list by appending all the items in the given list");
192 
193  cl.def(
194  "insert",
195  [](Vector &v, DiffType i, const T &x) {
196  // Can't use wrap_i; i == v.size() is OK
197  if (i < 0) {
198  i += v.size();
199  }
200  if (i < 0 || (SizeType) i > v.size()) {
201  throw index_error();
202  }
203  v.insert(v.begin() + i, x);
204  },
205  arg("i"),
206  arg("x"),
207  "Insert an item at a given position.");
208 
209  cl.def(
210  "pop",
211  [](Vector &v) {
212  if (v.empty()) {
213  throw index_error();
214  }
215  T t = std::move(v.back());
216  v.pop_back();
217  return t;
218  },
219  "Remove and return the last item");
220 
221  cl.def(
222  "pop",
223  [wrap_i](Vector &v, DiffType i) {
224  i = wrap_i(i, v.size());
225  T t = std::move(v[(SizeType) i]);
226  v.erase(std::next(v.begin(), i));
227  return t;
228  },
229  arg("i"),
230  "Remove and return the item at index ``i``");
231 
232  cl.def("__setitem__", [wrap_i](Vector &v, DiffType i, const T &t) {
233  i = wrap_i(i, v.size());
234  v[(SizeType) i] = t;
235  });
236 
238  cl.def(
239  "__getitem__",
240  [](const Vector &v, const slice &slice) -> Vector * {
241  size_t start = 0, stop = 0, step = 0, slicelength = 0;
242 
243  if (!slice.compute(v.size(), &start, &stop, &step, &slicelength)) {
244  throw error_already_set();
245  }
246 
247  auto *seq = new Vector();
248  seq->reserve((size_t) slicelength);
249 
250  for (size_t i = 0; i < slicelength; ++i) {
251  seq->push_back(v[start]);
252  start += step;
253  }
254  return seq;
255  },
256  arg("s"),
257  "Retrieve list elements using a slice object");
258 
259  cl.def(
260  "__setitem__",
261  [](Vector &v, const slice &slice, const Vector &value) {
262  size_t start = 0, stop = 0, step = 0, slicelength = 0;
263  if (!slice.compute(v.size(), &start, &stop, &step, &slicelength)) {
264  throw error_already_set();
265  }
266 
267  if (slicelength != value.size()) {
268  throw std::runtime_error(
269  "Left and right hand size of slice assignment have different sizes!");
270  }
271 
272  for (size_t i = 0; i < slicelength; ++i) {
273  v[start] = value[i];
274  start += step;
275  }
276  },
277  "Assign list elements using a slice object");
278 
279  cl.def(
280  "__delitem__",
281  [wrap_i](Vector &v, DiffType i) {
282  i = wrap_i(i, v.size());
283  v.erase(v.begin() + i);
284  },
285  "Delete the list elements at index ``i``");
286 
287  cl.def(
288  "__delitem__",
289  [](Vector &v, const slice &slice) {
290  size_t start = 0, stop = 0, step = 0, slicelength = 0;
291 
292  if (!slice.compute(v.size(), &start, &stop, &step, &slicelength)) {
293  throw error_already_set();
294  }
295 
296  if (step == 1 && false) {
297  v.erase(v.begin() + (DiffType) start, v.begin() + DiffType(start + slicelength));
298  } else {
299  for (size_t i = 0; i < slicelength; ++i) {
300  v.erase(v.begin() + DiffType(start));
301  start += step - 1;
302  }
303  }
304  },
305  "Delete list elements using a slice object");
306 }
307 
308 // If the type has an operator[] that doesn't return a reference (most notably std::vector<bool>),
309 // we have to access by copying; otherwise we return by reference.
310 template <typename Vector>
311 using vector_needs_copy
312  = negation<std::is_same<decltype(std::declval<Vector>()[typename Vector::size_type()]),
313  typename Vector::value_type &>>;
314 
315 // The usual case: access and iterate by reference
316 template <typename Vector, typename Class_>
318  using T = typename Vector::value_type;
319  using SizeType = typename Vector::size_type;
320  using DiffType = typename Vector::difference_type;
321  using ItType = typename Vector::iterator;
322 
323  auto wrap_i = [](DiffType i, SizeType n) {
324  if (i < 0) {
325  i += n;
326  }
327  if (i < 0 || (SizeType) i >= n) {
328  throw index_error();
329  }
330  return i;
331  };
332 
333  cl.def(
334  "__getitem__",
335  [wrap_i](Vector &v, DiffType i) -> T & {
336  i = wrap_i(i, v.size());
337  return v[(SizeType) i];
338  },
339  return_value_policy::reference_internal // ref + keepalive
340  );
341 
342  cl.def(
343  "__iter__",
344  [](Vector &v) {
345  return make_iterator<return_value_policy::reference_internal, ItType, ItType, T &>(
346  v.begin(), v.end());
347  },
348  keep_alive<0, 1>() /* Essential: keep list alive while iterator exists */
349  );
350 }
351 
352 // The case for special objects, like std::vector<bool>, that have to be returned-by-copy:
353 template <typename Vector, typename Class_>
355  using T = typename Vector::value_type;
356  using SizeType = typename Vector::size_type;
357  using DiffType = typename Vector::difference_type;
358  using ItType = typename Vector::iterator;
359  cl.def("__getitem__", [](const Vector &v, DiffType i) -> T {
360  if (i < 0) {
361  i += v.size();
362  if (i < 0) {
363  throw index_error();
364  }
365  }
366  auto i_st = static_cast<SizeType>(i);
367  if (i_st >= v.size()) {
368  throw index_error();
369  }
370  return v[i_st];
371  });
372 
373  cl.def(
374  "__iter__",
375  [](Vector &v) {
376  return make_iterator<return_value_policy::copy, ItType, ItType, T>(v.begin(), v.end());
377  },
378  keep_alive<0, 1>() /* Essential: keep list alive while iterator exists */
379  );
380 }
381 
382 template <typename Vector, typename Class_>
383 auto vector_if_insertion_operator(Class_ &cl, std::string const &name)
384  -> decltype(std::declval<std::ostream &>() << std::declval<typename Vector::value_type>(),
385  void()) {
386  using size_type = typename Vector::size_type;
387 
388  cl.def(
389  "__repr__",
390  [name](Vector &v) {
391  std::ostringstream s;
392  s << name << '[';
393  for (size_type i = 0; i < v.size(); ++i) {
394  s << v[i];
395  if (i != v.size() - 1) {
396  s << ", ";
397  }
398  }
399  s << ']';
400  return s.str();
401  },
402  "Return the canonical string representation of this list.");
403 }
404 
405 // Provide the buffer interface for vectors if we have data() and we have a format for it
406 // GCC seems to have "void std::vector<bool>::data()" - doing SFINAE on the existence of data()
407 // is insufficient, we need to check it returns an appropriate pointer
408 template <typename Vector, typename = void>
409 struct vector_has_data_and_format : std::false_type {};
410 template <typename Vector>
412  Vector,
413  enable_if_t<std::is_same<decltype(format_descriptor<typename Vector::value_type>::format(),
414  std::declval<Vector>().data()),
415  typename Vector::value_type *>::value>> : std::true_type {};
416 
417 // [workaround(intel)] Separate function required here
418 // Workaround as the Intel compiler does not compile the enable_if_t part below
419 // (tested with icc (ICC) 2021.1 Beta 20200827)
420 template <typename... Args>
421 constexpr bool args_any_are_buffer() {
422  return detail::any_of<std::is_same<Args, buffer_protocol>...>::value;
423 }
424 
425 // [workaround(intel)] Separate function required here
426 // [workaround(msvc)] Can't use constexpr bool in return type
427 
428 // Add the buffer interface to a vector
429 template <typename Vector, typename Class_, typename... Args>
430 void vector_buffer_impl(Class_ &cl, std::true_type) {
431  using T = typename Vector::value_type;
432 
434  "There is not an appropriate format descriptor for this vector");
435 
436  // numpy.h declares this for arbitrary types, but it may raise an exception and crash hard
437  // at runtime if PYBIND11_NUMPY_DTYPE hasn't been called, so check here
439 
440  cl.def_buffer([](Vector &v) -> buffer_info {
441  return buffer_info(v.data(),
442  static_cast<ssize_t>(sizeof(T)),
444  1,
445  {v.size()},
446  {sizeof(T)});
447  });
448 
449  cl.def(init([](const buffer &buf) {
450  auto info = buf.request();
451  if (info.ndim != 1 || info.strides[0] % static_cast<ssize_t>(sizeof(T))) {
452  throw type_error("Only valid 1D buffers can be copied to a vector");
453  }
455  || (ssize_t) sizeof(T) != info.itemsize) {
456  throw type_error("Format mismatch (Python: " + info.format
457  + " C++: " + format_descriptor<T>::format() + ")");
458  }
459 
460  T *p = static_cast<T *>(info.ptr);
461  ssize_t step = info.strides[0] / static_cast<ssize_t>(sizeof(T));
462  T *end = p + info.shape[0] * step;
463  if (step == 1) {
464  return Vector(p, end);
465  }
466  Vector vec;
467  vec.reserve((size_t) info.shape[0]);
468  for (; p != end; p += step) {
469  vec.push_back(*p);
470  }
471  return vec;
472  }));
473 
474  return;
475 }
476 
477 template <typename Vector, typename Class_, typename... Args>
478 void vector_buffer_impl(Class_ &, std::false_type) {}
479 
480 template <typename Vector, typename Class_, typename... Args>
481 void vector_buffer(Class_ &cl) {
482  vector_buffer_impl<Vector, Class_, Args...>(
483  cl, detail::any_of<std::is_same<Args, buffer_protocol>...>{});
484 }
485 
487 
488 //
489 // std::vector
490 //
491 template <typename Vector, typename holder_type = std::unique_ptr<Vector>, typename... Args>
492 class_<Vector, holder_type> bind_vector(handle scope, std::string const &name, Args &&...args) {
493  using Class_ = class_<Vector, holder_type>;
494 
495  // If the value_type is unregistered (e.g. a converting type) or is itself registered
496  // module-local then make the vector binding module-local as well:
497  using vtype = typename Vector::value_type;
498  auto *vtype_info = detail::get_type_info(typeid(vtype));
499  bool local = !vtype_info || vtype_info->module_local;
500 
501  Class_ cl(scope, name.c_str(), pybind11::module_local(local), std::forward<Args>(args)...);
502 
503  // Declare the buffer interface if a buffer_protocol() is passed in
504  detail::vector_buffer<Vector, Class_, Args...>(cl);
505 
506  cl.def(init<>());
507 
508  // Register copy constructor (if possible)
509  detail::vector_if_copy_constructible<Vector, Class_>(cl);
510 
511  // Register comparison-related operators and functions (if possible)
512  detail::vector_if_equal_operator<Vector, Class_>(cl);
513 
514  // Register stream insertion operator (if possible)
515  detail::vector_if_insertion_operator<Vector, Class_>(cl, name);
516 
517  // Modifiers require copyable vector value type
518  detail::vector_modifiers<Vector, Class_>(cl);
519 
520  // Accessor and iterator; return by value if copyable, otherwise we return by ref + keep-alive
521  detail::vector_accessor<Vector, Class_>(cl);
522 
523  cl.def(
524  "__bool__",
525  [](const Vector &v) -> bool { return !v.empty(); },
526  "Check whether the list is nonempty");
527 
528  cl.def("__len__", &Vector::size);
529 
530 #if 0
531  // C++ style functions deprecated, leaving it here as an example
532  cl.def(init<size_type>());
533 
534  cl.def("resize",
535  (void (Vector::*) (size_type count)) & Vector::resize,
536  "changes the number of elements stored");
537 
538  cl.def("erase",
539  [](Vector &v, SizeType i) {
540  if (i >= v.size())
541  throw index_error();
542  v.erase(v.begin() + i);
543  }, "erases element at index ``i``");
544 
545  cl.def("empty", &Vector::empty, "checks whether the container is empty");
546  cl.def("size", &Vector::size, "returns the number of elements");
547  cl.def("push_back", (void (Vector::*)(const T&)) &Vector::push_back, "adds an element to the end");
548  cl.def("pop_back", &Vector::pop_back, "removes the last element");
549 
550  cl.def("max_size", &Vector::max_size, "returns the maximum possible number of elements");
551  cl.def("reserve", &Vector::reserve, "reserves storage");
552  cl.def("capacity", &Vector::capacity, "returns the number of elements that can be held in currently allocated storage");
553  cl.def("shrink_to_fit", &Vector::shrink_to_fit, "reduces memory usage by freeing unused memory");
554 
555  cl.def("clear", &Vector::clear, "clears the contents");
556  cl.def("swap", &Vector::swap, "swaps the contents");
557 
558  cl.def("front", [](Vector &v) {
559  if (v.size()) return v.front();
560  else throw index_error();
561  }, "access the first element");
562 
563  cl.def("back", [](Vector &v) {
564  if (v.size()) return v.back();
565  else throw index_error();
566  }, "access the last element ");
567 
568 #endif
569 
570  return cl;
571 }
572 
573 //
574 // std::map, std::unordered_map
575 //
576 
578 
579 /* Fallback functions */
580 template <typename, typename, typename... Args>
581 void map_if_insertion_operator(const Args &...) {}
582 template <typename, typename, typename... Args>
583 void map_assignment(const Args &...) {}
584 
585 // Map assignment when copy-assignable: just copy the value
586 template <typename Map, typename Class_>
589  using KeyType = typename Map::key_type;
590  using MappedType = typename Map::mapped_type;
591 
592  cl.def("__setitem__", [](Map &m, const KeyType &k, const MappedType &v) {
593  auto it = m.find(k);
594  if (it != m.end()) {
595  it->second = v;
596  } else {
597  m.emplace(k, v);
598  }
599  });
600 }
601 
602 // Not copy-assignable, but still copy-constructible: we can update the value by erasing and
603 // reinserting
604 template <typename Map, typename Class_>
607  Class_> &cl) {
608  using KeyType = typename Map::key_type;
609  using MappedType = typename Map::mapped_type;
610 
611  cl.def("__setitem__", [](Map &m, const KeyType &k, const MappedType &v) {
612  // We can't use m[k] = v; because value type might not be default constructable
613  auto r = m.emplace(k, v);
614  if (!r.second) {
615  // value type is not copy assignable so the only way to insert it is to erase it
616  // first...
617  m.erase(r.first);
618  m.emplace(k, v);
619  }
620  });
621 }
622 
623 template <typename Map, typename Class_>
624 auto map_if_insertion_operator(Class_ &cl, std::string const &name)
625  -> decltype(std::declval<std::ostream &>() << std::declval<typename Map::key_type>()
626  << std::declval<typename Map::mapped_type>(),
627  void()) {
628 
629  cl.def(
630  "__repr__",
631  [name](Map &m) {
632  std::ostringstream s;
633  s << name << '{';
634  bool f = false;
635  for (auto const &kv : m) {
636  if (f) {
637  s << ", ";
638  }
639  s << kv.first << ": " << kv.second;
640  f = true;
641  }
642  s << '}';
643  return s.str();
644  },
645  "Return the canonical string representation of this map.");
646 }
647 
648 template <typename KeyType>
649 struct keys_view {
650  virtual size_t len() = 0;
651  virtual iterator iter() = 0;
652  virtual bool contains(const KeyType &k) = 0;
653  virtual bool contains(const object &k) = 0;
654  virtual ~keys_view() = default;
655 };
656 
657 template <typename MappedType>
658 struct values_view {
659  virtual size_t len() = 0;
660  virtual iterator iter() = 0;
661  virtual ~values_view() = default;
662 };
663 
664 template <typename KeyType, typename MappedType>
665 struct items_view {
666  virtual size_t len() = 0;
667  virtual iterator iter() = 0;
668  virtual ~items_view() = default;
669 };
670 
671 template <typename Map, typename KeysView>
672 struct KeysViewImpl : public KeysView {
673  explicit KeysViewImpl(Map &map) : map(map) {}
674  size_t len() override { return map.size(); }
675  iterator iter() override { return make_key_iterator(map.begin(), map.end()); }
676  bool contains(const typename Map::key_type &k) override { return map.find(k) != map.end(); }
677  bool contains(const object &) override { return false; }
678  Map &map;
679 };
680 
681 template <typename Map, typename ValuesView>
682 struct ValuesViewImpl : public ValuesView {
683  explicit ValuesViewImpl(Map &map) : map(map) {}
684  size_t len() override { return map.size(); }
685  iterator iter() override { return make_value_iterator(map.begin(), map.end()); }
686  Map &map;
687 };
688 
689 template <typename Map, typename ItemsView>
690 struct ItemsViewImpl : public ItemsView {
691  explicit ItemsViewImpl(Map &map) : map(map) {}
692  size_t len() override { return map.size(); }
693  iterator iter() override { return make_iterator(map.begin(), map.end()); }
694  Map &map;
695 };
696 
698 
699 template <typename Map, typename holder_type = std::unique_ptr<Map>, typename... Args>
700 class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args &&...args) {
701  using KeyType = typename Map::key_type;
702  using MappedType = typename Map::mapped_type;
703  using StrippedKeyType = detail::remove_cvref_t<KeyType>;
704  using StrippedMappedType = detail::remove_cvref_t<MappedType>;
705  using KeysView = detail::keys_view<StrippedKeyType>;
706  using ValuesView = detail::values_view<StrippedMappedType>;
707  using ItemsView = detail::items_view<StrippedKeyType, StrippedMappedType>;
708  using Class_ = class_<Map, holder_type>;
709 
710  // If either type is a non-module-local bound type then make the map binding non-local as well;
711  // otherwise (e.g. both types are either module-local or converting) the map will be
712  // module-local.
713  auto *tinfo = detail::get_type_info(typeid(MappedType));
714  bool local = !tinfo || tinfo->module_local;
715  if (local) {
716  tinfo = detail::get_type_info(typeid(KeyType));
717  local = !tinfo || tinfo->module_local;
718  }
719 
720  Class_ cl(scope, name.c_str(), pybind11::module_local(local), std::forward<Args>(args)...);
721  static constexpr auto key_type_descr = detail::make_caster<KeyType>::name;
722  static constexpr auto mapped_type_descr = detail::make_caster<MappedType>::name;
723  std::string key_type_name(key_type_descr.text), mapped_type_name(mapped_type_descr.text);
724 
725  // If key type isn't properly wrapped, fall back to C++ names
726  if (key_type_name == "%") {
727  key_type_name = detail::type_info_description(typeid(KeyType));
728  }
729  // Similarly for value type:
730  if (mapped_type_name == "%") {
731  mapped_type_name = detail::type_info_description(typeid(MappedType));
732  }
733 
734  // Wrap KeysView[KeyType] if it wasn't already wrapped
735  if (!detail::get_type_info(typeid(KeysView))) {
737  scope, ("KeysView[" + key_type_name + "]").c_str(), pybind11::module_local(local));
738  keys_view.def("__len__", &KeysView::len);
739  keys_view.def("__iter__",
741  keep_alive<0, 1>() /* Essential: keep view alive while iterator exists */
742  );
743  keys_view.def("__contains__",
744  static_cast<bool (KeysView::*)(const KeyType &)>(&KeysView::contains));
745  // Fallback for when the object is not of the key type
746  keys_view.def("__contains__",
747  static_cast<bool (KeysView::*)(const object &)>(&KeysView::contains));
748  }
749  // Similarly for ValuesView:
750  if (!detail::get_type_info(typeid(ValuesView))) {
752  ("ValuesView[" + mapped_type_name + "]").c_str(),
753  pybind11::module_local(local));
754  values_view.def("__len__", &ValuesView::len);
755  values_view.def("__iter__",
757  keep_alive<0, 1>() /* Essential: keep view alive while iterator exists */
758  );
759  }
760  // Similarly for ItemsView:
761  if (!detail::get_type_info(typeid(ItemsView))) {
763  scope,
764  ("ItemsView[" + key_type_name + ", ").append(mapped_type_name + "]").c_str(),
765  pybind11::module_local(local));
766  items_view.def("__len__", &ItemsView::len);
767  items_view.def("__iter__",
769  keep_alive<0, 1>() /* Essential: keep view alive while iterator exists */
770  );
771  }
772 
773  cl.def(init<>());
774 
775  // Register stream insertion operator (if possible)
776  detail::map_if_insertion_operator<Map, Class_>(cl, name);
777 
778  cl.def(
779  "__bool__",
780  [](const Map &m) -> bool { return !m.empty(); },
781  "Check whether the map is nonempty");
782 
783  cl.def(
784  "__iter__",
785  [](Map &m) { return make_key_iterator(m.begin(), m.end()); },
786  keep_alive<0, 1>() /* Essential: keep map alive while iterator exists */
787  );
788 
789  cl.def(
790  "keys",
791  [](Map &m) {
792  return std::unique_ptr<KeysView>(new detail::KeysViewImpl<Map, KeysView>(m));
793  },
794  keep_alive<0, 1>() /* Essential: keep map alive while view exists */
795  );
796 
797  cl.def(
798  "values",
799  [](Map &m) {
800  return std::unique_ptr<ValuesView>(new detail::ValuesViewImpl<Map, ValuesView>(m));
801  },
802  keep_alive<0, 1>() /* Essential: keep map alive while view exists */
803  );
804 
805  cl.def(
806  "items",
807  [](Map &m) {
808  return std::unique_ptr<ItemsView>(new detail::ItemsViewImpl<Map, ItemsView>(m));
809  },
810  keep_alive<0, 1>() /* Essential: keep map alive while view exists */
811  );
812 
813  cl.def(
814  "__getitem__",
815  [](Map &m, const KeyType &k) -> MappedType & {
816  auto it = m.find(k);
817  if (it == m.end()) {
818  throw key_error();
819  }
820  return it->second;
821  },
822  return_value_policy::reference_internal // ref + keepalive
823  );
824 
825  cl.def("__contains__", [](Map &m, const KeyType &k) -> bool {
826  auto it = m.find(k);
827  if (it == m.end()) {
828  return false;
829  }
830  return true;
831  });
832  // Fallback for when the object is not of the key type
833  cl.def("__contains__", [](Map &, const object &) -> bool { return false; });
834 
835  // Assignment provided only if the type is copyable
836  detail::map_assignment<Map, Class_>(cl);
837 
838  cl.def("__delitem__", [](Map &m, const KeyType &k) {
839  auto it = m.find(k);
840  if (it == m.end()) {
841  throw key_error();
842  }
843  m.erase(it);
844  });
845 
846  cl.def("__len__", &Map::size);
847 
848  return cl;
849 }
850 
return_value_policy::reference_internal
@ reference_internal
compare
bool compare
Definition: SolverComparer.cpp:98
items_view::~items_view
virtual ~items_view()=default
format_descriptor
Definition: wrap/pybind11/include/pybind11/detail/common.h:1023
vector_if_insertion_operator
void vector_if_insertion_operator(const Args &...)
Definition: stl_bind.h:83
name
Annotation for function names.
Definition: attr.h:51
keys_view::len
virtual size_t len()=0
container_traits::test_comparable
static std::true_type test_comparable(decltype(std::declval< const T2 & >()==std::declval< const T2 & >()) *)
ItemsViewImpl
Definition: stl_bind.h:690
KeysViewImpl::contains
bool contains(const object &) override
Definition: stl_bind.h:677
cast.h
vector_if_copy_constructible
void vector_if_copy_constructible(const Args &...)
Definition: stl_bind.h:79
format
std::string format(const std::string &str, const std::vector< std::string > &find, const std::vector< std::string > &replace)
Definition: openglsupport.cpp:226
items_view
Definition: stl_bind.h:665
vector_if_equal_operator
void vector_if_equal_operator(const Args &...)
Definition: stl_bind.h:81
ssize_t
Py_ssize_t ssize_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:475
scope
Annotation for parent scope.
Definition: attr.h:39
resize
v resize(3)
error_already_set
Definition: pytypes.h:722
map_assignment
void map_assignment(const Args &...)
Definition: stl_bind.h:583
s
RealScalar s
Definition: level1_cplx_impl.h:126
ValuesViewImpl::map
Map & map
Definition: stl_bind.h:686
container_traits
Definition: stl_bind.h:26
keys_view::~keys_view
virtual ~keys_view()=default
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
items_view::iter
virtual iterator iter()=0
is_comparable
Definition: stl_bind.h:52
container_traits::test_pair
static std::true_type test_pair(typename T2::first_type *, typename T2::second_type *)
PYBIND11_NAMESPACE_END
#define PYBIND11_NAMESPACE_END(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:80
KeysViewImpl::KeysViewImpl
KeysViewImpl(Map &map)
Definition: stl_bind.h:673
vector_buffer
void vector_buffer(Class_ &cl)
Definition: stl_bind.h:481
detail
Definition: testSerializationNonlinear.cpp:70
ValuesViewImpl::iter
iterator iter() override
Definition: stl_bind.h:685
buffer
Definition: pytypes.h:2223
values_view::~values_view
virtual ~values_view()=default
PYBIND11_NAMESPACE_BEGIN
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:76
iterator
Definition: pytypes.h:1426
is_copy_assignable
Definition: type_caster_base.h:1009
h
const double h
Definition: testSimpleHelicopter.cpp:19
values_view::len
virtual size_t len()=0
type_info_description
PYBIND11_NOINLINE std::string type_info_description(const std::type_info &ti)
Definition: type_caster_base.h:1167
ItemsViewImpl::ItemsViewImpl
ItemsViewImpl(Map &map)
Definition: stl_bind.h:691
bind_vector
class_< Vector, holder_type > bind_vector(handle scope, std::string const &name, Args &&...args)
Definition: stl_bind.h:492
keys_view::iter
virtual iterator iter()=0
name
static char name[]
Definition: rgamma.c:72
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
ValuesViewImpl::len
size_t len() override
Definition: stl_bind.h:684
n
int n
Definition: BiCGSTAB_simple.cpp:1
slice
Definition: pytypes.h:1867
buffer::request
buffer_info request(bool writable=false) const
Definition: pytypes.h:2227
class_
Definition: pybind11.h:1496
handle
Definition: pytypes.h:217
ValuesViewImpl
Definition: stl_bind.h:682
vector_has_data_and_format
Definition: stl_bind.h:409
make_iterator
iterator make_iterator(Iterator first, Sentinel last, Extra &&...extra)
Makes a python iterator from a first and past-the-end C++ InputIterator.
Definition: pybind11.h:2409
ItemsViewImpl::len
size_t len() override
Definition: stl_bind.h:692
container_traits::is_vector
static constexpr const bool is_vector
Definition: stl_bind.h:46
KeysViewImpl::len
size_t len() override
Definition: stl_bind.h:674
keep_alive
Keep patient alive while nurse lives.
Definition: attr.h:73
vector_buffer_impl
void vector_buffer_impl(Class_ &cl, std::true_type)
Definition: stl_bind.h:430
slice::compute
bool compute(size_t length, size_t *start, size_t *stop, size_t *step, size_t *slicelength) const
Definition: pytypes.h:1886
negation
Definition: wrap/pybind11/include/pybind11/detail/common.h:699
gtsam::utils.visual_isam.step
def step(data, isam, result, truth, currPoseIndex, isamArgs=())
Definition: visual_isam.py:82
info
else if n * info
Definition: 3rdparty/Eigen/lapack/cholesky.cpp:18
get_type_info
PYBIND11_NOINLINE detail::type_info * get_type_info(PyTypeObject *type)
Definition: type_caster_base.h:184
make_value_iterator
iterator make_value_iterator(Iterator first, Sentinel last, Extra &&...extra)
Definition: pybind11.h:2441
cl
Definition: cxx11_tensor_builtins_sycl.cpp:30
vector_accessor
void vector_accessor(enable_if_t<!vector_needs_copy< Vector >::value, Class_ > &cl)
Definition: stl_bind.h:317
is_copy_constructible
Definition: type_caster_base.h:991
PYBIND11_NAMESPACE
Definition: test_custom_type_casters.cpp:24
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
Eigen::Triplet< double >
ItemsViewImpl::map
Map & map
Definition: stl_bind.h:694
arg
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE ArgReturnType arg() const
Definition: ArrayCwiseUnaryOps.h:66
map_if_insertion_operator
void map_if_insertion_operator(const Args &...)
Definition: stl_bind.h:581
common.h
KeysViewImpl::contains
bool contains(const typename Map::key_type &k) override
Definition: stl_bind.h:676
ItemsViewImpl::iter
iterator iter() override
Definition: stl_bind.h:693
init
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Definition: pybind11.h:1912
values_view
Definition: stl_bind.h:658
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
items_view::len
virtual size_t len()=0
bind_map
class_< Map, holder_type > bind_map(handle scope, const std::string &name, Args &&...args)
Definition: stl_bind.h:700
make_key_iterator
iterator make_key_iterator(Iterator first, Sentinel last, Extra &&...extra)
Definition: pybind11.h:2425
iter
iterator iter(handle obj)
Definition: pytypes.h:2428
iterable
Definition: pytypes.h:1517
container_traits::test_value
static std::true_type test_value(typename T2::value_type *)
std
Definition: BFloat16.h:88
args
Definition: pytypes.h:2163
KeysViewImpl
Definition: stl_bind.h:672
buffer_info
Information record describing a Python buffer object.
Definition: buffer_info.h:46
p
float * p
Definition: Tutorial_Map_using.cpp:9
c_str
const char * c_str(Args &&...args)
Definition: internals.h:599
KeysViewImpl::iter
iterator iter() override
Definition: stl_bind.h:675
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
if
if((m *x).isApprox(y))
Definition: FullPivLU_solve.cpp:6
KeysViewImpl::map
Map & map
Definition: stl_bind.h:678
len_hint
size_t len_hint(handle h)
Definition: pytypes.h:2409
keys_view
Definition: stl_bind.h:649
values_view::iter
virtual iterator iter()=0
container_traits::is_pair
static constexpr const bool is_pair
Definition: stl_bind.h:44
len
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2399
container_traits::is_element
static constexpr const bool is_element
Definition: stl_bind.h:47
recursive_bottom
Definition: type_caster_base.h:881
Eigen::placeholders::end
static const EIGEN_DEPRECATED end_t end
Definition: IndexedViewHelper.h:181
align_3::t
Point2 t(10, 10)
swap
int EIGEN_BLAS_FUNC() swap(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:130
init
Definition: TutorialInplaceLU.cpp:2
keys_view::contains
virtual bool contains(const KeyType &k)=0
ValuesViewImpl::ValuesViewImpl
ValuesViewImpl(Map &map)
Definition: stl_bind.h:683
Eigen::seq
internal::enable_if<!(symbolic::is_symbolic< FirstType >::value||symbolic::is_symbolic< LastType >::value), ArithmeticSequence< typename internal::cleanup_index_type< FirstType >::type, Index > >::type seq(FirstType f, LastType l)
Definition: ArithmeticSequence.h:234
ceres::Vector
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
Definition: gtsam/3rdparty/ceres/eigen.h:38
enable_if_t
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
Definition: wrap/pybind11/include/pybind11/detail/common.h:640
operators.h
test_callbacks.value
value
Definition: test_callbacks.py:158
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
vector_modifiers
void vector_modifiers(const Args &...)
Definition: stl_bind.h:85
args_any_are_buffer
constexpr bool args_any_are_buffer()
Definition: stl_bind.h:421
type_caster_base.h


gtsam
Author(s):
autogenerated on Thu Jun 13 2024 03:06:33