pybind11/tests/test_class_sh_virtual_py_cpp_mix.cpp
Ralf W. Grosse-Kunstleve 5ab036bf08
[smart_holder] Simplification: Enable smart_holder functionality unconditionally. (#5531)
* git merge --squash purge_internals_versions_4_5

* Remove PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT, set PYBIND11_INTERNALS_VERSION 7

* Remove all uses of PYBIND11_SMART_HOLDER_ENABLED under include/pybind11

* Remove obsolete PYBIND11_ACTUALLY_USING_SMART_HOLDER_AS_DEFAULT macro.

* Remove PYBIND11_SMART_HOLDER_ENABLED in ubench/holder_comparison.cpp

* Remove all uses of PYBIND11_SMART_HOLDER_ENABLED under tests/

* Remove `#define PYBIND11_SMART_HOLDER_ENABLED`

* Remove all uses of PYBIND11_SMART_HOLDER_TYPE_CASTERS under tests/

* Remove all uses of PYBIND11_TYPE_CASTER_BASE_HOLDER under tests/

* Add missing `#include <cstdint>`

Example error message (🐍 3.11 • ubuntu-latest • x64, GNU 13.3.0):

```
include/pybind11/detail/value_and_holder.h:56:52: error: ‘uint8_t’ is not a member of ‘std’; did you mean ‘wint_t’?
   56 |             inst->nonsimple.status[index] &= (std::uint8_t) ~instance::status_holder_constructed;
      |                                                    ^~~~~~~
```

* Change PYBIND11_INTERNALS_VERSION to 106: It will be changed to 7 in a follow-on PR that actually changes the internals.
2025-02-22 10:22:20 -08:00

61 lines
1.7 KiB
C++

#include <pybind11/smart_holder.h>
#include "pybind11_tests.h"
#include <memory>
namespace pybind11_tests {
namespace class_sh_virtual_py_cpp_mix {
class Base {
public:
virtual ~Base() = default;
virtual int get() const { return 101; }
// Some compilers complain about implicitly defined versions of some of the following:
Base() = default;
Base(const Base &) = default;
};
class CppDerivedPlain : public Base {
public:
int get() const override { return 202; }
};
class CppDerived : public Base {
public:
int get() const override { return 212; }
};
int get_from_cpp_plainc_ptr(const Base *b) { return b->get() + 4000; }
int get_from_cpp_unique_ptr(std::unique_ptr<Base> b) { return b->get() + 5000; }
struct BaseVirtualOverrider : Base, py::trampoline_self_life_support {
using Base::Base;
int get() const override { PYBIND11_OVERRIDE(int, Base, get); }
};
struct CppDerivedVirtualOverrider : CppDerived, py::trampoline_self_life_support {
using CppDerived::CppDerived;
int get() const override { PYBIND11_OVERRIDE(int, CppDerived, get); }
};
} // namespace class_sh_virtual_py_cpp_mix
} // namespace pybind11_tests
using namespace pybind11_tests::class_sh_virtual_py_cpp_mix;
TEST_SUBMODULE(class_sh_virtual_py_cpp_mix, m) {
py::classh<Base, BaseVirtualOverrider>(m, "Base").def(py::init<>()).def("get", &Base::get);
py::classh<CppDerivedPlain, Base>(m, "CppDerivedPlain").def(py::init<>());
py::classh<CppDerived, Base, CppDerivedVirtualOverrider>(m, "CppDerived").def(py::init<>());
m.def("get_from_cpp_plainc_ptr", get_from_cpp_plainc_ptr, py::arg("b"));
m.def("get_from_cpp_unique_ptr", get_from_cpp_unique_ptr, py::arg("b"));
}