cpp_conduit.h
Go to the documentation of this file.
1 // Copyright (c) 2024 The pybind Community.
2 
3 #pragma once
4 
5 #include <pybind11/pytypes.h>
6 
7 #include "common.h"
8 #include "internals.h"
9 
10 #include <typeinfo>
11 
14 
15 // Forward declaration needed here: Refactoring opportunity.
16 extern "C" inline PyObject *pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *);
17 
18 inline bool type_is_managed_by_our_internals(PyTypeObject *type_obj) {
19 #if defined(PYPY_VERSION)
20  auto &internals = get_internals();
21  return bool(internals.registered_types_py.find(type_obj)
23 #else
24  return bool(type_obj->tp_new == pybind11_object_new);
25 #endif
26 }
27 
28 inline bool is_instance_method_of_type(PyTypeObject *type_obj, PyObject *attr_name) {
29  PyObject *descr = _PyType_Lookup(type_obj, attr_name);
30  return bool((descr != nullptr) && PyInstanceMethod_Check(descr));
31 }
32 
33 inline object try_get_cpp_conduit_method(PyObject *obj) {
34  if (PyType_Check(obj)) {
35  return object();
36  }
37  PyTypeObject *type_obj = Py_TYPE(obj);
38  str attr_name("_pybind11_conduit_v1_");
39  bool assumed_to_be_callable = false;
40  if (type_is_managed_by_our_internals(type_obj)) {
41  if (!is_instance_method_of_type(type_obj, attr_name.ptr())) {
42  return object();
43  }
44  assumed_to_be_callable = true;
45  }
46  PyObject *method = PyObject_GetAttr(obj, attr_name.ptr());
47  if (method == nullptr) {
48  PyErr_Clear();
49  return object();
50  }
51  if (!assumed_to_be_callable && PyCallable_Check(method) == 0) {
52  Py_DECREF(method);
53  return object();
54  }
55  return reinterpret_steal<object>(method);
56 }
57 
59  const std::type_info *cpp_type_info) {
60  object method = try_get_cpp_conduit_method(src.ptr());
61  if (method) {
62  capsule cpp_type_info_capsule(const_cast<void *>(static_cast<const void *>(cpp_type_info)),
63  typeid(std::type_info).name());
64  object cpp_conduit = method(bytes(PYBIND11_PLATFORM_ABI_ID),
65  cpp_type_info_capsule,
66  bytes("raw_pointer_ephemeral"));
67  if (isinstance<capsule>(cpp_conduit)) {
68  return reinterpret_borrow<capsule>(cpp_conduit).get_pointer();
69  }
70  }
71  return nullptr;
72 }
73 
74 #define PYBIND11_HAS_CPP_CONDUIT 1
75 
internals
Definition: internals.h:177
try_raw_pointer_ephemeral_from_cpp_conduit
void * try_raw_pointer_ephemeral_from_cpp_conduit(handle src, const std::type_info *cpp_type_info)
Definition: cpp_conduit.h:58
bytes
Definition: pytypes.h:1664
try_get_cpp_conduit_method
object try_get_cpp_conduit_method(PyObject *obj)
Definition: cpp_conduit.h:33
PYBIND11_NAMESPACE_END
#define PYBIND11_NAMESPACE_END(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:80
capsule
Definition: pytypes.h:1953
internals.h
type
Definition: pytypes.h:1527
detail
Definition: testSerializationNonlinear.cpp:69
get_internals
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
Definition: internals.h:500
PYBIND11_NAMESPACE_BEGIN
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:76
pybind11_object_new
PyObject * pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *)
Definition: class.h:369
descr
Definition: descr.h:25
name
static char name[]
Definition: rgamma.c:72
object
Definition: pytypes.h:364
handle
Definition: pytypes.h:226
PYBIND11_PLATFORM_ABI_ID
#define PYBIND11_PLATFORM_ABI_ID
Definition: internals.h:324
PYBIND11_NAMESPACE
Definition: test_custom_type_casters.cpp:24
common.h
type_is_managed_by_our_internals
bool type_is_managed_by_our_internals(PyTypeObject *type_obj)
Definition: cpp_conduit.h:18
pytypes.h
str
Definition: pytypes.h:1560
is_instance_method_of_type
bool is_instance_method_of_type(PyTypeObject *type_obj, PyObject *attr_name)
Definition: cpp_conduit.h:28
handle::ptr
PyObject * ptr() const
Return the underlying PyObject * pointer.
Definition: pytypes.h:250
internals::registered_types_py
std::unordered_map< PyTypeObject *, std::vector< type_info * > > registered_types_py
Definition: internals.h:184


gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:01:30