|
1 | 1 | // Copyright (c) 2024 The pybind Community. |
2 | 2 |
|
3 | 3 | // THIS MUST STAY AT THE TOP! |
4 | | -#include <pybind11/compat/pybind11_platform_abi_id.h> |
| 4 | +#include <pybind11/compat/pybind11_conduit_v1.h> // VERY light-weight dependency. |
5 | 5 |
|
6 | 6 | #include "test_cpp_conduit_traveler_types.h" |
7 | 7 |
|
8 | 8 | #include <Python.h> |
9 | | -#include <typeinfo> |
10 | 9 |
|
11 | 10 | namespace { |
12 | 11 |
|
13 | | -void *get_cpp_conduit_void_ptr(PyObject *py_obj, const std::type_info *cpp_type_info) { |
14 | | - PyObject *cpp_type_info_capsule |
15 | | - = PyCapsule_New(const_cast<void *>(static_cast<const void *>(cpp_type_info)), |
16 | | - typeid(std::type_info).name(), |
17 | | - nullptr); |
18 | | - if (cpp_type_info_capsule == nullptr) { |
19 | | - return nullptr; |
20 | | - } |
21 | | - PyObject *cpp_conduit = PyObject_CallMethod(py_obj, |
22 | | - "_pybind11_conduit_v1_", |
23 | | - "yOy", |
24 | | - PYBIND11_PLATFORM_ABI_ID, |
25 | | - cpp_type_info_capsule, |
26 | | - "raw_pointer_ephemeral"); |
27 | | - Py_DECREF(cpp_type_info_capsule); |
28 | | - if (cpp_conduit == nullptr) { |
29 | | - return nullptr; |
30 | | - } |
31 | | - void *void_ptr = PyCapsule_GetPointer(cpp_conduit, cpp_type_info->name()); |
32 | | - Py_DECREF(cpp_conduit); |
33 | | - if (PyErr_Occurred()) { |
34 | | - return nullptr; |
35 | | - } |
36 | | - return void_ptr; |
37 | | -} |
38 | | - |
39 | | -template <typename T> |
40 | | -T *get_cpp_conduit_type_ptr(PyObject *py_obj) { |
41 | | - void *void_ptr = get_cpp_conduit_void_ptr(py_obj, &typeid(T)); |
42 | | - if (void_ptr == nullptr) { |
43 | | - return nullptr; |
44 | | - } |
45 | | - return static_cast<T *>(void_ptr); |
46 | | -} |
47 | | - |
48 | 12 | extern "C" PyObject *wrapGetLuggage(PyObject * /*self*/, PyObject *traveler) { |
49 | | - const auto *cpp_traveler |
50 | | - = get_cpp_conduit_type_ptr<pybind11_tests::test_cpp_conduit::Traveler>(traveler); |
| 13 | + const auto *cpp_traveler = pybind11_conduit_v1::get_type_pointer_ephemeral< |
| 14 | + pybind11_tests::test_cpp_conduit::Traveler>(traveler); |
51 | 15 | if (cpp_traveler == nullptr) { |
52 | 16 | return nullptr; |
53 | 17 | } |
54 | 18 | return PyUnicode_FromString(cpp_traveler->luggage.c_str()); |
55 | 19 | } |
56 | 20 |
|
57 | 21 | extern "C" PyObject *wrapGetPoints(PyObject * /*self*/, PyObject *premium_traveler) { |
58 | | - const auto *cpp_premium_traveler |
59 | | - = get_cpp_conduit_type_ptr<pybind11_tests::test_cpp_conduit::PremiumTraveler>( |
60 | | - premium_traveler); |
| 22 | + const auto *cpp_premium_traveler = pybind11_conduit_v1::get_type_pointer_ephemeral< |
| 23 | + pybind11_tests::test_cpp_conduit::PremiumTraveler>(premium_traveler); |
61 | 24 | if (cpp_premium_traveler == nullptr) { |
62 | 25 | return nullptr; |
63 | 26 | } |
|
0 commit comments