|
| 1 | +#include "pybind11_tests.h" |
| 2 | + |
| 3 | +#include <memory> |
| 4 | + |
| 5 | +namespace pybind11_tests { |
| 6 | +namespace type_caster_bare_interface { |
| 7 | + |
| 8 | +struct mpty {}; |
| 9 | + |
| 10 | +// clang-format off |
| 11 | + |
| 12 | +mpty rtrn_mpty_valu() { mpty obj; return obj; } |
| 13 | +mpty&& rtrn_mpty_rref() { static mpty obj; return std::move(obj); } |
| 14 | +mpty const& rtrn_mpty_cref() { static mpty obj; return obj; } |
| 15 | +mpty& rtrn_mpty_mref() { static mpty obj; return obj; } |
| 16 | +mpty const* rtrn_mpty_cptr() { return new mpty; } |
| 17 | +mpty* rtrn_mpty_mptr() { return new mpty; } |
| 18 | + |
| 19 | +const char* pass_mpty_valu(mpty) { return "load_valu"; } |
| 20 | +const char* pass_mpty_rref(mpty&&) { return "load_rref"; } |
| 21 | +const char* pass_mpty_cref(mpty const&) { return "load_cref"; } |
| 22 | +const char* pass_mpty_mref(mpty&) { return "load_mref"; } |
| 23 | +const char* pass_mpty_cptr(mpty const*) { return "load_cptr"; } |
| 24 | +const char* pass_mpty_mptr(mpty*) { return "load_mptr"; } |
| 25 | + |
| 26 | +std::shared_ptr<mpty> rtrn_mpty_shmp() { return std::shared_ptr<mpty >(new mpty); } |
| 27 | +std::shared_ptr<mpty const> rtrn_mpty_shcp() { return std::shared_ptr<mpty const>(new mpty); } |
| 28 | + |
| 29 | +const char* pass_mpty_shmp(std::shared_ptr<mpty>) { return "load_shmp"; } |
| 30 | +const char* pass_mpty_shcp(std::shared_ptr<mpty const>) { return "load_shcp"; } |
| 31 | + |
| 32 | +std::unique_ptr<mpty> rtrn_mpty_uqmp() { return std::unique_ptr<mpty >(new mpty); } |
| 33 | +std::unique_ptr<mpty const> rtrn_mpty_uqcp() { return std::unique_ptr<mpty const>(new mpty); } |
| 34 | + |
| 35 | +const char* pass_mpty_uqmp(std::unique_ptr<mpty>) { return "load_uqmp"; } |
| 36 | +const char* pass_mpty_uqcp(std::unique_ptr<mpty const>) { return "load_uqcp"; } |
| 37 | + |
| 38 | +// clang-format on |
| 39 | + |
| 40 | +} // namespace type_caster_bare_interface |
| 41 | +} // namespace pybind11_tests |
| 42 | + |
| 43 | +namespace pybind11 { |
| 44 | +namespace detail { |
| 45 | + |
| 46 | +using namespace pybind11_tests::type_caster_bare_interface; |
| 47 | + |
| 48 | +template <> |
| 49 | +struct type_caster<mpty> { |
| 50 | + static constexpr auto name = _<mpty>(); |
| 51 | + |
| 52 | + // static handle cast(mpty, ...) |
| 53 | + // is redundant (leads to ambiguous overloads). |
| 54 | + |
| 55 | + static handle cast(mpty && /*src*/, return_value_policy /*policy*/, handle /*parent*/) { |
| 56 | + return str("cast_rref").release(); |
| 57 | + } |
| 58 | + |
| 59 | + static handle cast(mpty const & /*src*/, return_value_policy /*policy*/, handle /*parent*/) { |
| 60 | + return str("cast_cref").release(); |
| 61 | + } |
| 62 | + |
| 63 | + static handle cast(mpty & /*src*/, return_value_policy /*policy*/, handle /*parent*/) { |
| 64 | + return str("cast_mref").release(); |
| 65 | + } |
| 66 | + |
| 67 | + static handle cast(mpty const *src, return_value_policy /*policy*/, handle /*parent*/) { |
| 68 | + delete src; |
| 69 | + return str("cast_cptr").release(); |
| 70 | + } |
| 71 | + |
| 72 | + static handle cast(mpty *src, return_value_policy /*policy*/, handle /*parent*/) { |
| 73 | + delete src; |
| 74 | + return str("cast_mptr").release(); |
| 75 | + } |
| 76 | + |
| 77 | + template <typename T_> |
| 78 | + using cast_op_type = conditional_t< |
| 79 | + std::is_same<remove_reference_t<T_>, mpty const *>::value, |
| 80 | + mpty const *, |
| 81 | + conditional_t< |
| 82 | + std::is_same<remove_reference_t<T_>, mpty *>::value, |
| 83 | + mpty *, |
| 84 | + conditional_t< |
| 85 | + std::is_same<T_, mpty const &>::value, |
| 86 | + mpty const &, |
| 87 | + conditional_t<std::is_same<T_, mpty &>::value, |
| 88 | + mpty &, |
| 89 | + conditional_t<std::is_same<T_, mpty &&>::value, mpty &&, mpty>>>>>; |
| 90 | + |
| 91 | + // clang-format off |
| 92 | + |
| 93 | + operator mpty() { return rtrn_mpty_valu(); } |
| 94 | + operator mpty&&() && { return rtrn_mpty_rref(); } |
| 95 | + operator mpty const&() { return rtrn_mpty_cref(); } |
| 96 | + operator mpty&() { return rtrn_mpty_mref(); } |
| 97 | + operator mpty const*() { static mpty obj; return &obj; } |
| 98 | + operator mpty*() { static mpty obj; return &obj; } |
| 99 | + |
| 100 | + // clang-format on |
| 101 | + |
| 102 | + bool load(handle /*src*/, bool /*convert*/) { return true; } |
| 103 | +}; |
| 104 | + |
| 105 | +template <> |
| 106 | +struct type_caster<std::shared_ptr<mpty>> { |
| 107 | + static constexpr auto name = _<std::shared_ptr<mpty>>(); |
| 108 | + |
| 109 | + static handle cast(const std::shared_ptr<mpty> & /*src*/, |
| 110 | + return_value_policy /*policy*/, |
| 111 | + handle /*parent*/) { |
| 112 | + return str("cast_shmp").release(); |
| 113 | + } |
| 114 | + |
| 115 | + template <typename> |
| 116 | + using cast_op_type = std::shared_ptr<mpty>; |
| 117 | + |
| 118 | + operator std::shared_ptr<mpty>() { return rtrn_mpty_shmp(); } |
| 119 | + |
| 120 | + bool load(handle /*src*/, bool /*convert*/) { return true; } |
| 121 | +}; |
| 122 | + |
| 123 | +template <> |
| 124 | +struct type_caster<std::shared_ptr<mpty const>> { |
| 125 | + static constexpr auto name = _<std::shared_ptr<mpty const>>(); |
| 126 | + |
| 127 | + static handle cast(const std::shared_ptr<mpty const> & /*src*/, |
| 128 | + return_value_policy /*policy*/, |
| 129 | + handle /*parent*/) { |
| 130 | + return str("cast_shcp").release(); |
| 131 | + } |
| 132 | + |
| 133 | + template <typename> |
| 134 | + using cast_op_type = std::shared_ptr<mpty const>; |
| 135 | + |
| 136 | + operator std::shared_ptr<mpty const>() { return rtrn_mpty_shcp(); } |
| 137 | + |
| 138 | + bool load(handle /*src*/, bool /*convert*/) { return true; } |
| 139 | +}; |
| 140 | + |
| 141 | +template <> |
| 142 | +struct type_caster<std::unique_ptr<mpty>> { |
| 143 | + static constexpr auto name = _<std::unique_ptr<mpty>>(); |
| 144 | + |
| 145 | + static handle |
| 146 | + cast(std::unique_ptr<mpty> && /*src*/, return_value_policy /*policy*/, handle /*parent*/) { |
| 147 | + return str("cast_uqmp").release(); |
| 148 | + } |
| 149 | + |
| 150 | + template <typename> |
| 151 | + using cast_op_type = std::unique_ptr<mpty>; |
| 152 | + |
| 153 | + operator std::unique_ptr<mpty>() { return rtrn_mpty_uqmp(); } |
| 154 | + |
| 155 | + bool load(handle /*src*/, bool /*convert*/) { return true; } |
| 156 | +}; |
| 157 | + |
| 158 | +template <> |
| 159 | +struct type_caster<std::unique_ptr<mpty const>> { |
| 160 | + static constexpr auto name = _<std::unique_ptr<mpty const>>(); |
| 161 | + |
| 162 | + static handle cast(std::unique_ptr<mpty const> && /*src*/, |
| 163 | + return_value_policy /*policy*/, |
| 164 | + handle /*parent*/) { |
| 165 | + return str("cast_uqcp").release(); |
| 166 | + } |
| 167 | + |
| 168 | + template <typename> |
| 169 | + using cast_op_type = std::unique_ptr<mpty const>; |
| 170 | + |
| 171 | + operator std::unique_ptr<mpty const>() { return rtrn_mpty_uqcp(); } |
| 172 | + |
| 173 | + bool load(handle /*src*/, bool /*convert*/) { return true; } |
| 174 | +}; |
| 175 | + |
| 176 | +} // namespace detail |
| 177 | +} // namespace pybind11 |
| 178 | + |
| 179 | +namespace pybind11_tests { |
| 180 | +namespace type_caster_bare_interface { |
| 181 | + |
| 182 | +TEST_SUBMODULE(type_caster_bare_interface, m) { |
| 183 | + m.def("rtrn_mpty_valu", rtrn_mpty_valu); |
| 184 | + m.def("rtrn_mpty_rref", rtrn_mpty_rref); |
| 185 | + m.def("rtrn_mpty_cref", rtrn_mpty_cref); |
| 186 | + m.def("rtrn_mpty_mref", rtrn_mpty_mref); |
| 187 | + m.def("rtrn_mpty_cptr", rtrn_mpty_cptr); |
| 188 | + m.def("rtrn_mpty_mptr", rtrn_mpty_mptr); |
| 189 | + |
| 190 | + m.def("pass_mpty_valu", pass_mpty_valu); |
| 191 | + m.def("pass_mpty_rref", pass_mpty_rref); |
| 192 | + m.def("pass_mpty_cref", pass_mpty_cref); |
| 193 | + m.def("pass_mpty_mref", pass_mpty_mref); |
| 194 | + m.def("pass_mpty_cptr", pass_mpty_cptr); |
| 195 | + m.def("pass_mpty_mptr", pass_mpty_mptr); |
| 196 | + |
| 197 | + m.def("rtrn_mpty_shmp", rtrn_mpty_shmp); |
| 198 | + m.def("rtrn_mpty_shcp", rtrn_mpty_shcp); |
| 199 | + |
| 200 | + m.def("pass_mpty_shmp", pass_mpty_shmp); |
| 201 | + m.def("pass_mpty_shcp", pass_mpty_shcp); |
| 202 | + |
| 203 | + m.def("rtrn_mpty_uqmp", rtrn_mpty_uqmp); |
| 204 | + m.def("rtrn_mpty_uqcp", rtrn_mpty_uqcp); |
| 205 | + |
| 206 | + m.def("pass_mpty_uqmp", pass_mpty_uqmp); |
| 207 | + m.def("pass_mpty_uqcp", pass_mpty_uqcp); |
| 208 | +} |
| 209 | + |
| 210 | +} // namespace type_caster_bare_interface |
| 211 | +} // namespace pybind11_tests |
0 commit comments