2021-03-04 01:58:42 +00:00
|
|
|
#include "pybind11_tests.h"
|
|
|
|
|
|
|
|
#include <pybind11/smart_holder.h>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace pybind11_tests {
|
|
|
|
namespace test_class_sh_with_alias {
|
|
|
|
|
2021-03-16 13:31:24 +00:00
|
|
|
template <int SerNo> // Using int as a trick to easily generate a series of types.
|
2021-03-04 01:58:42 +00:00
|
|
|
struct Abase {
|
|
|
|
int val = 0;
|
|
|
|
virtual ~Abase() = default;
|
|
|
|
Abase(int val_) : val{val_} {}
|
|
|
|
int Get() const { return val * 10 + 3; }
|
|
|
|
virtual int Add(int other_val) const = 0;
|
|
|
|
|
|
|
|
// Some compilers complain about implicitly defined versions of some of the following:
|
|
|
|
Abase(const Abase &) = default;
|
|
|
|
Abase(Abase &&) = default;
|
|
|
|
Abase &operator=(const Abase &) = default;
|
|
|
|
Abase &operator=(Abase &&) = default;
|
|
|
|
};
|
|
|
|
|
2021-03-16 13:31:24 +00:00
|
|
|
template <int SerNo>
|
|
|
|
struct AbaseAlias : Abase<SerNo> {
|
|
|
|
using Abase<SerNo>::Abase;
|
2021-03-04 01:58:42 +00:00
|
|
|
|
|
|
|
int Add(int other_val) const override {
|
2021-03-16 13:31:24 +00:00
|
|
|
PYBIND11_OVERRIDE_PURE(int, /* Return type */
|
|
|
|
Abase<SerNo>, /* Parent class */
|
|
|
|
Add, /* Name of function in C++ (must match Python name) */
|
2021-03-04 01:58:42 +00:00
|
|
|
other_val);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-16 13:31:24 +00:00
|
|
|
template <>
|
2021-03-19 19:18:39 +00:00
|
|
|
struct AbaseAlias<1> : Abase<1>, py::virtual_overrider_self_life_support {
|
2021-03-16 13:31:24 +00:00
|
|
|
using Abase<1>::Abase;
|
2021-03-04 01:58:42 +00:00
|
|
|
|
2021-03-16 13:31:24 +00:00
|
|
|
int Add(int other_val) const override {
|
|
|
|
PYBIND11_OVERRIDE_PURE(int, /* Return type */
|
|
|
|
Abase<1>, /* Parent class */
|
|
|
|
Add, /* Name of function in C++ (must match Python name) */
|
|
|
|
other_val);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <int SerNo>
|
|
|
|
int AddInCppRawPtr(const Abase<SerNo> *obj, int other_val) {
|
|
|
|
return obj->Add(other_val) * 10 + 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <int SerNo>
|
|
|
|
int AddInCppSharedPtr(std::shared_ptr<Abase<SerNo>> obj, int other_val) {
|
2021-03-04 01:58:42 +00:00
|
|
|
return obj->Add(other_val) * 100 + 11;
|
|
|
|
}
|
|
|
|
|
2021-03-16 13:31:24 +00:00
|
|
|
template <int SerNo>
|
|
|
|
int AddInCppUniquePtr(std::unique_ptr<Abase<SerNo>> obj, int other_val) {
|
2021-03-04 01:58:42 +00:00
|
|
|
return obj->Add(other_val) * 100 + 13;
|
|
|
|
}
|
|
|
|
|
2021-03-16 13:31:24 +00:00
|
|
|
template <int SerNo>
|
|
|
|
void wrap(py::module_ m, const char *py_class_name) {
|
|
|
|
py::classh<Abase<SerNo>, AbaseAlias<SerNo>>(m, py_class_name)
|
|
|
|
.def(py::init<int>(), py::arg("val"))
|
|
|
|
.def("Get", &Abase<SerNo>::Get)
|
|
|
|
.def("Add", &Abase<SerNo>::Add, py::arg("other_val"));
|
|
|
|
|
|
|
|
m.def("AddInCppRawPtr", AddInCppRawPtr<SerNo>, py::arg("obj"), py::arg("other_val"));
|
|
|
|
m.def("AddInCppSharedPtr", AddInCppSharedPtr<SerNo>, py::arg("obj"), py::arg("other_val"));
|
|
|
|
m.def("AddInCppUniquePtr", AddInCppUniquePtr<SerNo>, py::arg("obj"), py::arg("other_val"));
|
|
|
|
}
|
|
|
|
|
2021-03-04 01:58:42 +00:00
|
|
|
} // namespace test_class_sh_with_alias
|
|
|
|
} // namespace pybind11_tests
|
|
|
|
|
2021-03-16 13:31:24 +00:00
|
|
|
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::test_class_sh_with_alias::Abase<0>)
|
|
|
|
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::test_class_sh_with_alias::Abase<1>)
|
2021-03-04 01:58:42 +00:00
|
|
|
|
|
|
|
TEST_SUBMODULE(class_sh_with_alias, m) {
|
|
|
|
using namespace pybind11_tests::test_class_sh_with_alias;
|
2021-03-16 13:31:24 +00:00
|
|
|
wrap<0>(m, "Abase0");
|
|
|
|
wrap<1>(m, "Abase1");
|
2021-03-04 01:58:42 +00:00
|
|
|
}
|