File: test_potentially_slicing_weak_ptr.cpp

package info (click to toggle)
pybind11 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,448 kB
  • sloc: cpp: 27,239; python: 13,512; ansic: 4,244; makefile: 204; sh: 36
file content (168 lines) | stat: -rw-r--r-- 5,371 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) 2025 The pybind Community.
// All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#include "pybind11_tests.h"

#include <memory>

namespace pybind11_tests {
namespace potentially_slicing_weak_ptr {

template <int> // Using int as a trick to easily generate multiple types.
struct VirtBase {
    virtual ~VirtBase() = default;
    virtual int get_code() { return 100; }
};

using VirtBaseSH = VirtBase<0>; // for testing with py::smart_holder
using VirtBaseSP = VirtBase<1>; // for testing with std::shared_ptr as holder

// Similar to trampoline_self_life_support
struct trampoline_is_alive_simple {
    std::uint64_t magic_token = 197001010000u;

    trampoline_is_alive_simple() = default;

    ~trampoline_is_alive_simple() { magic_token = 20380118191407u; }

    trampoline_is_alive_simple(const trampoline_is_alive_simple &other) = default;
    trampoline_is_alive_simple(trampoline_is_alive_simple &&other) noexcept
        : magic_token(other.magic_token) {
        other.magic_token = 20380118191407u;
    }

    trampoline_is_alive_simple &operator=(const trampoline_is_alive_simple &) = delete;
    trampoline_is_alive_simple &operator=(trampoline_is_alive_simple &&) = delete;
};

template <typename VB>
const char *determine_trampoline_state(const std::shared_ptr<VB> &sp) {
    if (!sp) {
        return "sp nullptr";
    }
    auto *tias = dynamic_cast<trampoline_is_alive_simple *>(sp.get());
    if (!tias) {
        return "dynamic_cast failed";
    }
    if (tias->magic_token == 197001010000u) {
        return "trampoline alive";
    }
    if (tias->magic_token == 20380118191407u) {
        return "trampoline DEAD";
    }
    return "UNDEFINED BEHAVIOR";
}

struct PyVirtBaseSH : VirtBaseSH, py::trampoline_self_life_support, trampoline_is_alive_simple {
    using VirtBaseSH::VirtBaseSH;
    int get_code() override { PYBIND11_OVERRIDE(int, VirtBaseSH, get_code); }
};

struct PyVirtBaseSP : VirtBaseSP, trampoline_is_alive_simple { // self-life-support not available
    using VirtBaseSP::VirtBaseSP;
    int get_code() override { PYBIND11_OVERRIDE(int, VirtBaseSP, get_code); }
};

template <typename VB>
std::shared_ptr<VB> rtrn_obj_cast_shared_ptr(py::handle obj) {
    return obj.cast<std::shared_ptr<VB>>();
}

// There is no type_caster<std::weak_ptr<VB>>, and to minimize code complexity
// we do not want to add one, therefore we have to return a shared_ptr here.
template <typename VB>
std::shared_ptr<VB> rtrn_potentially_slicing_shared_ptr(py::handle obj) {
    return py::potentially_slicing_weak_ptr<VB>(obj).lock();
}

template <typename VB>
struct SpOwner {
    void set_sp(const std::shared_ptr<VB> &sp_) { sp = sp_; }

    int get_code() const {
        if (!sp) {
            return -888;
        }
        return sp->get_code();
    }

    const char *get_trampoline_state() const { return determine_trampoline_state(sp); }

private:
    std::shared_ptr<VB> sp;
};

template <typename VB>
struct WpOwner {
    void set_wp(const std::weak_ptr<VB> &wp_) { wp = wp_; }

    int get_code() const {
        auto sp = wp.lock();
        if (!sp) {
            return -999;
        }
        return sp->get_code();
    }

    const char *get_trampoline_state() const { return determine_trampoline_state(wp.lock()); }

private:
    std::weak_ptr<VB> wp;
};

template <typename VB>
void wrap(py::module_ &m,
          const char *roc_pyname,
          const char *rps_pyname,
          const char *spo_pyname,
          const char *wpo_pyname) {
    m.def(roc_pyname, rtrn_obj_cast_shared_ptr<VB>);
    m.def(rps_pyname, rtrn_potentially_slicing_shared_ptr<VB>);

    py::classh<SpOwner<VB>>(m, spo_pyname)
        .def(py::init<>())
        .def("set_sp", &SpOwner<VB>::set_sp)
        .def("get_code", &SpOwner<VB>::get_code)
        .def("get_trampoline_state", &SpOwner<VB>::get_trampoline_state);

    py::classh<WpOwner<VB>>(m, wpo_pyname)
        .def(py::init<>())
        .def("set_wp",
             [](WpOwner<VB> &self, py::handle obj) {
                 self.set_wp(obj.cast<std::shared_ptr<VB>>());
             })
        .def("set_wp_potentially_slicing",
             [](WpOwner<VB> &self, py::handle obj) {
                 self.set_wp(py::potentially_slicing_weak_ptr<VB>(obj));
             })
        .def("get_code", &WpOwner<VB>::get_code)
        .def("get_trampoline_state", &WpOwner<VB>::get_trampoline_state);
}

} // namespace potentially_slicing_weak_ptr
} // namespace pybind11_tests

using namespace pybind11_tests::potentially_slicing_weak_ptr;

TEST_SUBMODULE(potentially_slicing_weak_ptr, m) {
    py::classh<VirtBaseSH, PyVirtBaseSH>(m, "VirtBaseSH")
        .def(py::init<>())
        .def("get_code", &VirtBaseSH::get_code);

    py::class_<VirtBaseSP, std::shared_ptr<VirtBaseSP>, PyVirtBaseSP>(m, "VirtBaseSP")
        .def(py::init<>())
        .def("get_code", &VirtBaseSP::get_code);

    wrap<VirtBaseSH>(m,
                     "SH_rtrn_obj_cast_shared_ptr",
                     "SH_rtrn_potentially_slicing_shared_ptr",
                     "SH_SpOwner",
                     "SH_WpOwner");

    wrap<VirtBaseSP>(m,
                     "SP_rtrn_obj_cast_shared_ptr",
                     "SP_rtrn_potentially_slicing_shared_ptr",
                     "SP_SpOwner",
                     "SP_WpOwner");
}