7
7
#include < memory>
8
8
9
9
namespace pybind11_tests {
10
- namespace class_sp_trampoline_weak_ptr {
10
+ namespace potentially_slicing_shared_ptr {
11
11
12
12
struct VirtBase {
13
13
virtual ~VirtBase () = default ;
@@ -17,66 +17,69 @@ struct VirtBase {
17
17
struct PyVirtBase : VirtBase, py::trampoline_self_life_support {
18
18
using VirtBase::VirtBase;
19
19
int get_code () override { PYBIND11_OVERRIDE (int , VirtBase, get_code); }
20
-
21
- ~PyVirtBase () override {
22
- fflush (stderr);
23
- printf (" \n LOOOK ~PyVirtBase()\n " );
24
- fflush (stdout);
25
- }
26
20
};
27
21
28
- struct WpOwner {
29
- void set_wp (const std::shared_ptr<VirtBase> &sp) { wp = sp; }
22
+ std::shared_ptr<VirtBase> rtrn_obj_cast_shared_ptr (py::handle obj) {
23
+ return obj.cast <std::shared_ptr<VirtBase>>();
24
+ }
25
+
26
+ std::shared_ptr<VirtBase> rtrn_potentially_slicing_shared_ptr (py::handle obj) {
27
+ return py::potentially_slicing_shared_ptr<VirtBase>(obj);
28
+ }
29
+
30
+ struct SpOwner {
31
+ void set_sp (const std::shared_ptr<VirtBase> &sp_) { sp = sp_; }
30
32
31
33
int get_code () {
32
- auto sp = wp.lock ();
33
34
if (!sp) {
34
- return -999 ;
35
+ return -888 ;
35
36
}
36
37
return sp->get_code ();
37
38
}
38
39
39
40
private:
40
- std::weak_ptr <VirtBase> wp ;
41
+ std::shared_ptr <VirtBase> sp ;
41
42
};
42
43
43
- struct SpOwner {
44
- void set_sp (const std::shared_ptr<VirtBase> &sp_ ) { sp = sp_ ; }
44
+ struct WpOwner {
45
+ void set_wp (const std::shared_ptr<VirtBase> &sp ) { wp = sp ; }
45
46
46
47
int get_code () {
48
+ auto sp = wp.lock ();
47
49
if (!sp) {
48
- return -888 ;
50
+ return -999 ;
49
51
}
50
52
return sp->get_code ();
51
53
}
52
54
53
55
private:
54
- std::shared_ptr <VirtBase> sp ;
56
+ std::weak_ptr <VirtBase> wp ;
55
57
};
56
58
57
- std::shared_ptr<VirtBase> pass_through_sp_VirtBase (const std::shared_ptr<VirtBase> &sp) {
58
- return sp;
59
- }
60
-
61
- } // namespace class_sp_trampoline_weak_ptr
59
+ } // namespace potentially_slicing_shared_ptr
62
60
} // namespace pybind11_tests
63
61
64
- using namespace pybind11_tests ::class_sp_trampoline_weak_ptr ;
62
+ using namespace pybind11_tests ::potentially_slicing_shared_ptr ;
65
63
66
- TEST_SUBMODULE (class_sp_trampoline_weak_ptr , m) {
67
- py::class_ <VirtBase, std::shared_ptr<VirtBase> , PyVirtBase>(m, " VirtBase" )
64
+ TEST_SUBMODULE (potentially_slicing_shared_ptr , m) {
65
+ py::classh <VirtBase, PyVirtBase>(m, " VirtBase" )
68
66
.def (py::init<>())
69
67
.def (" get_code" , &VirtBase::get_code);
70
68
71
- py::class_<WpOwner>(m, " WpOwner" )
72
- .def (py::init<>())
73
- .def (" set_wp" , &WpOwner::set_wp)
74
- .def (" get_code" , &WpOwner::get_code);
69
+ m.def (" rtrn_obj_cast_shared_ptr" , rtrn_obj_cast_shared_ptr);
70
+ m.def (" rtrn_potentially_slicing_shared_ptr" , rtrn_potentially_slicing_shared_ptr);
75
71
76
- py::class_ <SpOwner>(m, " SpOwner" )
72
+ py::classh <SpOwner>(m, " SpOwner" )
77
73
.def (py::init<>())
78
74
.def (" set_sp" , &SpOwner::set_sp)
79
75
.def (" get_code" , &SpOwner::get_code);
80
76
81
- m.def (" pass_through_sp_VirtBase" , pass_through_sp_VirtBase);
77
+ py::classh<WpOwner>(m, " WpOwner" )
78
+ .def (py::init<>())
79
+ .def (" set_wp" , &WpOwner::set_wp)
80
+ .def (" set_wp_potentially_slicing" ,
81
+ [](WpOwner &self, py::handle obj) {
82
+ self.set_wp (py::potentially_slicing_shared_ptr<VirtBase>(obj));
83
+ })
84
+ .def (" get_code" , &WpOwner::get_code);
82
85
}
0 commit comments