|
1 | 1 | #include "pybind11_tests.h"
|
2 | 2 |
|
3 |
| -namespace adl_one { |
4 |
| -struct type_one {}; |
5 |
| -struct type_caster_one { |
| 3 | +// adl = Argument Dependent Lookup |
| 4 | + |
| 5 | +namespace adl_mock { |
| 6 | +struct type_mock {}; |
| 7 | +struct mock_caster { |
6 | 8 | static int num() { return 101; }
|
7 | 9 | };
|
8 |
| -type_caster_one pybind11_select_caster(type_one *); |
9 |
| -} // namespace adl_one |
| 10 | +mock_caster pybind11_select_caster(type_mock *); |
| 11 | +} // namespace adl_mock |
| 12 | + |
| 13 | +namespace adl_mrc { // minimal real caster |
10 | 14 |
|
11 |
| -namespace adl_two { |
12 |
| -struct type_two {}; |
13 |
| -struct type_caster_two { |
14 |
| - static int num() { return 202; } |
| 15 | +struct type_mrc { |
| 16 | + int value = -9999; |
15 | 17 | };
|
16 |
| -type_caster_two pybind11_select_caster(type_two *); |
17 |
| -} // namespace adl_two |
| 18 | + |
| 19 | +struct minimal_real_caster { |
| 20 | + static constexpr auto name = py::detail::const_name<type_mrc>(); |
| 21 | + |
| 22 | + static py::handle |
| 23 | + cast(type_mrc const &src, py::return_value_policy /*policy*/, py::handle /*parent*/) { |
| 24 | + return py::int_(src.value + 1000).release(); |
| 25 | + } |
| 26 | + |
| 27 | + // Maximizing simplicity. This will go terribly wrong for other arg types. |
| 28 | + template <typename> |
| 29 | + using cast_op_type = const type_mrc &; |
| 30 | + |
| 31 | + operator type_mrc const &() { |
| 32 | + static type_mrc obj; |
| 33 | + obj.value = 303; |
| 34 | + return obj; |
| 35 | + } |
| 36 | + |
| 37 | + bool load(py::handle src, bool /*convert*/) { |
| 38 | + // Only accepts str, but the value is ignored. |
| 39 | + return py::isinstance<py::str>(src); |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +minimal_real_caster pybind11_select_caster(type_mrc *); |
| 44 | + |
| 45 | +} // namespace adl_mrc |
18 | 46 |
|
19 | 47 | TEST_SUBMODULE(make_caster_adl, m) {
|
20 |
| - m.def("num_one", []() { return py::detail::make_caster<adl_one::type_one>::num(); }); |
21 |
| - m.def("num_two", []() { return py::detail::make_caster<adl_two::type_two>::num(); }); |
| 48 | + m.def("num_mock", []() { return py::detail::make_caster<adl_mock::type_mock>::num(); }); |
| 49 | + |
| 50 | + m.def("obj_mrc_return", []() { |
| 51 | + adl_mrc::type_mrc obj; |
| 52 | + obj.value = 404; |
| 53 | + return obj; |
| 54 | + }); |
| 55 | + m.def("obj_mrc_arg", [](adl_mrc::type_mrc const &obj) { return obj.value + 2000; }); |
22 | 56 | }
|
0 commit comments