type_caster_base.h
Go to the documentation of this file.
1 /*
2  pybind11/detail/type_caster_base.h (originally first part of pybind11/cast.h)
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 "../pytypes.h"
13 #include "common.h"
14 #include "descr.h"
15 #include "internals.h"
16 #include "typeid.h"
17 
18 #include <cstdint>
19 #include <iterator>
20 #include <new>
21 #include <string>
22 #include <type_traits>
23 #include <typeindex>
24 #include <typeinfo>
25 #include <unordered_map>
26 #include <utility>
27 #include <vector>
28 
31 
32 class loader_life_support {
35 private:
37  std::unordered_set<PyObject *> keep_alive;
38 
39 #if defined(WITH_THREAD)
40  // Store stack pointer in thread-local storage.
41  static PYBIND11_TLS_KEY_REF get_stack_tls_key() {
42 # if PYBIND11_INTERNALS_VERSION == 4
43  return get_local_internals().loader_life_support_tls_key;
44 # else
45  return get_internals().loader_life_support_tls_key;
46 # endif
47  }
49  return static_cast<loader_life_support *>(PYBIND11_TLS_GET_VALUE(get_stack_tls_key()));
50  }
52  PYBIND11_TLS_REPLACE_VALUE(get_stack_tls_key(), value);
53  }
54 #else
55  // Use single global variable for stack.
57  static loader_life_support *global_stack = nullptr;
58  return global_stack;
59  }
62 #endif
63 
64 public:
67 
70  if (get_stack_top() != this) {
71  pybind11_fail("loader_life_support: internal error");
72  }
74  for (auto *item : keep_alive) {
75  Py_DECREF(item);
76  }
77  }
78 
83  if (!frame) {
84  // NOTE: It would be nice to include the stack frames here, as this indicates
85  // use of pybind11::cast<> outside the normal call framework, finding such
86  // a location is challenging. Developers could consider printing out
87  // stack frame addresses here using something like __builtin_frame_address(0)
88  throw cast_error("When called outside a bound function, py::cast() cannot "
89  "do Python -> C++ conversions which require the creation "
90  "of temporary values");
91  }
92 
93  if (frame->keep_alive.insert(h.ptr()).second) {
94  Py_INCREF(h.ptr());
95  }
96  }
97 };
98 
99 // Gets the cache entry for the given type, creating it if necessary. The return value is the pair
100 // returned by emplace, i.e. an iterator for the entry and a bool set to `true` if the entry was
101 // just created.
102 inline std::pair<decltype(internals::registered_types_py)::iterator, bool>
103 all_type_info_get_cache(PyTypeObject *type);
104 
105 // Populates a just-created cache entry.
106 PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector<type_info *> &bases) {
107  std::vector<PyTypeObject *> check;
108  for (handle parent : reinterpret_borrow<tuple>(t->tp_bases)) {
109  check.push_back((PyTypeObject *) parent.ptr());
110  }
111 
112  auto const &type_dict = get_internals().registered_types_py;
113  for (size_t i = 0; i < check.size(); i++) {
114  auto *type = check[i];
115  // Ignore Python2 old-style class super type:
116  if (!PyType_Check((PyObject *) type)) {
117  continue;
118  }
119 
120  // Check `type` in the current set of registered python types:
121  auto it = type_dict.find(type);
122  if (it != type_dict.end()) {
123  // We found a cache entry for it, so it's either pybind-registered or has pre-computed
124  // pybind bases, but we have to make sure we haven't already seen the type(s) before:
125  // we want to follow Python/virtual C++ rules that there should only be one instance of
126  // a common base.
127  for (auto *tinfo : it->second) {
128  // NB: Could use a second set here, rather than doing a linear search, but since
129  // having a large number of immediate pybind11-registered types seems fairly
130  // unlikely, that probably isn't worthwhile.
131  bool found = false;
132  for (auto *known : bases) {
133  if (known == tinfo) {
134  found = true;
135  break;
136  }
137  }
138  if (!found) {
139  bases.push_back(tinfo);
140  }
141  }
142  } else if (type->tp_bases) {
143  // It's some python type, so keep follow its bases classes to look for one or more
144  // registered types
145  if (i + 1 == check.size()) {
146  // When we're at the end, we can pop off the current element to avoid growing
147  // `check` when adding just one base (which is typical--i.e. when there is no
148  // multiple inheritance)
149  check.pop_back();
150  i--;
151  }
152  for (handle parent : reinterpret_borrow<tuple>(type->tp_bases)) {
153  check.push_back((PyTypeObject *) parent.ptr());
154  }
155  }
156  }
157 }
158 
169 inline const std::vector<detail::type_info *> &all_type_info(PyTypeObject *type) {
170  auto ins = all_type_info_get_cache(type);
171  if (ins.second) {
172  // New cache entry: populate it
173  all_type_info_populate(type, ins.first->second);
174  }
175 
176  return ins.first->second;
177 }
178 
184 PYBIND11_NOINLINE detail::type_info *get_type_info(PyTypeObject *type) {
185  const auto &bases = all_type_info(type);
186  if (bases.empty()) {
187  return nullptr;
188  }
189  if (bases.size() > 1) {
191  "pybind11::detail::get_type_info: type has multiple pybind11-registered bases");
192  }
193  return bases.front();
194 }
195 
196 inline detail::type_info *get_local_type_info(const std::type_index &tp) {
197  auto &locals = get_local_internals().registered_types_cpp;
198  auto it = locals.find(tp);
199  if (it != locals.end()) {
200  return it->second;
201  }
202  return nullptr;
203 }
204 
205 inline detail::type_info *get_global_type_info(const std::type_index &tp) {
206  auto &types = get_internals().registered_types_cpp;
207  auto it = types.find(tp);
208  if (it != types.end()) {
209  return it->second;
210  }
211  return nullptr;
212 }
213 
216 PYBIND11_NOINLINE detail::type_info *get_type_info(const std::type_index &tp,
217  bool throw_if_missing = false) {
218  if (auto *ltype = get_local_type_info(tp)) {
219  return ltype;
220  }
221  if (auto *gtype = get_global_type_info(tp)) {
222  return gtype;
223  }
224 
225  if (throw_if_missing) {
226  std::string tname = tp.name();
227  detail::clean_type_id(tname);
228  pybind11_fail("pybind11::detail::get_type_info: unable to find type info for \""
229  + std::move(tname) + '"');
230  }
231  return nullptr;
232 }
233 
234 PYBIND11_NOINLINE handle get_type_handle(const std::type_info &tp, bool throw_if_missing) {
235  detail::type_info *type_info = get_type_info(tp, throw_if_missing);
236  return handle(type_info ? ((PyObject *) type_info->type) : nullptr);
237 }
238 
239 // Searches the inheritance graph for a registered Python instance, using all_type_info().
241  const detail::type_info *tinfo) {
242  auto it_instances = get_internals().registered_instances.equal_range(src);
243  for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) {
244  for (auto *instance_type : detail::all_type_info(Py_TYPE(it_i->second))) {
245  if (instance_type && same_type(*instance_type->cpptype, *tinfo->cpptype)) {
246  return handle((PyObject *) it_i->second).inc_ref();
247  }
248  }
249  }
250  return handle();
251 }
252 
254  instance *inst = nullptr;
255  size_t index = 0u;
256  const detail::type_info *type = nullptr;
257  void **vh = nullptr;
258 
259  // Main constructor for a found value/holder:
260  value_and_holder(instance *i, const detail::type_info *type, size_t vpos, size_t index)
261  : inst{i}, index{index}, type{type},
263  : &inst->nonsimple.values_and_holders[vpos]} {}
264 
265  // Default constructor (used to signal a value-and-holder not found by get_value_and_holder())
266  value_and_holder() = default;
267 
268  // Used for past-the-end iterator
269  explicit value_and_holder(size_t index) : index{index} {}
270 
271  template <typename V = void>
272  V *&value_ptr() const {
273  return reinterpret_cast<V *&>(vh[0]);
274  }
275  // True if this `value_and_holder` has a non-null value pointer
276  explicit operator bool() const { return value_ptr() != nullptr; }
277 
278  template <typename H>
279  H &holder() const {
280  return reinterpret_cast<H &>(vh[1]);
281  }
282  bool holder_constructed() const {
283  return inst->simple_layout
286  }
287  // NOLINTNEXTLINE(readability-make-member-function-const)
288  void set_holder_constructed(bool v = true) {
289  if (inst->simple_layout) {
291  } else if (v) {
293  } else {
295  }
296  }
297  bool instance_registered() const {
298  return inst->simple_layout
301  }
302  // NOLINTNEXTLINE(readability-make-member-function-const)
303  void set_instance_registered(bool v = true) {
304  if (inst->simple_layout) {
306  } else if (v) {
308  } else {
310  }
311  }
312 };
313 
314 // Container for accessing and iterating over an instance's values/holders
316 private:
318  using type_vec = std::vector<detail::type_info *>;
319  const type_vec &tinfo;
320 
321 public:
323  : inst{inst}, tinfo(all_type_info(Py_TYPE(inst))) {}
324 
325  struct iterator {
326  private:
327  instance *inst = nullptr;
328  const type_vec *types = nullptr;
330  friend struct values_and_holders;
332  : inst{inst}, types{tinfo},
333  curr(inst /* instance */,
334  types->empty() ? nullptr : (*types)[0] /* type info */,
335  0, /* vpos: (non-simple types only): the first vptr comes first */
336  0 /* index */) {}
337  // Past-the-end iterator:
338  explicit iterator(size_t end) : curr(end) {}
339 
340  public:
341  bool operator==(const iterator &other) const { return curr.index == other.curr.index; }
342  bool operator!=(const iterator &other) const { return curr.index != other.curr.index; }
344  if (!inst->simple_layout) {
345  curr.vh += 1 + (*types)[curr.index]->holder_size_in_ptrs;
346  }
347  ++curr.index;
348  curr.type = curr.index < types->size() ? (*types)[curr.index] : nullptr;
349  return *this;
350  }
353  };
354 
355  iterator begin() { return iterator(inst, &tinfo); }
356  iterator end() { return iterator(tinfo.size()); }
357 
358  iterator find(const type_info *find_type) {
359  auto it = begin(), endit = end();
360  while (it != endit && it->type != find_type) {
361  ++it;
362  }
363  return it;
364  }
365 
366  size_t size() { return tinfo.size(); }
367 };
368 
380 instance::get_value_and_holder(const type_info *find_type /*= nullptr default in common.h*/,
381  bool throw_if_missing /*= true in common.h*/) {
382  // Optimize common case:
383  if (!find_type || Py_TYPE(this) == find_type->type) {
384  return value_and_holder(this, find_type, 0, 0);
385  }
386 
387  detail::values_and_holders vhs(this);
388  auto it = vhs.find(find_type);
389  if (it != vhs.end()) {
390  return *it;
391  }
392 
393  if (!throw_if_missing) {
394  return value_and_holder();
395  }
396 
397 #if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
398  pybind11_fail("pybind11::detail::instance::get_value_and_holder: `"
399  + get_fully_qualified_tp_name(find_type->type)
400  + "' is not a pybind11 base of the given `"
401  + get_fully_qualified_tp_name(Py_TYPE(this)) + "' instance");
402 #else
404  "pybind11::detail::instance::get_value_and_holder: "
405  "type is not a pybind11 base of the given instance "
406  "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for type details)");
407 #endif
408 }
409 
411  const auto &tinfo = all_type_info(Py_TYPE(this));
412 
413  const size_t n_types = tinfo.size();
414 
415  if (n_types == 0) {
417  "instance allocation failed: new instance has no pybind11-registered base types");
418  }
419 
421  = n_types == 1 && tinfo.front()->holder_size_in_ptrs <= instance_simple_holder_in_ptrs();
422 
423  // Simple path: no python-side multiple inheritance, and a small-enough holder
424  if (simple_layout) {
425  simple_value_holder[0] = nullptr;
428  } else { // multiple base types or a too-large holder
429  // Allocate space to hold: [v1*][h1][v2*][h2]...[bb...] where [vN*] is a value pointer,
430  // [hN] is the (uninitialized) holder instance for value N, and [bb...] is a set of bool
431  // values that tracks whether each associated holder has been initialized. Each [block] is
432  // padded, if necessary, to an integer multiple of sizeof(void *).
433  size_t space = 0;
434  for (auto *t : tinfo) {
435  space += 1; // value pointer
436  space += t->holder_size_in_ptrs; // holder instance
437  }
438  size_t flags_at = space;
439  space += size_in_ptrs(n_types); // status bytes (holder_constructed and
440  // instance_registered)
441 
442  // Allocate space for flags, values, and holders, and initialize it to 0 (flags and values,
443  // in particular, need to be 0). Use Python's memory allocation
444  // functions: Python is using pymalloc, which is designed to be
445  // efficient for small allocations like the one we're doing here;
446  // for larger allocations they are just wrappers around malloc.
447  // TODO: is this still true for pure Python 3.6?
448  nonsimple.values_and_holders = (void **) PyMem_Calloc(space, sizeof(void *));
450  throw std::bad_alloc();
451  }
453  = reinterpret_cast<std::uint8_t *>(&nonsimple.values_and_holders[flags_at]);
454  }
455  owned = true;
456 }
457 
458 // NOLINTNEXTLINE(readability-make-member-function-const)
460  if (!simple_layout) {
461  PyMem_Free(nonsimple.values_and_holders);
462  }
463 }
464 
465 PYBIND11_NOINLINE bool isinstance_generic(handle obj, const std::type_info &tp) {
466  handle type = detail::get_type_handle(tp, false);
467  if (!type) {
468  return false;
469  }
470  return isinstance(obj, type);
471 }
472 
473 PYBIND11_NOINLINE handle get_object_handle(const void *ptr, const detail::type_info *type) {
474  auto &instances = get_internals().registered_instances;
475  auto range = instances.equal_range(ptr);
476  for (auto it = range.first; it != range.second; ++it) {
477  for (const auto &vh : values_and_holders(it->second)) {
478  if (vh.type == type) {
479  return handle((PyObject *) it->second);
480  }
481  }
482  }
483  return handle();
484 }
485 
486 inline PyThreadState *get_thread_state_unchecked() {
487 #if defined(PYPY_VERSION)
488  return PyThreadState_GET();
489 #else
490  return _PyThreadState_UncheckedGet();
491 #endif
492 }
493 
494 // Forward declarations
495 void keep_alive_impl(handle nurse, handle patient);
496 inline PyObject *make_new_instance(PyTypeObject *type);
497 
499 public:
500  PYBIND11_NOINLINE explicit type_caster_generic(const std::type_info &type_info)
502 
504  : typeinfo(typeinfo), cpptype(typeinfo ? typeinfo->cpptype : nullptr) {}
505 
506  bool load(handle src, bool convert) { return load_impl<type_caster_generic>(src, convert); }
507 
508  PYBIND11_NOINLINE static handle cast(const void *_src,
509  return_value_policy policy,
510  handle parent,
511  const detail::type_info *tinfo,
512  void *(*copy_constructor)(const void *),
513  void *(*move_constructor)(const void *),
514  const void *existing_holder = nullptr) {
515  if (!tinfo) { // no type info: error will be set already
516  return handle();
517  }
518 
519  void *src = const_cast<void *>(_src);
520  if (src == nullptr) {
521  return none().release();
522  }
523 
524  if (handle registered_inst = find_registered_python_instance(src, tinfo)) {
525  return registered_inst;
526  }
527 
528  auto inst = reinterpret_steal<object>(make_new_instance(tinfo->type));
529  auto *wrapper = reinterpret_cast<instance *>(inst.ptr());
530  wrapper->owned = false;
531  void *&valueptr = values_and_holders(wrapper).begin()->value_ptr();
532 
533  switch (policy) {
536  valueptr = src;
537  wrapper->owned = true;
538  break;
539 
542  valueptr = src;
543  wrapper->owned = false;
544  break;
545 
547  if (copy_constructor) {
548  valueptr = copy_constructor(src);
549  } else {
550 #if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
551  std::string type_name(tinfo->cpptype->name());
553  throw cast_error("return_value_policy = copy, but type " + type_name
554  + " is non-copyable!");
555 #else
556  throw cast_error("return_value_policy = copy, but type is "
557  "non-copyable! (#define PYBIND11_DETAILED_ERROR_MESSAGES or "
558  "compile in debug mode for details)");
559 #endif
560  }
561  wrapper->owned = true;
562  break;
563 
565  if (move_constructor) {
566  valueptr = move_constructor(src);
567  } else if (copy_constructor) {
568  valueptr = copy_constructor(src);
569  } else {
570 #if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
571  std::string type_name(tinfo->cpptype->name());
573  throw cast_error("return_value_policy = move, but type " + type_name
574  + " is neither movable nor copyable!");
575 #else
576  throw cast_error("return_value_policy = move, but type is neither "
577  "movable nor copyable! "
578  "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in "
579  "debug mode for details)");
580 #endif
581  }
582  wrapper->owned = true;
583  break;
584 
586  valueptr = src;
587  wrapper->owned = false;
588  keep_alive_impl(inst, parent);
589  break;
590 
591  default:
592  throw cast_error("unhandled return_value_policy: should not happen!");
593  }
594 
595  tinfo->init_instance(wrapper, existing_holder);
596 
597  return inst.release();
598  }
599 
600  // Base methods for generic caster; there are overridden in copyable_holder_caster
602  auto *&vptr = v_h.value_ptr();
603  // Lazy allocation for unallocated values:
604  if (vptr == nullptr) {
605  const auto *type = v_h.type ? v_h.type : typeinfo;
606  if (type->operator_new) {
607  vptr = type->operator_new(type->type_size);
608  } else {
609 #if defined(__cpp_aligned_new) && (!defined(_MSC_VER) || _MSC_VER >= 1912)
610  if (type->type_align > __STDCPP_DEFAULT_NEW_ALIGNMENT__) {
611  vptr = ::operator new(type->type_size, std::align_val_t(type->type_align));
612  } else {
613  vptr = ::operator new(type->type_size);
614  }
615 #else
616  vptr = ::operator new(type->type_size);
617 #endif
618  }
619  }
620  value = vptr;
621  }
623  for (const auto &cast : typeinfo->implicit_casts) {
624  type_caster_generic sub_caster(*cast.first);
625  if (sub_caster.load(src, convert)) {
626  value = cast.second(sub_caster.value);
627  return true;
628  }
629  }
630  return false;
631  }
633  for (auto &converter : *typeinfo->direct_conversions) {
634  if (converter(src.ptr(), value)) {
635  return true;
636  }
637  }
638  return false;
639  }
641 
642  PYBIND11_NOINLINE static void *local_load(PyObject *src, const type_info *ti) {
643  auto caster = type_caster_generic(ti);
644  if (caster.load(src, false)) {
645  return caster.value;
646  }
647  return nullptr;
648  }
649 
653  constexpr auto *local_key = PYBIND11_MODULE_LOCAL_ID;
654  const auto pytype = type::handle_of(src);
655  if (!hasattr(pytype, local_key)) {
656  return false;
657  }
658 
659  type_info *foreign_typeinfo = reinterpret_borrow<capsule>(getattr(pytype, local_key));
660  // Only consider this foreign loader if actually foreign and is a loader of the correct cpp
661  // type
662  if (foreign_typeinfo->module_local_load == &local_load
663  || (cpptype && !same_type(*cpptype, *foreign_typeinfo->cpptype))) {
664  return false;
665  }
666 
667  if (auto *result = foreign_typeinfo->module_local_load(src.ptr(), foreign_typeinfo)) {
668  value = result;
669  return true;
670  }
671  return false;
672  }
673 
674  // Implementation of `load`; this takes the type of `this` so that it can dispatch the relevant
675  // bits of code between here and copyable_holder_caster where the two classes need different
676  // logic (without having to resort to virtual inheritance).
677  template <typename ThisT>
679  if (!src) {
680  return false;
681  }
682  if (!typeinfo) {
683  return try_load_foreign_module_local(src);
684  }
685 
686  auto &this_ = static_cast<ThisT &>(*this);
687  this_.check_holder_compat();
688 
689  PyTypeObject *srctype = Py_TYPE(src.ptr());
690 
691  // Case 1: If src is an exact type match for the target type then we can reinterpret_cast
692  // the instance's value pointer to the target type:
693  if (srctype == typeinfo->type) {
694  this_.load_value(reinterpret_cast<instance *>(src.ptr())->get_value_and_holder());
695  return true;
696  }
697  // Case 2: We have a derived class
698  if (PyType_IsSubtype(srctype, typeinfo->type)) {
699  const auto &bases = all_type_info(srctype);
700  bool no_cpp_mi = typeinfo->simple_type;
701 
702  // Case 2a: the python type is a Python-inherited derived class that inherits from just
703  // one simple (no MI) pybind11 class, or is an exact match, so the C++ instance is of
704  // the right type and we can use reinterpret_cast.
705  // (This is essentially the same as case 2b, but because not using multiple inheritance
706  // is extremely common, we handle it specially to avoid the loop iterator and type
707  // pointer lookup overhead)
708  if (bases.size() == 1 && (no_cpp_mi || bases.front()->type == typeinfo->type)) {
709  this_.load_value(reinterpret_cast<instance *>(src.ptr())->get_value_and_holder());
710  return true;
711  }
712  // Case 2b: the python type inherits from multiple C++ bases. Check the bases to see
713  // if we can find an exact match (or, for a simple C++ type, an inherited match); if
714  // so, we can safely reinterpret_cast to the relevant pointer.
715  if (bases.size() > 1) {
716  for (auto *base : bases) {
717  if (no_cpp_mi ? PyType_IsSubtype(base->type, typeinfo->type)
718  : base->type == typeinfo->type) {
719  this_.load_value(
720  reinterpret_cast<instance *>(src.ptr())->get_value_and_holder(base));
721  return true;
722  }
723  }
724  }
725 
726  // Case 2c: C++ multiple inheritance is involved and we couldn't find an exact type
727  // match in the registered bases, above, so try implicit casting (needed for proper C++
728  // casting when MI is involved).
729  if (this_.try_implicit_casts(src, convert)) {
730  return true;
731  }
732  }
733 
734  // Perform an implicit conversion
735  if (convert) {
736  for (const auto &converter : typeinfo->implicit_conversions) {
737  auto temp = reinterpret_steal<object>(converter(src.ptr(), typeinfo->type));
738  if (load_impl<ThisT>(temp, false)) {
740  return true;
741  }
742  }
743  if (this_.try_direct_conversions(src)) {
744  return true;
745  }
746  }
747 
748  // Failed to match local typeinfo. Try again with global.
749  if (typeinfo->module_local) {
750  if (auto *gtype = get_global_type_info(*typeinfo->cpptype)) {
751  typeinfo = gtype;
752  return load(src, false);
753  }
754  }
755 
756  // Global typeinfo has precedence over foreign module_local
758  return true;
759  }
760 
761  // Custom converters didn't take None, now we convert None to nullptr.
762  if (src.is_none()) {
763  // Defer accepting None to other overloads (if we aren't in convert mode):
764  if (!convert) {
765  return false;
766  }
767  value = nullptr;
768  return true;
769  }
770 
771  return false;
772  }
773 
774  // Called to do type lookup and wrap the pointer and type in a pair when a dynamic_cast
775  // isn't needed or can't be used. If the type is unknown, sets the error and returns a pair
776  // with .second = nullptr. (p.first = nullptr is not an error: it becomes None).
777  PYBIND11_NOINLINE static std::pair<const void *, const type_info *>
778  src_and_type(const void *src,
779  const std::type_info &cast_type,
780  const std::type_info *rtti_type = nullptr) {
781  if (auto *tpi = get_type_info(cast_type)) {
782  return {src, const_cast<const type_info *>(tpi)};
783  }
784 
785  // Not found, set error:
786  std::string tname = rtti_type ? rtti_type->name() : cast_type.name();
787  detail::clean_type_id(tname);
788  std::string msg = "Unregistered type : " + tname;
789  PyErr_SetString(PyExc_TypeError, msg.c_str());
790  return {nullptr, nullptr};
791  }
792 
793  const type_info *typeinfo = nullptr;
794  const std::type_info *cpptype = nullptr;
795  void *value = nullptr;
796 };
797 
805 template <typename T>
807  typename std::add_pointer<intrinsic_t<T>>::type,
808  typename std::add_lvalue_reference<intrinsic_t<T>>::type>;
809 
817 template <typename T>
820  typename std::add_pointer<intrinsic_t<T>>::type,
822  typename std::add_rvalue_reference<intrinsic_t<T>>::type,
823  typename std::add_lvalue_reference<intrinsic_t<T>>::type>>;
824 
825 // Does the container have a mapped type and is it recursive?
826 // Implemented by specializations below.
827 template <typename Container, typename SFINAE = void>
829  static constexpr bool has_mapped_type = false;
830  static constexpr bool has_recursive_mapped_type = false;
831 };
832 
833 template <typename Container>
835  Container,
836  typename std::enable_if<
837  std::is_same<typename Container::mapped_type, Container>::value>::type> {
838  static constexpr bool has_mapped_type = true;
839  static constexpr bool has_recursive_mapped_type = true;
840 };
841 
842 template <typename Container>
844  Container,
845  typename std::enable_if<
846  negation<std::is_same<typename Container::mapped_type, Container>>::value>::type> {
847  static constexpr bool has_mapped_type = true;
848  static constexpr bool has_recursive_mapped_type = false;
849 };
850 
851 // Does the container have a value type and is it recursive?
852 // Implemented by specializations below.
853 template <typename Container, typename SFINAE = void>
854 struct container_value_type_traits : std::false_type {
855  static constexpr bool has_value_type = false;
856  static constexpr bool has_recursive_value_type = false;
857 };
858 
859 template <typename Container>
861  Container,
862  typename std::enable_if<
863  std::is_same<typename Container::value_type, Container>::value>::type> {
864  static constexpr bool has_value_type = true;
865  static constexpr bool has_recursive_value_type = true;
866 };
867 
868 template <typename Container>
870  Container,
871  typename std::enable_if<
872  negation<std::is_same<typename Container::value_type, Container>>::value>::type> {
873  static constexpr bool has_value_type = true;
874  static constexpr bool has_recursive_value_type = false;
875 };
876 
877 /*
878  * Tag to be used for representing the bottom of recursively defined types.
879  * Define this tag so we don't have to use void.
880  */
882 
883 /*
884  * Implementation detail of `recursive_container_traits` below.
885  * `T` is the `value_type` of the container, which might need to be modified to
886  * avoid recursive types and const types.
887  */
888 template <typename T, bool is_this_a_map>
890  /*
891  * If the container is recursive, then no further recursion should be done.
892  */
894  /*
895  * Otherwise yield `T` unchanged.
896  */
898 };
899 
900 /*
901  * For pairs - only as value type of a map -, the first type should remove the `const`.
902  * Also, if the map is recursive, then the recursive checking should consider
903  * the first type only.
904  */
905 template <typename A, typename B>
906 struct impl_type_to_check_recursively<std::pair<A, B>, /* is_this_a_map = */ true> {
909 };
910 
911 /*
912  * Implementation of `recursive_container_traits` below.
913  */
914 template <typename Container, typename SFINAE = void>
917 };
918 
919 template <typename Container>
921  Container,
922  typename std::enable_if<container_value_type_traits<Container>::has_value_type>::type> {
923  static constexpr bool is_recursive
926  /*
927  * This member dictates which type Pybind11 should check recursively in traits
928  * such as `is_move_constructible`, `is_copy_constructible`, `is_move_assignable`, ...
929  * Direct access to `value_type` should be avoided:
930  * 1. `value_type` might recursively contain the type again
931  * 2. `value_type` of STL map types is `std::pair<A const, B>`, the `const`
932  * should be removed.
933  *
934  */
935  using type_to_check_recursively = typename std::conditional<
936  is_recursive,
938  typename Container::value_type,
941  typename Container::value_type,
943 };
944 
945 /*
946  * This trait defines the `type_to_check_recursively` which is needed to properly
947  * handle recursively defined traits such as `is_move_constructible` without going
948  * into an infinite recursion.
949  * Should be used instead of directly accessing the `value_type`.
950  * It cancels the recursion by returning the `recursive_bottom` tag.
951  *
952  * The default definition of `type_to_check_recursively` is as follows:
953  *
954  * 1. By default, it is `recursive_bottom`, so that the recursion is canceled.
955  * 2. If the type is non-recursive and defines a `value_type`, then the `value_type` is used.
956  * If the `value_type` is a pair and a `mapped_type` is defined,
957  * then the `const` is removed from the first type.
958  * 3. If the type is recursive and `value_type` is not a pair, then `recursive_bottom` is returned.
959  * 4. If the type is recursive and `value_type` is a pair and a `mapped_type` is defined,
960  * then `const` is removed from the first type and the first type is returned.
961  *
962  * This behavior can be extended by the user as seen in test_stl_binders.cpp.
963  *
964  * This struct is exactly the same as impl_recursive_container_traits.
965  * The duplication achieves that user-defined specializations don't compete
966  * with internal specializations, but take precedence.
967  */
968 template <typename Container, typename SFINAE = void>
970 
971 template <typename T>
973  : all_of<std::is_move_constructible<T>,
974  is_move_constructible<
975  typename recursive_container_traits<T>::type_to_check_recursively>> {};
976 
977 template <>
978 struct is_move_constructible<recursive_bottom> : std::true_type {};
979 
980 // Likewise for std::pair
981 // (after C++17 it is mandatory that the move constructor not exist when the two types aren't
982 // themselves move constructible, but this can not be relied upon when T1 or T2 are themselves
983 // containers).
984 template <typename T1, typename T2>
985 struct is_move_constructible<std::pair<T1, T2>>
986  : all_of<is_move_constructible<T1>, is_move_constructible<T2>> {};
987 
988 // std::is_copy_constructible isn't quite enough: it lets std::vector<T> (and similar) through when
989 // T is non-copyable, but code containing such a copy constructor fails to actually compile.
990 template <typename T>
992  : all_of<std::is_copy_constructible<T>,
993  is_copy_constructible<
994  typename recursive_container_traits<T>::type_to_check_recursively>> {};
995 
996 template <>
997 struct is_copy_constructible<recursive_bottom> : std::true_type {};
998 
999 // Likewise for std::pair
1000 // (after C++17 it is mandatory that the copy constructor not exist when the two types aren't
1001 // themselves copy constructible, but this can not be relied upon when T1 or T2 are themselves
1002 // containers).
1003 template <typename T1, typename T2>
1004 struct is_copy_constructible<std::pair<T1, T2>>
1005  : all_of<is_copy_constructible<T1>, is_copy_constructible<T2>> {};
1006 
1007 // The same problems arise with std::is_copy_assignable, so we use the same workaround.
1008 template <typename T>
1010  : all_of<
1011  std::is_copy_assignable<T>,
1012  is_copy_assignable<typename recursive_container_traits<T>::type_to_check_recursively>> {
1013 };
1014 
1015 template <>
1016 struct is_copy_assignable<recursive_bottom> : std::true_type {};
1017 
1018 template <typename T1, typename T2>
1019 struct is_copy_assignable<std::pair<T1, T2>>
1020  : all_of<is_copy_assignable<T1>, is_copy_assignable<T2>> {};
1021 
1023 
1024 // polymorphic_type_hook<itype>::get(src, tinfo) determines whether the object pointed
1025 // to by `src` actually is an instance of some class derived from `itype`.
1026 // If so, it sets `tinfo` to point to the std::type_info representing that derived
1027 // type, and returns a pointer to the start of the most-derived object of that type
1028 // (in which `src` is a subobject; this will be the same address as `src` in most
1029 // single inheritance cases). If not, or if `src` is nullptr, it simply returns `src`
1030 // and leaves `tinfo` at its default value of nullptr.
1031 //
1032 // The default polymorphic_type_hook just returns src. A specialization for polymorphic
1033 // types determines the runtime type of the passed object and adjusts the this-pointer
1034 // appropriately via dynamic_cast<void*>. This is what enables a C++ Animal* to appear
1035 // to Python as a Dog (if Dog inherits from Animal, Animal is polymorphic, Dog is
1036 // registered with pybind11, and this Animal is in fact a Dog).
1037 //
1038 // You may specialize polymorphic_type_hook yourself for types that want to appear
1039 // polymorphic to Python but do not use C++ RTTI. (This is a not uncommon pattern
1040 // in performance-sensitive applications, used most notably in LLVM.)
1041 //
1042 // polymorphic_type_hook_base allows users to specialize polymorphic_type_hook with
1043 // std::enable_if. User provided specializations will always have higher priority than
1044 // the default implementation and specialization provided in polymorphic_type_hook_base.
1045 template <typename itype, typename SFINAE = void>
1047  static const void *get(const itype *src, const std::type_info *&) { return src; }
1048 };
1049 template <typename itype>
1050 struct polymorphic_type_hook_base<itype, detail::enable_if_t<std::is_polymorphic<itype>::value>> {
1051  static const void *get(const itype *src, const std::type_info *&type) {
1052  type = src ? &typeid(*src) : nullptr;
1053  return dynamic_cast<const void *>(src);
1054  }
1055 };
1056 template <typename itype, typename SFINAE = void>
1058 
1060 
1061 template <typename type>
1065 
1066 public:
1067  static constexpr auto name = const_name<type>();
1068 
1070  explicit type_caster_base(const std::type_info &info) : type_caster_generic(info) {}
1071 
1072  static handle cast(const itype &src, return_value_policy policy, handle parent) {
1073  if (policy == return_value_policy::automatic
1075  policy = return_value_policy::copy;
1076  }
1077  return cast(&src, policy, parent);
1078  }
1079 
1080  static handle cast(itype &&src, return_value_policy, handle parent) {
1081  return cast(&src, return_value_policy::move, parent);
1082  }
1083 
1084  // Returns a (pointer, type_info) pair taking care of necessary type lookup for a
1085  // polymorphic type (using RTTI by default, but can be overridden by specializing
1086  // polymorphic_type_hook). If the instance isn't derived, returns the base version.
1087  static std::pair<const void *, const type_info *> src_and_type(const itype *src) {
1088  const auto &cast_type = typeid(itype);
1089  const std::type_info *instance_type = nullptr;
1090  const void *vsrc = polymorphic_type_hook<itype>::get(src, instance_type);
1091  if (instance_type && !same_type(cast_type, *instance_type)) {
1092  // This is a base pointer to a derived type. If the derived type is registered
1093  // with pybind11, we want to make the full derived object available.
1094  // In the typical case where itype is polymorphic, we get the correct
1095  // derived pointer (which may be != base pointer) by a dynamic_cast to
1096  // most derived type. If itype is not polymorphic, we won't get here
1097  // except via a user-provided specialization of polymorphic_type_hook,
1098  // and the user has promised that no this-pointer adjustment is
1099  // required in that case, so it's OK to use static_cast.
1100  if (const auto *tpi = get_type_info(*instance_type)) {
1101  return {vsrc, tpi};
1102  }
1103  }
1104  // Otherwise we have either a nullptr, an `itype` pointer, or an unknown derived pointer,
1105  // so don't do a cast
1106  return type_caster_generic::src_and_type(src, cast_type, instance_type);
1107  }
1108 
1109  static handle cast(const itype *src, return_value_policy policy, handle parent) {
1110  auto st = src_and_type(src);
1111  return type_caster_generic::cast(st.first,
1112  policy,
1113  parent,
1114  st.second,
1115  make_copy_constructor(src),
1116  make_move_constructor(src));
1117  }
1118 
1119  static handle cast_holder(const itype *src, const void *holder) {
1120  auto st = src_and_type(src);
1121  return type_caster_generic::cast(st.first,
1123  {},
1124  st.second,
1125  nullptr,
1126  nullptr,
1127  holder);
1128  }
1129 
1130  template <typename T>
1131  using cast_op_type = detail::cast_op_type<T>;
1132 
1133  // NOLINTNEXTLINE(google-explicit-constructor)
1134  operator itype *() { return (type *) value; }
1135  // NOLINTNEXTLINE(google-explicit-constructor)
1136  operator itype &() {
1137  if (!value) {
1138  throw reference_cast_error();
1139  }
1140  return *((itype *) value);
1141  }
1142 
1143 protected:
1144  using Constructor = void *(*) (const void *);
1145 
1146  /* Only enabled when the types are {copy,move}-constructible *and* when the type
1147  does not have a private operator new implementation. A comma operator is used in the
1148  decltype argument to apply SFINAE to the public copy/move constructors.*/
1150  static auto make_copy_constructor(const T *)
1151  -> decltype(new T(std::declval<const T>()), Constructor{}) {
1152  return [](const void *arg) -> void * { return new T(*reinterpret_cast<const T *>(arg)); };
1153  }
1154 
1156  static auto make_move_constructor(const T *)
1157  -> decltype(new T(std::declval<T &&>()), Constructor{}) {
1158  return [](const void *arg) -> void * {
1159  return new T(std::move(*const_cast<T *>(reinterpret_cast<const T *>(arg))));
1160  };
1161  }
1162 
1163  static Constructor make_copy_constructor(...) { return nullptr; }
1164  static Constructor make_move_constructor(...) { return nullptr; }
1165 };
1166 
1167 PYBIND11_NOINLINE std::string type_info_description(const std::type_info &ti) {
1168  if (auto *type_data = get_type_info(ti)) {
1169  handle th((PyObject *) type_data->type);
1170  return th.attr("__module__").cast<std::string>() + '.'
1171  + th.attr("__qualname__").cast<std::string>();
1172  }
1173  return clean_type_id(ti.name());
1174 }
1175 
return_value_policy::reference_internal
@ reference_internal
loader_life_support::get_stack_pp
static loader_life_support ** get_stack_pp()
Definition: type_caster_base.h:56
H
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 y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate set rrange[*:*] noreverse nowriteback set trange[*:*] noreverse nowriteback set urange[*:*] noreverse nowriteback set vrange[*:*] noreverse nowriteback set xlabel matrix size set x2label set timefmt d m y n H
Definition: gnuplot_common_settings.hh:74
values_and_holders::tinfo
const type_vec & tinfo
Definition: type_caster_base.h:319
return_value_policy::move
@ move
all_of
std::is_same< bools< Ts::value..., true >, bools< true, Ts::value... > > all_of
Definition: wrap/pybind11/include/pybind11/detail/common.h:726
type_caster_generic::try_direct_conversions
bool try_direct_conversions(handle src)
Definition: type_caster_base.h:632
PYBIND11_MODULE_LOCAL_ID
#define PYBIND11_MODULE_LOCAL_ID
Definition: internals.h:315
polymorphic_type_hook_base::get
static const void * get(const itype *src, const std::type_info *&)
Definition: type_caster_base.h:1047
type_caster_base< T >::itype
intrinsic_t< T > itype
Definition: type_caster_base.h:1064
name
Annotation for function names.
Definition: attr.h:51
type_caster_base::cast
static handle cast(itype &&src, return_value_policy, handle parent)
Definition: type_caster_base.h:1080
type_info::module_local_load
void *(* module_local_load)(PyObject *, const type_info *)
Definition: internals.h:238
value_and_holder::value_and_holder
value_and_holder(size_t index)
Definition: type_caster_base.h:269
base
Annotation indicating that a class derives from another given type.
Definition: attr.h:64
loader_life_support::keep_alive
std::unordered_set< PyObject * > keep_alive
Definition: type_caster_base.h:37
type_info::implicit_conversions
std::vector< PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions
Definition: internals.h:233
gtsam.examples.DogLegOptimizerExample.type
type
Definition: DogLegOptimizerExample.py:111
instance::simple_layout
bool simple_layout
Definition: wrap/pybind11/include/pybind11/detail/common.h:603
values_and_holders::iterator::iterator
iterator(size_t end)
Definition: type_caster_base.h:338
type_info
Definition: internals.h:226
movable_cast_op_type
conditional_t< std::is_pointer< typename std::remove_reference< T >::type >::value, typename std::add_pointer< intrinsic_t< T > >::type, conditional_t< std::is_rvalue_reference< T >::value, typename std::add_rvalue_reference< intrinsic_t< T > >::type, typename std::add_lvalue_reference< intrinsic_t< T > >::type > > movable_cast_op_type
Definition: type_caster_base.h:823
instance_simple_holder_in_ptrs
constexpr size_t instance_simple_holder_in_ptrs()
Definition: wrap/pybind11/include/pybind11/detail/common.h:553
instance::nonsimple
nonsimple_values_and_holders nonsimple
Definition: wrap/pybind11/include/pybind11/detail/common.h:574
type_info::simple_type
bool simple_type
Definition: internals.h:243
value_and_holder::index
size_t index
Definition: type_caster_base.h:255
return_value_policy
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
Definition: wrap/pybind11/include/pybind11/detail/common.h:485
type_info::type
PyTypeObject * type
Definition: internals.h:227
loader_life_support::loader_life_support
loader_life_support()
A new patient frame is created when a function is entered.
Definition: type_caster_base.h:66
type_caster_base< T >::Constructor
void *(*)(const void *) Constructor
Definition: type_caster_base.h:1144
container_mapped_type_traits::has_recursive_mapped_type
static constexpr bool has_recursive_mapped_type
Definition: type_caster_base.h:830
internals::registered_types_cpp
type_map< type_info * > registered_types_cpp
Definition: internals.h:170
get_global_type_info
detail::type_info * get_global_type_info(const std::type_index &tp)
Definition: type_caster_base.h:205
instance::get_value_and_holder
value_and_holder get_value_and_holder(const type_info *find_type=nullptr, bool throw_if_missing=true)
Definition: type_caster_base.h:380
type_info::implicit_casts
std::vector< std::pair< const std::type_info *, void *(*)(void *)> > implicit_casts
Definition: internals.h:234
instance::status_instance_registered
static constexpr uint8_t status_instance_registered
Definition: wrap/pybind11/include/pybind11/detail/common.h:626
type_caster_generic::value
void * value
Definition: type_caster_base.h:795
clean_type_id
PYBIND11_NOINLINE void clean_type_id(std::string &name)
Definition: typeid.h:35
values_and_holders::inst
instance * inst
Definition: type_caster_base.h:317
handle::inc_ref
const handle & inc_ref() const &
Definition: pytypes.h:249
values_and_holders::begin
iterator begin()
Definition: type_caster_base.h:355
B
Definition: test_numpy_dtypes.cpp:299
values_and_holders::iterator::operator++
iterator & operator++()
Definition: type_caster_base.h:343
PYBIND11_NAMESPACE_END
#define PYBIND11_NAMESPACE_END(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:80
values_and_holders::iterator::curr
value_and_holder curr
Definition: type_caster_base.h:329
hasattr
bool hasattr(handle obj, handle name)
Definition: pytypes.h:853
type_caster_generic::type_caster_generic
PYBIND11_NOINLINE type_caster_generic(const std::type_info &type_info)
Definition: type_caster_base.h:500
internals.h
PYBIND11_NOINLINE
#define PYBIND11_NOINLINE
Definition: wrap/pybind11/include/pybind11/detail/common.h:186
type
Definition: pytypes.h:1491
get_thread_state_unchecked
PyThreadState * get_thread_state_unchecked()
Definition: type_caster_base.h:486
getattr
object getattr(handle obj, handle name)
Definition: pytypes.h:873
value_and_holder
Definition: type_caster_base.h:253
T
Eigen::Triplet< double > T
Definition: Tutorial_sparse_example.cpp:6
type_caster_base::make_move_constructor
static Constructor make_move_constructor(...)
Definition: type_caster_base.h:1164
detail
Definition: testSerializationNonlinear.cpp:70
PYBIND11_TLS_GET_VALUE
#define PYBIND11_TLS_GET_VALUE(key)
Definition: internals.h:101
type_caster_base< T >::cast_op_type
detail::cast_op_type< T > cast_op_type
Definition: type_caster_base.h:1131
type_caster_base::make_copy_constructor
static Constructor make_copy_constructor(...)
Definition: type_caster_base.h:1163
nonsimple_values_and_holders::values_and_holders
void ** values_and_holders
Definition: wrap/pybind11/include/pybind11/detail/common.h:564
get_internals
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
Definition: internals.h:467
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
size_in_ptrs
static constexpr size_t size_in_ptrs(size_t s)
Definition: wrap/pybind11/include/pybind11/detail/common.h:543
h
const double h
Definition: testSimpleHelicopter.cpp:19
instance::simple_value_holder
void * simple_value_holder[1+instance_simple_holder_in_ptrs()]
Definition: wrap/pybind11/include/pybind11/detail/common.h:573
type_caster_base::src_and_type
static std::pair< const void *, const type_info * > src_and_type(const itype *src)
Definition: type_caster_base.h:1087
is_move_constructible
Definition: type_caster_base.h:972
nonsimple_values_and_holders::status
uint8_t * status
Definition: wrap/pybind11/include/pybind11/detail/common.h:565
all_type_info
const std::vector< detail::type_info * > & all_type_info(PyTypeObject *type)
Definition: type_caster_base.h:169
type_caster_generic::try_implicit_casts
bool try_implicit_casts(handle src, bool convert)
Definition: type_caster_base.h:622
get_local_type_info
detail::type_info * get_local_type_info(const std::type_index &tp)
Definition: type_caster_base.h:196
result
Values result
Definition: OdometryOptimize.cpp:8
values_and_holders::iterator::operator!=
bool operator!=(const iterator &other) const
Definition: type_caster_base.h:342
type_info::cpptype
const std::type_info * cpptype
Definition: internals.h:228
type_info_description
PYBIND11_NOINLINE std::string type_info_description(const std::type_info &ti)
Definition: type_caster_base.h:1167
type_caster_base::make_copy_constructor
static auto make_copy_constructor(const T *) -> decltype(new T(std::declval< const T >()), Constructor
Definition: type_caster_base.h:1150
values_and_holders::iterator::iterator
iterator(instance *inst, const type_vec *tinfo)
Definition: type_caster_base.h:331
uint8_t
unsigned char uint8_t
Definition: ms_stdint.h:83
instance::allocate_layout
void allocate_layout()
Definition: type_caster_base.h:410
conditional_t
typename std::conditional< B, T, F >::type conditional_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:642
internals::registered_instances
std::unordered_multimap< const void *, instance * > registered_instances
Definition: internals.h:173
values_and_holders::iterator::inst
instance * inst
Definition: type_caster_base.h:327
instance
The 'instance' type which needs to be standard layout (need to be able to use 'offsetof')
Definition: wrap/pybind11/include/pybind11/detail/common.h:569
type_name
string type_name()
Definition: benchmark-blocking-sizes.cpp:252
type_caster_generic::load_value
void load_value(value_and_holder &&v_h)
Definition: type_caster_base.h:601
gtsam::range
Double_ range(const Point2_ &p, const Point2_ &q)
Definition: slam/expressions.h:30
values_and_holders::find
iterator find(const type_info *find_type)
Definition: type_caster_base.h:358
find_registered_python_instance
PYBIND11_NOINLINE handle find_registered_python_instance(void *src, const detail::type_info *tinfo)
Definition: type_caster_base.h:240
type_caster_base::cast
static handle cast(const itype &src, return_value_policy policy, handle parent)
Definition: type_caster_base.h:1072
value_and_holder::set_instance_registered
void set_instance_registered(bool v=true)
Definition: type_caster_base.h:303
value_and_holder::set_holder_constructed
void set_holder_constructed(bool v=true)
Definition: type_caster_base.h:288
same_type
bool same_type(const std::type_info &lhs, const std::type_info &rhs)
Definition: internals.h:132
recursive_container_traits
Definition: type_caster_base.h:969
PYBIND11_TLS_REPLACE_VALUE
#define PYBIND11_TLS_REPLACE_VALUE(key, value)
Definition: internals.h:115
get_fully_qualified_tp_name
std::string get_fully_qualified_tp_name(PyTypeObject *type)
Definition: class.h:28
value_and_holder::vh
void ** vh
Definition: type_caster_base.h:257
isinstance
bool isinstance(handle obj)
Definition: pytypes.h:825
return_value_policy::automatic
@ automatic
impl_recursive_container_traits< Container, typename std::enable_if< container_value_type_traits< Container >::has_value_type >::type >::type_to_check_recursively
typename std::conditional< is_recursive, typename impl_type_to_check_recursively< typename Container::value_type, container_mapped_type_traits< Container >::has_mapped_type >::if_recursive, typename impl_type_to_check_recursively< typename Container::value_type, container_mapped_type_traits< Container >::has_mapped_type >::if_not_recursive >::type type_to_check_recursively
Definition: type_caster_base.h:942
intrinsic_t
typename intrinsic_type< T >::type intrinsic_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:798
handle
Definition: pytypes.h:217
PYBIND11_TLS_KEY_REF
#define PYBIND11_TLS_KEY_REF
Definition: internals.h:98
type_caster_base::type_caster_base
type_caster_base(const std::type_info &info)
Definition: type_caster_base.h:1070
handle::cast
T cast() const
Definition: cast.h:1110
type::handle_of
static handle handle_of()
Definition: cast.h:1684
value_and_holder::inst
instance * inst
Definition: type_caster_base.h:254
polymorphic_type_hook
Definition: type_caster_base.h:1057
impl_type_to_check_recursively< std::pair< A, B >, true >::if_recursive
typename std::remove_const< A >::type if_recursive
Definition: type_caster_base.h:907
return_value_policy::copy
@ copy
container_value_type_traits::has_recursive_value_type
static constexpr bool has_recursive_value_type
Definition: type_caster_base.h:856
object::release
handle release()
Definition: pytypes.h:368
values_and_holders::iterator::operator*
value_and_holder & operator*()
Definition: type_caster_base.h:351
check
void check(bool b, bool ref)
Definition: fastmath.cpp:12
typeid.h
local_internals::registered_types_cpp
type_map< type_info * > registered_types_cpp
Definition: internals.h:545
value_and_holder::value_and_holder
value_and_holder(instance *i, const detail::type_info *type, size_t vpos, size_t index)
Definition: type_caster_base.h:260
type_caster_generic::load_impl
PYBIND11_NOINLINE bool load_impl(handle src, bool convert)
Definition: type_caster_base.h:678
instance::simple_instance_registered
bool simple_instance_registered
For simple layout, tracks whether the instance is registered in registered_instances
Definition: wrap/pybind11/include/pybind11/detail/common.h:607
instance::deallocate_layout
void deallocate_layout()
Destroys/deallocates all of the above.
Definition: type_caster_base.h:459
arg
Definition: cast.h:1277
return_value_policy::reference
@ reference
keep_alive
Keep patient alive while nurse lives.
Definition: attr.h:73
values_and_holders::iterator::types
const type_vec * types
Definition: type_caster_base.h:328
matlab_wrap.wrapper
wrapper
Definition: matlab_wrap.py:59
info
else if n * info
Definition: 3rdparty/Eigen/lapack/cholesky.cpp:18
make_new_instance
PyObject * make_new_instance(PyTypeObject *type)
Definition: class.h:349
get_type_info
PYBIND11_NOINLINE detail::type_info * get_type_info(PyTypeObject *type)
Definition: type_caster_base.h:184
container_value_type_traits
Definition: type_caster_base.h:854
instance::owned
bool owned
If true, the pointer is owned which means we're free to manage it with a holder.
Definition: wrap/pybind11/include/pybind11/detail/common.h:579
is_copy_constructible
Definition: type_caster_base.h:991
PYBIND11_NAMESPACE
Definition: test_custom_type_casters.cpp:24
impl_recursive_container_traits
Definition: type_caster_base.h:915
type_caster_base::make_move_constructor
static auto make_move_constructor(const T *) -> decltype(new T(std::declval< T && >()), Constructor
Definition: type_caster_base.h:1156
Eigen::Triplet< double >
arg
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE ArgReturnType arg() const
Definition: ArrayCwiseUnaryOps.h:66
common.h
instance::status_holder_constructed
static constexpr uint8_t status_holder_constructed
Bit values for the non-simple status flags.
Definition: wrap/pybind11/include/pybind11/detail/common.h:625
pybind11_fail
PyExc_RuntimeError PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
Definition: wrap/pybind11/include/pybind11/detail/common.h:1013
container_value_type_traits::has_value_type
static constexpr bool has_value_type
Definition: type_caster_base.h:855
values_and_holders::type_vec
std::vector< detail::type_info * > type_vec
Definition: type_caster_base.h:318
value_and_holder::value_ptr
V *& value_ptr() const
Definition: type_caster_base.h:272
values_and_holders::iterator
Definition: type_caster_base.h:325
type_info::direct_conversions
std::vector< bool(*)(PyObject *, void *&)> * direct_conversions
Definition: internals.h:235
cast_op_type
conditional_t< std::is_pointer< remove_reference_t< T > >::value, typename std::add_pointer< intrinsic_t< T > >::type, typename std::add_lvalue_reference< intrinsic_t< T > >::type > cast_op_type
Definition: type_caster_base.h:808
descr.h
type_caster_generic
Definition: type_caster_base.h:498
return_value_policy::take_ownership
@ take_ownership
impl_type_to_check_recursively
Definition: type_caster_base.h:889
get_object_handle
PYBIND11_NOINLINE handle get_object_handle(const void *ptr, const detail::type_info *type)
Definition: type_caster_base.h:473
type_caster_generic::local_load
static PYBIND11_NOINLINE void * local_load(PyObject *src, const type_info *ti)
Definition: type_caster_base.h:642
handle::ptr
PyObject * ptr() const
Return the underlying PyObject * pointer.
Definition: pytypes.h:241
loader_life_support::~loader_life_support
~loader_life_support()
... and destroyed after it returns
Definition: type_caster_base.h:69
type_caster_generic::src_and_type
static PYBIND11_NOINLINE std::pair< const void *, const type_info * > src_and_type(const void *src, const std::type_info &cast_type, const std::type_info *rtti_type=nullptr)
Definition: type_caster_base.h:778
value_and_holder::value_and_holder
value_and_holder()=default
keep_alive_impl
void keep_alive_impl(handle nurse, handle patient)
Definition: pybind11.h:2224
instance::simple_holder_constructed
bool simple_holder_constructed
For simple layout, tracks whether the holder has been constructed.
Definition: wrap/pybind11/include/pybind11/detail/common.h:605
type_caster_generic::check_holder_compat
void check_holder_compat()
Definition: type_caster_base.h:640
value_and_holder::instance_registered
bool instance_registered() const
Definition: type_caster_base.h:297
get_type_handle
PYBIND11_NOINLINE handle get_type_handle(const std::type_info &tp, bool throw_if_missing)
Definition: type_caster_base.h:234
std
Definition: BFloat16.h:88
value_and_holder::holder
H & holder() const
Definition: type_caster_base.h:279
value_and_holder::holder_constructed
bool holder_constructed() const
Definition: type_caster_base.h:282
get_local_internals
local_internals & get_local_internals()
Works like get_internals, but for things which are locally registered.
Definition: internals.h:584
type_info::module_local
bool module_local
Definition: internals.h:249
container_mapped_type_traits::has_mapped_type
static constexpr bool has_mapped_type
Definition: type_caster_base.h:829
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
values_and_holders::size
size_t size()
Definition: type_caster_base.h:366
all_type_info_populate
PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector< type_info * > &bases)
Definition: type_caster_base.h:106
type_caster_generic::try_load_foreign_module_local
PYBIND11_NOINLINE bool try_load_foreign_module_local(handle src)
Definition: type_caster_base.h:652
all_type_info_get_cache
std::pair< decltype(internals::registered_types_py)::iterator, bool > all_type_info_get_cache(PyTypeObject *type)
Definition: pybind11.h:2273
internals::registered_types_py
std::unordered_map< PyTypeObject *, std::vector< type_info * > > registered_types_py
Definition: internals.h:172
V
MatrixXcd V
Definition: EigenSolver_EigenSolver_MatrixType.cpp:15
type_caster_generic::cpptype
const std::type_info * cpptype
Definition: type_caster_base.h:794
type_caster_generic::typeinfo
const type_info * typeinfo
Definition: type_caster_base.h:793
type_caster_generic::load
bool load(handle src, bool convert)
Definition: type_caster_base.h:506
type_caster_base::cast
static handle cast(const itype *src, return_value_policy policy, handle parent)
Definition: type_caster_base.h:1109
type_caster_base::cast_holder
static handle cast_holder(const itype *src, const void *holder)
Definition: type_caster_base.h:1119
type_caster_generic::cast
static PYBIND11_NOINLINE handle cast(const void *_src, return_value_policy policy, handle parent, const detail::type_info *tinfo, void *(*copy_constructor)(const void *), void *(*move_constructor)(const void *), const void *existing_holder=nullptr)
Definition: type_caster_base.h:508
polymorphic_type_hook_base
Definition: type_caster_base.h:1046
recursive_bottom
Definition: type_caster_base.h:881
values_and_holders::values_and_holders
values_and_holders(instance *inst)
Definition: type_caster_base.h:322
none
Definition: pytypes.h:1744
loader_life_support
Definition: type_caster_base.h:34
align_3::t
Point2 t(10, 10)
return_value_policy::automatic_reference
@ automatic_reference
gtsam::convert
static BinaryMeasurement< Rot3 > convert(const BetweenFactor< Pose3 >::shared_ptr &f)
Definition: ShonanAveraging.cpp:998
type_caster_base::type_caster_base
type_caster_base()
Definition: type_caster_base.h:1069
values_and_holders
Definition: type_caster_base.h:315
values_and_holders::iterator::operator->
value_and_holder * operator->()
Definition: type_caster_base.h:352
values_and_holders::iterator::operator==
bool operator==(const iterator &other) const
Definition: type_caster_base.h:341
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
value_and_holder::type
const detail::type_info * type
Definition: type_caster_base.h:256
values_and_holders::end
iterator end()
Definition: type_caster_base.h:356
test_callbacks.value
value
Definition: test_callbacks.py:158
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
loader_life_support::add_patient
static PYBIND11_NOINLINE void add_patient(handle h)
Definition: type_caster_base.h:81
loader_life_support::get_stack_top
static loader_life_support * get_stack_top()
Definition: type_caster_base.h:60
impl_type_to_check_recursively< std::pair< A, B >, true >::if_not_recursive
std::pair< typename std::remove_const< A >::type, B > if_not_recursive
Definition: type_caster_base.h:908
isinstance_generic
PYBIND11_NOINLINE bool isinstance_generic(handle obj, const std::type_info &tp)
Definition: type_caster_base.h:465
polymorphic_type_hook_base< itype, detail::enable_if_t< std::is_polymorphic< itype >::value > >::get
static const void * get(const itype *src, const std::type_info *&type)
Definition: type_caster_base.h:1051
pybind_wrapper_test_script.inst
inst
Definition: pybind_wrapper_test_script.py:49
loader_life_support::parent
loader_life_support * parent
Definition: type_caster_base.h:36
pybind11.msg
msg
Definition: wrap/pybind11/pybind11/__init__.py:4
type_caster_base
Generic type caster for objects stored on the heap.
Definition: type_caster_base.h:1063
loader_life_support::set_stack_top
static void set_stack_top(loader_life_support *value)
Definition: type_caster_base.h:61
container_mapped_type_traits
Definition: type_caster_base.h:828
type_caster_generic::type_caster_generic
type_caster_generic(const type_info *typeinfo)
Definition: type_caster_base.h:503


gtsam
Author(s):
autogenerated on Sat Jun 1 2024 03:08:31