mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-25 14:45:12 +00:00
bd8985aa0f
* Step 1: Establish new `PYBIND11_SMART_HOLDER_ENABLED` macro, but only under include/pybind11/ At the stage `PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT` and `PYBIND11_SMART_HOLDER_ENABLED` are still equivalent. * Systematically replace all `PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT` with `PYBIND11_SMART_HOLDER_ENABLED` under tests/ and ubench/ As at the previous stage, `PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT` and `PYBIND11_SMART_HOLDER_ENABLED` are still equivalent. * Introduce `PYBIND11_SMART_HOLDER_DISABLE` option. * `#ifdef` out entire `wrap()` function to avoid `unused-parameter` warning-as-error under macos-13 ``` /Users/runner/work/pybind11/pybind11/tests/test_class_sh_trampoline_basic.cpp:67:23: error: unused parameter 'm' [-Werror,-Wunused-parameter] void wrap(py::module_ m, const char *py_class_name) { ^ /Users/runner/work/pybind11/pybind11/tests/test_class_sh_trampoline_basic.cpp:67:38: error: unused parameter 'py_class_name' [-Werror,-Wunused-parameter] void wrap(py::module_ m, const char *py_class_name) { ^ 2 errors generated. ```
99 lines
3.1 KiB
C++
99 lines
3.1 KiB
C++
#include <pybind11/smart_holder.h>
|
|
|
|
#include "pybind11_tests.h"
|
|
|
|
#include <memory>
|
|
|
|
namespace pybind11_tests {
|
|
namespace class_sh_trampoline_basic {
|
|
|
|
template <int SerNo> // Using int as a trick to easily generate a series of types.
|
|
struct Abase {
|
|
int val = 0;
|
|
virtual ~Abase() = default;
|
|
explicit Abase(int val_) : val{val_} {}
|
|
int Get() const { return val * 10 + 3; }
|
|
virtual int Add(int other_val) const = 0;
|
|
|
|
// Some compilers complain about implicitly defined versions of some of the following:
|
|
Abase(const Abase &) = default;
|
|
Abase(Abase &&) noexcept = default;
|
|
Abase &operator=(const Abase &) = default;
|
|
Abase &operator=(Abase &&) noexcept = default;
|
|
};
|
|
|
|
template <int SerNo>
|
|
struct AbaseAlias : Abase<SerNo> {
|
|
using Abase<SerNo>::Abase;
|
|
|
|
int Add(int other_val) const override {
|
|
PYBIND11_OVERRIDE_PURE(int, /* Return type */
|
|
Abase<SerNo>, /* Parent class */
|
|
Add, /* Name of function in C++ (must match Python name) */
|
|
other_val);
|
|
}
|
|
};
|
|
|
|
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
|
template <>
|
|
struct AbaseAlias<1> : Abase<1>, py::trampoline_self_life_support {
|
|
using Abase<1>::Abase;
|
|
|
|
int Add(int other_val) const override {
|
|
PYBIND11_OVERRIDE_PURE(int, /* Return type */
|
|
Abase<1>, /* Parent class */
|
|
Add, /* Name of function in C++ (must match Python name) */
|
|
other_val);
|
|
}
|
|
};
|
|
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
|
|
|
template <int SerNo>
|
|
int AddInCppRawPtr(const Abase<SerNo> *obj, int other_val) {
|
|
return obj->Add(other_val) * 10 + 7;
|
|
}
|
|
|
|
template <int SerNo>
|
|
int AddInCppSharedPtr(std::shared_ptr<Abase<SerNo>> obj, int other_val) {
|
|
return obj->Add(other_val) * 100 + 11;
|
|
}
|
|
|
|
template <int SerNo>
|
|
int AddInCppUniquePtr(std::unique_ptr<Abase<SerNo>> obj, int other_val) {
|
|
return obj->Add(other_val) * 100 + 13;
|
|
}
|
|
|
|
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
|
template <int SerNo>
|
|
void wrap(py::module_ m, const char *py_class_name) {
|
|
py::classh<Abase<SerNo>, AbaseAlias<SerNo>>(m, py_class_name)
|
|
.def(py::init<int>(), py::arg("val"))
|
|
.def("Get", &Abase<SerNo>::Get)
|
|
.def("Add", &Abase<SerNo>::Add, py::arg("other_val"));
|
|
|
|
m.def("AddInCppRawPtr", AddInCppRawPtr<SerNo>, py::arg("obj"), py::arg("other_val"));
|
|
m.def("AddInCppSharedPtr", AddInCppSharedPtr<SerNo>, py::arg("obj"), py::arg("other_val"));
|
|
m.def("AddInCppUniquePtr", AddInCppUniquePtr<SerNo>, py::arg("obj"), py::arg("other_val"));
|
|
}
|
|
#endif
|
|
|
|
} // namespace class_sh_trampoline_basic
|
|
} // namespace pybind11_tests
|
|
|
|
using namespace pybind11_tests::class_sh_trampoline_basic;
|
|
|
|
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Abase<0>)
|
|
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Abase<1>)
|
|
|
|
TEST_SUBMODULE(class_sh_trampoline_basic, m) {
|
|
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
|
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
|
false;
|
|
#else
|
|
true;
|
|
|
|
wrap<0>(m, "Abase0");
|
|
wrap<1>(m, "Abase1");
|
|
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
|
}
|