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
+
1
5
#include " pybind11_tests.h"
2
6
3
7
#include < memory>
@@ -9,31 +13,31 @@ struct mpty {};
9
13
10
14
// clang-format off
11
15
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; }
18
22
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" ; }
25
29
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); }
28
32
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" ; }
31
35
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); }
34
38
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" ; }
37
41
38
42
// clang-format on
39
43
@@ -90,10 +94,10 @@ struct type_caster<mpty> {
90
94
91
95
// clang-format off
92
96
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 (); }
97
101
operator mpty const *() { static mpty obj; return &obj; }
98
102
operator mpty*() { static mpty obj; return &obj; }
99
103
@@ -115,7 +119,7 @@ struct type_caster<std::shared_ptr<mpty>> {
115
119
template <typename >
116
120
using cast_op_type = std::shared_ptr<mpty>;
117
121
118
- operator std::shared_ptr<mpty>() { return rtrn_mpty_shmp (); }
122
+ operator std::shared_ptr<mpty>() { return rtrn_shmp (); }
119
123
120
124
bool load (handle /* src*/ , bool /* convert*/ ) { return true ; }
121
125
};
@@ -133,7 +137,7 @@ struct type_caster<std::shared_ptr<mpty const>> {
133
137
template <typename >
134
138
using cast_op_type = std::shared_ptr<mpty const >;
135
139
136
- operator std::shared_ptr<mpty const >() { return rtrn_mpty_shcp (); }
140
+ operator std::shared_ptr<mpty const >() { return rtrn_shcp (); }
137
141
138
142
bool load (handle /* src*/ , bool /* convert*/ ) { return true ; }
139
143
};
@@ -150,7 +154,7 @@ struct type_caster<std::unique_ptr<mpty>> {
150
154
template <typename >
151
155
using cast_op_type = std::unique_ptr<mpty>;
152
156
153
- operator std::unique_ptr<mpty>() { return rtrn_mpty_uqmp (); }
157
+ operator std::unique_ptr<mpty>() { return rtrn_uqmp (); }
154
158
155
159
bool load (handle /* src*/ , bool /* convert*/ ) { return true ; }
156
160
};
@@ -168,7 +172,7 @@ struct type_caster<std::unique_ptr<mpty const>> {
168
172
template <typename >
169
173
using cast_op_type = std::unique_ptr<mpty const >;
170
174
171
- operator std::unique_ptr<mpty const >() { return rtrn_mpty_uqcp (); }
175
+ operator std::unique_ptr<mpty const >() { return rtrn_uqcp (); }
172
176
173
177
bool load (handle /* src*/ , bool /* convert*/ ) { return true ; }
174
178
};
@@ -180,31 +184,31 @@ namespace pybind11_tests {
180
184
namespace type_caster_bare_interface {
181
185
182
186
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 );
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 );
208
212
}
209
213
210
214
} // namespace type_caster_bare_interface
0 commit comments