pybind11/tests/test_class_sh_trampoline_unique_ptr.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

65 lines
1.8 KiB
C++

// Copyright (c) 2021 The Pybind Development Team.
// All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "pybind11/smart_holder.h"
#include "pybind11/trampoline_self_life_support.h"
#include "pybind11_tests.h"
#include <cstdint>
namespace pybind11_tests {
namespace class_sh_trampoline_unique_ptr {
class Class {
public:
virtual ~Class() = default;
void setVal(std::uint64_t val) { val_ = val; }
std::uint64_t getVal() const { return val_; }
virtual std::unique_ptr<Class> clone() const = 0;
virtual int foo() const = 0;
protected:
Class() = default;
// Some compilers complain about implicitly defined versions of some of the following:
Class(const Class &) = default;
private:
std::uint64_t val_ = 0;
};
} // namespace class_sh_trampoline_unique_ptr
} // namespace pybind11_tests
namespace pybind11_tests {
namespace class_sh_trampoline_unique_ptr {
class PyClass : public Class, public py::trampoline_self_life_support {
public:
std::unique_ptr<Class> clone() const override {
PYBIND11_OVERRIDE_PURE(std::unique_ptr<Class>, Class, clone);
}
int foo() const override { PYBIND11_OVERRIDE_PURE(int, Class, foo); }
};
} // namespace class_sh_trampoline_unique_ptr
} // namespace pybind11_tests
TEST_SUBMODULE(class_sh_trampoline_unique_ptr, m) {
using namespace pybind11_tests::class_sh_trampoline_unique_ptr;
py::classh<Class, PyClass>(m, "Class")
.def(py::init<>())
.def("set_val", &Class::setVal)
.def("get_val", &Class::getVal)
.def("clone", &Class::clone)
.def("foo", &Class::foo);
m.def("clone", [](const Class &obj) { return obj.clone(); });
m.def("clone_and_foo", [](const Class &obj) { return obj.clone()->foo(); });
}