2021-01-11 19:55:03 +00:00
|
|
|
#include "pybind11_tests.h"
|
|
|
|
|
2021-01-25 06:15:58 +00:00
|
|
|
#include <pybind11/smart_holder.h>
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
#include <memory>
|
2021-01-11 23:58:14 +00:00
|
|
|
#include <string>
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
namespace pybind11_tests {
|
2021-01-25 07:06:33 +00:00
|
|
|
namespace class_sh_basic {
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-23 16:39:26 +00:00
|
|
|
struct atyp { // Short for "any type".
|
2021-01-12 18:53:42 +00:00
|
|
|
std::string mtxt;
|
2021-02-12 02:00:22 +00:00
|
|
|
atyp() : mtxt("DefaultConstructor") {}
|
|
|
|
atyp(const std::string &mtxt_) : mtxt(mtxt_) {}
|
|
|
|
atyp(const atyp &other) { mtxt = other.mtxt + ".CpCtor"; }
|
|
|
|
atyp(atyp &&other) { mtxt = other.mtxt + ".MvCtor"; }
|
2021-01-12 18:53:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// clang-format off
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-23 16:39:26 +00:00
|
|
|
atyp rtrn_valu_atyp() { atyp obj{"rtrn_valu"}; return obj; }
|
|
|
|
atyp&& rtrn_rref_atyp() { static atyp obj; obj.mtxt = "rtrn_rref"; return std::move(obj); }
|
|
|
|
atyp const& rtrn_cref_atyp() { static atyp obj; obj.mtxt = "rtrn_cref"; return obj; }
|
|
|
|
atyp& rtrn_mref_atyp() { static atyp obj; obj.mtxt = "rtrn_mref"; return obj; }
|
|
|
|
atyp const* rtrn_cptr_atyp() { return new atyp{"rtrn_cptr"}; }
|
|
|
|
atyp* rtrn_mptr_atyp() { return new atyp{"rtrn_mptr"}; }
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-23 16:39:26 +00:00
|
|
|
std::string pass_valu_atyp(atyp obj) { return "pass_valu:" + obj.mtxt; }
|
|
|
|
std::string pass_rref_atyp(atyp&& obj) { return "pass_rref:" + obj.mtxt; }
|
|
|
|
std::string pass_cref_atyp(atyp const& obj) { return "pass_cref:" + obj.mtxt; }
|
|
|
|
std::string pass_mref_atyp(atyp& obj) { return "pass_mref:" + obj.mtxt; }
|
|
|
|
std::string pass_cptr_atyp(atyp const* obj) { return "pass_cptr:" + obj->mtxt; }
|
|
|
|
std::string pass_mptr_atyp(atyp* obj) { return "pass_mptr:" + obj->mtxt; }
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-23 16:39:26 +00:00
|
|
|
std::shared_ptr<atyp> rtrn_shmp_atyp() { return std::shared_ptr<atyp >(new atyp{"rtrn_shmp"}); }
|
|
|
|
std::shared_ptr<atyp const> rtrn_shcp_atyp() { return std::shared_ptr<atyp const>(new atyp{"rtrn_shcp"}); }
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-23 16:39:26 +00:00
|
|
|
std::string pass_shmp_atyp(std::shared_ptr<atyp> obj) { return "pass_shmp:" + obj->mtxt; }
|
|
|
|
std::string pass_shcp_atyp(std::shared_ptr<atyp const> obj) { return "pass_shcp:" + obj->mtxt; }
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-23 16:39:26 +00:00
|
|
|
std::unique_ptr<atyp> rtrn_uqmp_atyp() { return std::unique_ptr<atyp >(new atyp{"rtrn_uqmp"}); }
|
|
|
|
std::unique_ptr<atyp const> rtrn_uqcp_atyp() { return std::unique_ptr<atyp const>(new atyp{"rtrn_uqcp"}); }
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-23 16:39:26 +00:00
|
|
|
std::string pass_uqmp_atyp(std::unique_ptr<atyp > obj) { return "pass_uqmp:" + obj->mtxt; }
|
|
|
|
std::string pass_uqcp_atyp(std::unique_ptr<atyp const> obj) { return "pass_uqcp:" + obj->mtxt; }
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-02-02 23:35:19 +00:00
|
|
|
struct sddm : std::default_delete<atyp > {};
|
|
|
|
struct sddc : std::default_delete<atyp const> {};
|
2021-02-01 22:06:58 +00:00
|
|
|
|
2021-02-02 23:35:19 +00:00
|
|
|
std::unique_ptr<atyp, sddm> rtrn_udmp_atyp() { return std::unique_ptr<atyp, sddm>(new atyp{"rtrn_udmp"}); }
|
|
|
|
std::unique_ptr<atyp const, sddc> rtrn_udcp_atyp() { return std::unique_ptr<atyp const, sddc>(new atyp{"rtrn_udcp"}); }
|
2021-02-01 22:06:58 +00:00
|
|
|
|
2021-02-02 23:35:19 +00:00
|
|
|
std::string pass_udmp_atyp(std::unique_ptr<atyp, sddm> obj) { return "pass_udmp:" + obj->mtxt; }
|
|
|
|
std::string pass_udcp_atyp(std::unique_ptr<atyp const, sddc> obj) { return "pass_udcp:" + obj->mtxt; }
|
2021-02-01 22:06:58 +00:00
|
|
|
|
2021-01-12 18:53:42 +00:00
|
|
|
// clang-format on
|
|
|
|
|
2021-01-15 19:44:07 +00:00
|
|
|
// Helpers for testing.
|
2021-01-23 16:39:26 +00:00
|
|
|
std::string get_mtxt(atyp const &obj) { return obj.mtxt; }
|
|
|
|
std::unique_ptr<atyp> unique_ptr_roundtrip(std::unique_ptr<atyp> obj) { return obj; }
|
2021-01-12 21:26:22 +00:00
|
|
|
|
2021-01-25 07:06:33 +00:00
|
|
|
} // namespace class_sh_basic
|
2021-01-12 18:53:42 +00:00
|
|
|
} // namespace pybind11_tests
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-25 07:06:33 +00:00
|
|
|
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_basic::atyp)
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
namespace pybind11_tests {
|
2021-01-25 07:06:33 +00:00
|
|
|
namespace class_sh_basic {
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-25 07:06:33 +00:00
|
|
|
TEST_SUBMODULE(class_sh_basic, m) {
|
2021-01-11 19:55:03 +00:00
|
|
|
namespace py = pybind11;
|
|
|
|
|
2021-01-28 15:31:40 +00:00
|
|
|
py::classh<atyp>(m, "atyp").def(py::init<>()).def(py::init([](const std::string &mtxt) {
|
|
|
|
atyp obj;
|
|
|
|
obj.mtxt = mtxt;
|
|
|
|
return obj;
|
|
|
|
}));
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-23 16:39:26 +00:00
|
|
|
m.def("rtrn_valu_atyp", rtrn_valu_atyp);
|
|
|
|
m.def("rtrn_rref_atyp", rtrn_rref_atyp);
|
|
|
|
m.def("rtrn_cref_atyp", rtrn_cref_atyp);
|
|
|
|
m.def("rtrn_mref_atyp", rtrn_mref_atyp);
|
|
|
|
m.def("rtrn_cptr_atyp", rtrn_cptr_atyp);
|
|
|
|
m.def("rtrn_mptr_atyp", rtrn_mptr_atyp);
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-23 16:39:26 +00:00
|
|
|
m.def("pass_valu_atyp", pass_valu_atyp);
|
|
|
|
m.def("pass_rref_atyp", pass_rref_atyp);
|
|
|
|
m.def("pass_cref_atyp", pass_cref_atyp);
|
|
|
|
m.def("pass_mref_atyp", pass_mref_atyp);
|
|
|
|
m.def("pass_cptr_atyp", pass_cptr_atyp);
|
|
|
|
m.def("pass_mptr_atyp", pass_mptr_atyp);
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-23 16:39:26 +00:00
|
|
|
m.def("rtrn_shmp_atyp", rtrn_shmp_atyp);
|
|
|
|
m.def("rtrn_shcp_atyp", rtrn_shcp_atyp);
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-23 16:39:26 +00:00
|
|
|
m.def("pass_shmp_atyp", pass_shmp_atyp);
|
|
|
|
m.def("pass_shcp_atyp", pass_shcp_atyp);
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-23 16:39:26 +00:00
|
|
|
m.def("rtrn_uqmp_atyp", rtrn_uqmp_atyp);
|
|
|
|
m.def("rtrn_uqcp_atyp", rtrn_uqcp_atyp);
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-23 16:39:26 +00:00
|
|
|
m.def("pass_uqmp_atyp", pass_uqmp_atyp);
|
|
|
|
m.def("pass_uqcp_atyp", pass_uqcp_atyp);
|
2021-01-12 21:26:22 +00:00
|
|
|
|
2021-02-02 23:35:19 +00:00
|
|
|
m.def("rtrn_udmp_atyp", rtrn_udmp_atyp);
|
|
|
|
m.def("rtrn_udcp_atyp", rtrn_udcp_atyp);
|
2021-02-01 22:06:58 +00:00
|
|
|
|
2021-02-02 23:35:19 +00:00
|
|
|
m.def("pass_udmp_atyp", pass_udmp_atyp);
|
|
|
|
m.def("pass_udcp_atyp", pass_udcp_atyp);
|
2021-02-01 22:06:58 +00:00
|
|
|
|
2021-01-15 19:44:07 +00:00
|
|
|
// Helpers for testing.
|
|
|
|
// These require selected functions above to work first, as indicated:
|
2021-01-23 16:39:26 +00:00
|
|
|
m.def("get_mtxt", get_mtxt); // pass_cref_atyp
|
|
|
|
m.def("unique_ptr_roundtrip", unique_ptr_roundtrip); // pass_uqmp_atyp, rtrn_uqmp_atyp
|
2021-01-28 22:25:19 +00:00
|
|
|
|
|
|
|
m.def("py_type_handle_of_atyp", []() {
|
|
|
|
return py::type::handle_of<atyp>(); // Exercises static_cast in this function.
|
|
|
|
});
|
2021-01-11 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 07:06:33 +00:00
|
|
|
} // namespace class_sh_basic
|
2021-01-12 18:53:42 +00:00
|
|
|
} // namespace pybind11_tests
|