@@ -17,6 +17,17 @@ struct atyp { // Short for "any type".
17
17
atyp (atyp &&other) { mtxt = other.mtxt + " _MvCtor" ; }
18
18
};
19
19
20
+ struct uconsumer { // unique_ptr consumer
21
+ std::unique_ptr<atyp> held;
22
+ bool valid () const { return static_cast <bool >(held); }
23
+
24
+ void pass_valu (std::unique_ptr<atyp> obj) { held = std::move (obj); }
25
+ void pass_rref (std::unique_ptr<atyp> &&obj) { held = std::move (obj); }
26
+ std::unique_ptr<atyp> rtrn_valu () { return std::move (held); }
27
+ std::unique_ptr<atyp>& rtrn_lref () { return held; }
28
+ const std::unique_ptr<atyp> &rtrn_cref () { return held; }
29
+ };
30
+
20
31
// clang-format off
21
32
22
33
atyp rtrn_valu () { atyp obj{" rtrn_valu" }; return obj; }
@@ -57,7 +68,11 @@ std::string pass_udcp(std::unique_ptr<atyp const, sddc> obj) { return "pass_udcp
57
68
58
69
// Helpers for testing.
59
70
std::string get_mtxt (atyp const &obj) { return obj.mtxt ; }
71
+ std::ptrdiff_t get_ptr (atyp const &obj) { return reinterpret_cast <std::ptrdiff_t >(&obj); }
72
+
60
73
std::unique_ptr<atyp> unique_ptr_roundtrip (std::unique_ptr<atyp> obj) { return obj; }
74
+ const std::unique_ptr<atyp>& unique_ptr_cref_roundtrip (const std::unique_ptr<atyp>& obj) { return obj; }
75
+
61
76
struct SharedPtrStash {
62
77
std::vector<std::shared_ptr<const atyp>> stash;
63
78
void Add (std::shared_ptr<const atyp> obj) { stash.push_back (obj); }
@@ -67,6 +82,7 @@ struct SharedPtrStash {
67
82
} // namespace pybind11_tests
68
83
69
84
PYBIND11_SMART_HOLDER_TYPE_CASTERS (pybind11_tests::class_sh_basic::atyp)
85
+ PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_basic::uconsumer)
70
86
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_basic::SharedPtrStash)
71
87
72
88
namespace pybind11_tests {
@@ -112,10 +128,23 @@ TEST_SUBMODULE(class_sh_basic, m) {
112
128
m.def (" pass_udmp" , pass_udmp);
113
129
m.def (" pass_udcp" , pass_udcp);
114
130
131
+ py::classh<uconsumer>(m, " uconsumer" )
132
+ .def (py::init<>())
133
+ .def (" valid" , &uconsumer::valid)
134
+ .def (" pass_valu" , &uconsumer::pass_valu)
135
+ .def (" pass_rref" , &uconsumer::pass_rref)
136
+ .def (" rtrn_valu" , &uconsumer::rtrn_valu)
137
+ .def (" rtrn_lref" , &uconsumer::rtrn_lref)
138
+ .def (" rtrn_cref" , &uconsumer::rtrn_cref);
139
+
115
140
// Helpers for testing.
116
141
// These require selected functions above to work first, as indicated:
117
142
m.def (" get_mtxt" , get_mtxt); // pass_cref
143
+ m.def (" get_ptr" , get_ptr); // pass_cref
144
+
118
145
m.def (" unique_ptr_roundtrip" , unique_ptr_roundtrip); // pass_uqmp, rtrn_uqmp
146
+ m.def (" unique_ptr_cref_roundtrip" , unique_ptr_cref_roundtrip);
147
+
119
148
py::classh<SharedPtrStash>(m, " SharedPtrStash" )
120
149
.def (py::init<>())
121
150
.def (" Add" , &SharedPtrStash::Add, py::arg (" obj" ));
0 commit comments