1+ // Systematically exercises the detail::type_caster<> interface. This is going a step in the
2+ // direction of an integration test, to ensure multiple components of pybind11 work together
3+ // correctly. It is also useful to show the type_caster<> interface virtually clutter-free.
4+
15#include " pybind11_tests.h"
26
37#include < memory>
@@ -9,31 +13,31 @@ struct mpty {};
913
1014// clang-format off
1115
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; }
16+ mpty rtrn_valu () { mpty obj; return obj; }
17+ mpty&& rtrn_rref () { static mpty obj; return std::move (obj); }
18+ mpty const & rtrn_cref () { static mpty obj; return obj; }
19+ mpty& rtrn_mref () { static mpty obj; return obj; }
20+ mpty const * rtrn_cptr () { return new mpty; }
21+ mpty* rtrn_mptr () { return new mpty; }
1822
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" ; }
23+ const char * pass_valu (mpty) { return " load_valu" ; }
24+ const char * pass_rref (mpty&&) { return " load_rref" ; }
25+ const char * pass_cref (mpty const &) { return " load_cref" ; }
26+ const char * pass_mref (mpty&) { return " load_mref" ; }
27+ const char * pass_cptr (mpty const *) { return " load_cptr" ; }
28+ const char * pass_mptr (mpty*) { return " load_mptr" ; }
2529
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); }
30+ std::shared_ptr<mpty> rtrn_shmp () { return std::shared_ptr<mpty >(new mpty); }
31+ std::shared_ptr<mpty const > rtrn_shcp () { return std::shared_ptr<mpty const >(new mpty); }
2832
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" ; }
33+ const char * pass_shmp (std::shared_ptr<mpty>) { return " load_shmp" ; }
34+ const char * pass_shcp (std::shared_ptr<mpty const >) { return " load_shcp" ; }
3135
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); }
36+ std::unique_ptr<mpty> rtrn_uqmp () { return std::unique_ptr<mpty >(new mpty); }
37+ std::unique_ptr<mpty const > rtrn_uqcp () { return std::unique_ptr<mpty const >(new mpty); }
3438
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" ; }
39+ const char * pass_uqmp (std::unique_ptr<mpty>) { return " load_uqmp" ; }
40+ const char * pass_uqcp (std::unique_ptr<mpty const >) { return " load_uqcp" ; }
3741
3842// clang-format on
3943
@@ -90,10 +94,10 @@ struct type_caster<mpty> {
9094
9195 // clang-format off
9296
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 () { return rtrn_valu (); }
98+ operator mpty&&() && { return rtrn_rref (); }
99+ operator mpty const &() { return rtrn_cref (); }
100+ operator mpty&() { return rtrn_mref (); }
97101 operator mpty const *() { static mpty obj; return &obj; }
98102 operator mpty*() { static mpty obj; return &obj; }
99103
@@ -115,7 +119,7 @@ struct type_caster<std::shared_ptr<mpty>> {
115119 template <typename >
116120 using cast_op_type = std::shared_ptr<mpty>;
117121
118- operator std::shared_ptr<mpty>() { return rtrn_mpty_shmp (); }
122+ operator std::shared_ptr<mpty>() { return rtrn_shmp (); }
119123
120124 bool load (handle /* src*/ , bool /* convert*/ ) { return true ; }
121125};
@@ -133,7 +137,7 @@ struct type_caster<std::shared_ptr<mpty const>> {
133137 template <typename >
134138 using cast_op_type = std::shared_ptr<mpty const >;
135139
136- operator std::shared_ptr<mpty const >() { return rtrn_mpty_shcp (); }
140+ operator std::shared_ptr<mpty const >() { return rtrn_shcp (); }
137141
138142 bool load (handle /* src*/ , bool /* convert*/ ) { return true ; }
139143};
@@ -150,7 +154,7 @@ struct type_caster<std::unique_ptr<mpty>> {
150154 template <typename >
151155 using cast_op_type = std::unique_ptr<mpty>;
152156
153- operator std::unique_ptr<mpty>() { return rtrn_mpty_uqmp (); }
157+ operator std::unique_ptr<mpty>() { return rtrn_uqmp (); }
154158
155159 bool load (handle /* src*/ , bool /* convert*/ ) { return true ; }
156160};
@@ -168,7 +172,7 @@ struct type_caster<std::unique_ptr<mpty const>> {
168172 template <typename >
169173 using cast_op_type = std::unique_ptr<mpty const >;
170174
171- operator std::unique_ptr<mpty const >() { return rtrn_mpty_uqcp (); }
175+ operator std::unique_ptr<mpty const >() { return rtrn_uqcp (); }
172176
173177 bool load (handle /* src*/ , bool /* convert*/ ) { return true ; }
174178};
@@ -180,31 +184,31 @@ namespace pybind11_tests {
180184namespace type_caster_bare_interface {
181185
182186TEST_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 );
187+ m.def (" rtrn_valu " , rtrn_valu );
188+ m.def (" rtrn_rref " , rtrn_rref );
189+ m.def (" rtrn_cref " , rtrn_cref );
190+ m.def (" rtrn_mref " , rtrn_mref );
191+ m.def (" rtrn_cptr " , rtrn_cptr );
192+ m.def (" rtrn_mptr " , rtrn_mptr );
193+
194+ m.def (" pass_valu " , pass_valu );
195+ m.def (" pass_rref " , pass_rref );
196+ m.def (" pass_cref " , pass_cref );
197+ m.def (" pass_mref " , pass_mref );
198+ m.def (" pass_cptr " , pass_cptr );
199+ m.def (" pass_mptr " , pass_mptr );
200+
201+ m.def (" rtrn_shmp " , rtrn_shmp );
202+ m.def (" rtrn_shcp " , rtrn_shcp );
203+
204+ m.def (" pass_shmp " , pass_shmp );
205+ m.def (" pass_shcp " , pass_shcp );
206+
207+ m.def (" rtrn_uqmp " , rtrn_uqmp );
208+ m.def (" rtrn_uqcp " , rtrn_uqcp );
209+
210+ m.def (" pass_uqmp " , pass_uqmp );
211+ m.def (" pass_uqcp " , pass_uqcp );
208212}
209213
210214} // namespace type_caster_bare_interface
0 commit comments