2021-04-09 05:56:46 +00:00
|
|
|
// 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"
|
2021-04-10 06:08:44 +00:00
|
|
|
#include "pybind11/trampoline_self_life_support.h"
|
2021-04-09 05:56:46 +00:00
|
|
|
#include "pybind11_tests.h"
|
|
|
|
|
2021-04-10 06:08:44 +00:00
|
|
|
#include <cstdint>
|
|
|
|
|
2024-07-22 08:56:42 +00:00
|
|
|
namespace pybind11_tests {
|
|
|
|
namespace class_sh_trampoline_unique_ptr {
|
2021-04-09 05:56:46 +00:00
|
|
|
|
|
|
|
class Class {
|
|
|
|
public:
|
2021-04-10 06:08:44 +00:00
|
|
|
virtual ~Class() = default;
|
|
|
|
|
|
|
|
void setVal(std::uint64_t val) { val_ = val; }
|
|
|
|
std::uint64_t getVal() const { return val_; }
|
|
|
|
|
2021-04-09 05:56:46 +00:00
|
|
|
virtual std::unique_ptr<Class> clone() const = 0;
|
2021-07-02 23:51:24 +00:00
|
|
|
virtual int foo() const = 0;
|
2021-04-09 05:56:46 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
Class() = default;
|
|
|
|
|
|
|
|
// Some compilers complain about implicitly defined versions of some of the following:
|
|
|
|
Class(const Class &) = default;
|
2021-04-10 06:08:44 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::uint64_t val_ = 0;
|
2021-04-09 05:56:46 +00:00
|
|
|
};
|
|
|
|
|
2024-07-22 08:56:42 +00:00
|
|
|
} // namespace class_sh_trampoline_unique_ptr
|
|
|
|
} // namespace pybind11_tests
|
2021-04-09 05:56:46 +00:00
|
|
|
|
2024-07-22 08:56:42 +00:00
|
|
|
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_trampoline_unique_ptr::Class)
|
2021-04-09 05:56:46 +00:00
|
|
|
|
2024-07-22 08:56:42 +00:00
|
|
|
namespace pybind11_tests {
|
|
|
|
namespace class_sh_trampoline_unique_ptr {
|
2021-04-09 05:56:46 +00:00
|
|
|
|
2024-09-01 21:34:36 +00:00
|
|
|
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
2021-04-10 06:08:44 +00:00
|
|
|
class PyClass : public Class, public py::trampoline_self_life_support {
|
2021-04-09 05:56:46 +00:00
|
|
|
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); }
|
|
|
|
};
|
2024-07-31 13:17:31 +00:00
|
|
|
#endif
|
2021-04-09 05:56:46 +00:00
|
|
|
|
2024-07-22 08:56:42 +00:00
|
|
|
} // namespace class_sh_trampoline_unique_ptr
|
|
|
|
} // namespace pybind11_tests
|
2021-04-09 05:56:46 +00:00
|
|
|
|
|
|
|
TEST_SUBMODULE(class_sh_trampoline_unique_ptr, m) {
|
2024-09-01 21:34:36 +00:00
|
|
|
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
|
|
|
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
2024-07-31 13:17:31 +00:00
|
|
|
false;
|
|
|
|
#else
|
|
|
|
true;
|
|
|
|
|
2024-07-22 08:56:42 +00:00
|
|
|
using namespace pybind11_tests::class_sh_trampoline_unique_ptr;
|
|
|
|
|
2021-04-09 05:56:46 +00:00
|
|
|
py::classh<Class, PyClass>(m, "Class")
|
|
|
|
.def(py::init<>())
|
2021-04-10 06:08:44 +00:00
|
|
|
.def("set_val", &Class::setVal)
|
|
|
|
.def("get_val", &Class::getVal)
|
2021-04-09 05:56:46 +00:00
|
|
|
.def("clone", &Class::clone)
|
|
|
|
.def("foo", &Class::foo);
|
|
|
|
|
2021-04-10 06:08:44 +00:00
|
|
|
m.def("clone", [](const Class &obj) { return obj.clone(); });
|
2021-04-09 05:56:46 +00:00
|
|
|
m.def("clone_and_foo", [](const Class &obj) { return obj.clone()->foo(); });
|
2024-09-01 21:34:36 +00:00
|
|
|
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
2021-04-09 05:56:46 +00:00
|
|
|
}
|