mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-29 00:22:00 +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. ```
76 lines
2.1 KiB
C++
76 lines
2.1 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
|
|
|
|
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_trampoline_unique_ptr::Class)
|
|
|
|
namespace pybind11_tests {
|
|
namespace class_sh_trampoline_unique_ptr {
|
|
|
|
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
|
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); }
|
|
};
|
|
#endif
|
|
|
|
} // namespace class_sh_trampoline_unique_ptr
|
|
} // namespace pybind11_tests
|
|
|
|
TEST_SUBMODULE(class_sh_trampoline_unique_ptr, m) {
|
|
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
|
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
|
false;
|
|
#else
|
|
true;
|
|
|
|
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(); });
|
|
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
|
}
|