Files
pybind11/tests/test_class_sh_trampoline_self_life_support.cpp
Ralf W. Grosse-Kunstleve bd8985aa0f [smart_holder] Introduce PYBIND11_SMART_HOLDER_DISABLE option. (#5348)
* 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.
```
2024-09-01 14:34:36 -07:00

99 lines
3.2 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 <memory>
#include <string>
#include <utility>
namespace pybind11_tests {
namespace class_sh_trampoline_self_life_support {
struct Big5 { // Also known as "rule of five".
std::string history;
explicit Big5(std::string history_start) : history{std::move(history_start)} {}
Big5(const Big5 &other) { history = other.history + "_CpCtor"; }
Big5(Big5 &&other) noexcept { history = other.history + "_MvCtor"; }
Big5 &operator=(const Big5 &other) {
history = other.history + "_OpEqLv";
return *this;
}
Big5 &operator=(Big5 &&other) noexcept {
history = other.history + "_OpEqRv";
return *this;
}
virtual ~Big5() = default;
protected:
Big5() : history{"DefaultConstructor"} {}
};
#ifdef PYBIND11_SMART_HOLDER_ENABLED
struct Big5Trampoline : Big5, py::trampoline_self_life_support {
using Big5::Big5;
};
#endif
} // namespace class_sh_trampoline_self_life_support
} // namespace pybind11_tests
using namespace pybind11_tests::class_sh_trampoline_self_life_support;
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Big5)
TEST_SUBMODULE(class_sh_trampoline_self_life_support, m) {
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
#ifndef PYBIND11_SMART_HOLDER_ENABLED
false;
#else
true;
py::classh<Big5, Big5Trampoline>(m, "Big5")
.def(py::init<std::string>())
.def_readonly("history", &Big5::history);
m.def("action", [](std::unique_ptr<Big5> obj, int action_id) {
py::object o2 = py::none();
// This is very unusual, but needed to directly exercise the trampoline_self_life_support
// CpCtor, MvCtor, operator= lvalue, operator= rvalue.
auto *obj_trampoline = dynamic_cast<Big5Trampoline *>(obj.get());
if (obj_trampoline != nullptr) {
switch (action_id) {
case 0: { // CpCtor
std::unique_ptr<Big5> cp(new Big5Trampoline(*obj_trampoline));
o2 = py::cast(std::move(cp));
} break;
case 1: { // MvCtor
std::unique_ptr<Big5> mv(new Big5Trampoline(std::move(*obj_trampoline)));
o2 = py::cast(std::move(mv));
} break;
case 2: { // operator= lvalue
std::unique_ptr<Big5> lv(new Big5Trampoline);
*lv = *obj_trampoline; // NOLINT clang-tidy cppcoreguidelines-slicing
o2 = py::cast(std::move(lv));
} break;
case 3: { // operator= rvalue
std::unique_ptr<Big5> rv(new Big5Trampoline);
*rv = std::move(*obj_trampoline);
o2 = py::cast(std::move(rv));
} break;
default:
break;
}
}
py::object o1 = py::cast(std::move(obj));
return py::make_tuple(o1, o2);
});
#endif // PYBIND11_SMART_HOLDER_ENABLED
}