mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-14 17:43:53 +00:00
6c922614ed
* Using new smart_holder::reclaim_disowned in smart_holder_type_caster for unique_ptr. * Systematically renaming was_disowned to is_disowned (because disowning is now reversible: reclaim_disowned). * Systematically renaming virtual_overrider_self_life_support to trampoline_self_life_support (to reuse existing terminology instead of introducing new one). * Systematically renaming test_class_sh_with_alias to test_class_sh_trampoline_basic. * Adding a Trampolines and std::unique_ptr section to README_smart_holder.rst. * MSVC compatibility.
61 lines
1.6 KiB
C++
61 lines
1.6 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 {
|
|
|
|
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
|
|
|
|
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Class)
|
|
|
|
namespace {
|
|
|
|
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
|
|
|
|
TEST_SUBMODULE(class_sh_trampoline_unique_ptr, m) {
|
|
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(); });
|
|
}
|