Reintroducing py::classh, this time as a simple alias for py::class_<U, py::smart_holder>.

This commit is contained in:
Ralf W. Grosse-Kunstleve 2021-01-28 07:31:40 -08:00
parent 4a4087fdd5
commit 87acc89b21
6 changed files with 28 additions and 17 deletions

View File

@ -5,3 +5,18 @@
#pragma once
#include "detail/smart_holder_type_casters.h"
#include "pybind11.h"
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
// Supports easier switching between py::class_<U> and py::class_<U, py::smart_holder>:
// users can simply replace the `_` in `class_` with `h` or vice versa.
// Note though that the PYBIND11_SMART_HOLDER_TYPE_CASTERS(U) macro also needs to be
// added (for `classh`) or commented out (for `class_`).
template <typename type_, typename... options>
class classh : public class_<type_, smart_holder, options...> {
public:
using class_<type_, smart_holder, options...>::class_;
};
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

View File

@ -22,7 +22,7 @@ PYBIND11_MODULE(class_sh_module_local_1, m) {
namespace py = pybind11;
using namespace pybind11_tests::class_sh_module_local;
py::class_<atyp, py::smart_holder>(m, "atyp", py::module_local())
py::classh<atyp>(m, "atyp", py::module_local())
.def(py::init([](const std::string &mtxt) {
atyp obj;
obj.mtxt = mtxt;

View File

@ -22,7 +22,7 @@ PYBIND11_MODULE(class_sh_module_local_2, m) {
namespace py = pybind11;
using namespace pybind11_tests::class_sh_module_local;
py::class_<atyp, py::smart_holder>(m, "atyp", py::module_local())
py::classh<atyp>(m, "atyp", py::module_local())
.def(py::init([](const std::string &mtxt) {
atyp obj;
obj.mtxt = mtxt;

View File

@ -57,13 +57,11 @@ namespace class_sh_basic {
TEST_SUBMODULE(class_sh_basic, m) {
namespace py = pybind11;
py::class_<atyp, py::smart_holder>(m, "atyp")
.def(py::init<>())
.def(py::init([](const std::string &mtxt) {
atyp obj;
obj.mtxt = mtxt;
return obj;
}));
py::classh<atyp>(m, "atyp").def(py::init<>()).def(py::init([](const std::string &mtxt) {
atyp obj;
obj.mtxt = mtxt;
return obj;
}));
m.def("rtrn_valu_atyp", rtrn_valu_atyp);
m.def("rtrn_rref_atyp", rtrn_rref_atyp);

View File

@ -73,8 +73,8 @@ namespace pybind11_tests {
namespace class_sh_inheritance {
TEST_SUBMODULE(class_sh_inheritance, m) {
py::class_<base, py::smart_holder>(m, "base");
py::class_<drvd, base, py::smart_holder>(m, "drvd");
py::classh<base>(m, "base");
py::classh<drvd, base>(m, "drvd");
auto rvto = py::return_value_policy::take_ownership;
@ -89,9 +89,9 @@ TEST_SUBMODULE(class_sh_inheritance, m) {
m.def("pass_shcp_drvd", pass_shcp_drvd);
// __init__ needed for Python inheritance.
py::class_<base1, py::smart_holder>(m, "base1").def(py::init<>());
py::class_<base2, py::smart_holder>(m, "base2").def(py::init<>());
py::class_<drvd2, base1, base2, py::smart_holder>(m, "drvd2");
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);

View File

@ -46,9 +46,7 @@ namespace pybind11_tests {
namespace class_sh_unique_ptr_member {
TEST_SUBMODULE(class_sh_unique_ptr_member, m) {
py::class_<pointee, py::smart_holder>(m, "pointee")
.def(py::init<>())
.def("get_int", &pointee::get_int);
py::classh<pointee>(m, "pointee").def(py::init<>()).def("get_int", &pointee::get_int);
m.def("make_unique_pointee", make_unique_pointee);