mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-29 08:32:02 +00:00
48f25275c4
* Put bakein branch @ 18b72c0ffa
on top of smart_holder branch:
Commands used:
```
git checkout bakein
git diff smart_holder > ~/zd
git checkout smart_holder
git checkout -b bakein_sh
patch -p 1 < ~/zd
git checkout smart_holder \
MANIFEST.in \
README.rst \
README_smart_holder.rst \
docs/advanced/smart_ptrs.rst \
ubench/holder_comparison.cpp \
ubench/holder_comparison.py \
ubench/holder_comparison_extract_sheet_data.py \
ubench/number_bucket.h \
ubench/python/number_bucket.clif
git add -A
```
* Add back README_smart_holder.rst in tests/extra_python_package/test_files.py
* Restore smart_holder_poc.h as-is on smart_holder branch (i.e. undo `PYBIND11_SMART_HOLDER_PADDING`, which was meant for stress-testing only).
* Insert `std::move()` as suggested by @laramiel
* `property_cpp_function_sh_*` named specializations, as suggested by @laramiel (https://github.com/pybind/pybind11/pull/5257#discussion_r1688346807)
* Call `property_cpp_function_classic` member functions, rather than inlining the implementations.
* Use `PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT` in holder_comparison.cpp (holder_comparison.py is NOT changed accordingly in this commit, i.e. can still only be run if the smart_holder functionality is available).
* Systematically rename `loaded_as` to `load_as` (`shared_ptr`, `unique_ptr`) as suggested by @laramiel
* Make change as suggested by @laramiel. This makes it much more obvious that the latest implementation of `smart_holder_from_unique_ptr()` accepts all existing `return_value_policy` enum values except `copy`.
* Resolve `BAKEIN_WIP: Rewrite comment.` for `property_cpp_function_*` specializations.
* Resolve `BAKEIN_WIP: Add comment to explain: This is meant for stress-testing only.`
* Resolve all remaining BAKEIN_WIP (in pybind11/cast.h).
Leave only two pairs of SMART_HOLDER_BAKEIN_FOLLOW_ON comments: refactoring of copyable_holder_caster, move_only_holder_caster. This is best left until after the smart_holder branch is merged into the master branch.
* Remove obsolete `using holder_type = smart_holder;` in `load_helper`
* Add SMART_HOLDER_BAKEIN_FOLLOW_ON comment for `internals::default_holder`
* README_smart_holder.rst update (line count reduced from 356 to 123).
113 lines
4.0 KiB
C++
113 lines
4.0 KiB
C++
#include <pybind11/smart_holder.h>
|
|
|
|
#include "pybind11_tests.h"
|
|
|
|
#include <memory>
|
|
|
|
namespace pybind11_tests {
|
|
namespace class_sh_inheritance {
|
|
|
|
template <int Id>
|
|
struct base_template {
|
|
base_template() : base_id(Id) {}
|
|
virtual ~base_template() = default;
|
|
virtual int id() const { return base_id; }
|
|
int base_id;
|
|
|
|
// Some compilers complain about implicitly defined versions of some of the following:
|
|
base_template(const base_template &) = default;
|
|
base_template(base_template &&) noexcept = default;
|
|
base_template &operator=(const base_template &) = default;
|
|
base_template &operator=(base_template &&) noexcept = default;
|
|
};
|
|
|
|
using base = base_template<100>;
|
|
|
|
struct drvd : base {
|
|
int id() const override { return 2 * base_id; }
|
|
};
|
|
|
|
// clang-format off
|
|
inline drvd *rtrn_mptr_drvd() { return new drvd; }
|
|
inline base *rtrn_mptr_drvd_up_cast() { return new drvd; }
|
|
|
|
inline int pass_cptr_base(base const *b) { return b->id() + 11; }
|
|
inline int pass_cptr_drvd(drvd const *d) { return d->id() + 12; }
|
|
|
|
inline std::shared_ptr<drvd> rtrn_shmp_drvd() { return std::make_shared<drvd>(); }
|
|
inline std::shared_ptr<base> rtrn_shmp_drvd_up_cast() { return std::make_shared<drvd>(); }
|
|
|
|
inline int pass_shcp_base(const std::shared_ptr<base const>& b) { return b->id() + 21; }
|
|
inline int pass_shcp_drvd(const std::shared_ptr<drvd const>& d) { return d->id() + 22; }
|
|
// clang-format on
|
|
|
|
using base1 = base_template<110>;
|
|
using base2 = base_template<120>;
|
|
|
|
// Not reusing base here because it would interfere with the single-inheritance test.
|
|
struct drvd2 : base1, base2 {
|
|
int id() const override { return 3 * base1::base_id + 4 * base2::base_id; }
|
|
};
|
|
|
|
// clang-format off
|
|
inline drvd2 *rtrn_mptr_drvd2() { return new drvd2; }
|
|
inline base1 *rtrn_mptr_drvd2_up_cast1() { return new drvd2; }
|
|
inline base2 *rtrn_mptr_drvd2_up_cast2() { return new drvd2; }
|
|
|
|
inline int pass_cptr_base1(base1 const *b) { return b->id() + 21; }
|
|
inline int pass_cptr_base2(base2 const *b) { return b->id() + 22; }
|
|
inline int pass_cptr_drvd2(drvd2 const *d) { return d->id() + 23; }
|
|
// clang-format on
|
|
|
|
} // namespace class_sh_inheritance
|
|
} // namespace pybind11_tests
|
|
|
|
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_inheritance::base)
|
|
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_inheritance::drvd)
|
|
|
|
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_inheritance::base1)
|
|
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_inheritance::base2)
|
|
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_inheritance::drvd2)
|
|
|
|
namespace pybind11_tests {
|
|
namespace class_sh_inheritance {
|
|
|
|
TEST_SUBMODULE(class_sh_inheritance, m) {
|
|
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
|
|
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
|
|
false;
|
|
#else
|
|
true;
|
|
|
|
py::classh<base>(m, "base");
|
|
py::classh<drvd, base>(m, "drvd");
|
|
|
|
auto rvto = py::return_value_policy::take_ownership;
|
|
|
|
m.def("rtrn_mptr_drvd", rtrn_mptr_drvd, rvto);
|
|
m.def("rtrn_mptr_drvd_up_cast", rtrn_mptr_drvd_up_cast, rvto);
|
|
m.def("pass_cptr_base", pass_cptr_base);
|
|
m.def("pass_cptr_drvd", pass_cptr_drvd);
|
|
|
|
m.def("rtrn_shmp_drvd", rtrn_shmp_drvd);
|
|
m.def("rtrn_shmp_drvd_up_cast", rtrn_shmp_drvd_up_cast);
|
|
m.def("pass_shcp_base", pass_shcp_base);
|
|
m.def("pass_shcp_drvd", pass_shcp_drvd);
|
|
|
|
// __init__ needed for Python inheritance.
|
|
py::classh<base1>(m, "base1").def(py::init<>());
|
|
py::classh<base2>(m, "base2").def(py::init<>());
|
|
py::classh<drvd2, base1, base2>(m, "drvd2");
|
|
|
|
m.def("rtrn_mptr_drvd2", rtrn_mptr_drvd2, rvto);
|
|
m.def("rtrn_mptr_drvd2_up_cast1", rtrn_mptr_drvd2_up_cast1, rvto);
|
|
m.def("rtrn_mptr_drvd2_up_cast2", rtrn_mptr_drvd2_up_cast2, rvto);
|
|
m.def("pass_cptr_base1", pass_cptr_base1);
|
|
m.def("pass_cptr_base2", pass_cptr_base2);
|
|
m.def("pass_cptr_drvd2", pass_cptr_drvd2);
|
|
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
|
|
}
|
|
|
|
} // namespace class_sh_inheritance
|
|
} // namespace pybind11_tests
|