test_classh_wip.cpp 8.02 KB
Newer Older
1
2
3
4
5
#include "pybind11_tests.h"

#include <pybind11/classh.h>

#include <memory>
6
#include <string>
7
8
9
10

namespace pybind11_tests {
namespace classh_wip {

11
struct mpty { std::string mtxt; };
12
13
14
15
16
17
18
19

mpty        rtrn_mpty_valu() { mpty obj; return obj; }
mpty&&      rtrn_mpty_rref() { mpty obj; return std::move(obj); }
mpty const& rtrn_mpty_cref() { static mpty obj; return obj; }
mpty&       rtrn_mpty_mref() { static mpty obj; return obj; }
mpty const* rtrn_mpty_cptr() { static mpty obj; return &obj; }
mpty*       rtrn_mpty_mptr() { static mpty obj; return &obj; }

20
21
22
23
24
25
std::string pass_mpty_valu(mpty obj)        { return "pass_valu:" + obj.mtxt; }
std::string pass_mpty_rref(mpty&& obj)      { return "pass_rref:" + obj.mtxt; }
std::string pass_mpty_cref(mpty const& obj) { return "pass_cref:" + obj.mtxt; }
std::string pass_mpty_mref(mpty& obj)       { return "pass_mref:" + obj.mtxt; }
std::string pass_mpty_cptr(mpty const* obj) { return "pass_cptr:" + obj->mtxt; }
std::string pass_mpty_mptr(mpty* obj)       { return "pass_mptr:" + obj->mtxt; }
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

std::shared_ptr<mpty>       rtrn_mpty_shmp() { return std::shared_ptr<mpty>(new mpty); }
std::shared_ptr<mpty const> rtrn_mpty_shcp() { return std::shared_ptr<mpty const>(new mpty); }

const char* pass_mpty_shmp(std::shared_ptr<mpty>)       { return "load_shmp"; }
const char* pass_mpty_shcp(std::shared_ptr<mpty const>) { return "load_shcp"; }

std::unique_ptr<mpty>       rtrn_mpty_uqmp() { return std::unique_ptr<mpty>(new mpty); }
std::unique_ptr<mpty const> rtrn_mpty_uqcp() { return std::unique_ptr<mpty const>(new mpty); }

const char* pass_mpty_uqmp(std::unique_ptr<mpty>)       { return "load_uqmp"; }
const char* pass_mpty_uqcp(std::unique_ptr<mpty const>) { return "load_uqcp"; }

}  // namespace classh_wip
}  // namespace pybind11_tests

namespace pybind11 {
namespace detail {

using namespace pybind11_tests::classh_wip;

47
48
49
50
51
52
53
54
55
56
57
58
59
60
template <typename T>
struct smart_holder_type_caster_load {
    bool load(handle src, bool /*convert*/) {
        if (!isinstance<T>(src)) return false;
        auto inst = reinterpret_cast<instance *>(src.ptr());
        auto v_h = inst->get_value_and_holder(get_type_info(typeid(T)));
        smhldr_ptr = &v_h.holder<pybindit::memory::smart_holder>();
        return true;
    }

  protected:
    pybindit::memory::smart_holder* smhldr_ptr = nullptr;
};

61
template <>
62
struct type_caster<mpty> : smart_holder_type_caster_load<mpty> {
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
    static constexpr auto name = _<mpty>();

    // static handle cast(mpty, ...)
    // is redundant (leads to ambiguous overloads).

    static handle cast(mpty&& /*src*/,
                       return_value_policy /*policy*/,
                       handle /*parent*/) {
        return str("cast_rref").release();
    }

    static handle cast(mpty const& /*src*/,
                       return_value_policy /*policy*/,
                       handle /*parent*/) {
        return str("cast_cref").release();
    }

    static handle cast(mpty& /*src*/,
                       return_value_policy /*policy*/,
                       handle /*parent*/) {
        return str("cast_mref").release();
    }

    static handle cast(mpty const* /*src*/,
                       return_value_policy /*policy*/,
                       handle /*parent*/) {
        return str("cast_cptr").release();
    }

    static handle cast(mpty* /*src*/,
                       return_value_policy /*policy*/,
                       handle /*parent*/) {
        return str("cast_mptr").release();
    }

    template <typename T_>
    using cast_op_type = conditional_t<
        std::is_same<remove_reference_t<T_>, mpty const*>::value, mpty const*,
        conditional_t<
            std::is_same<remove_reference_t<T_>, mpty*>::value, mpty*,
            conditional_t<
                std::is_same<T_, mpty const&>::value, mpty const&,
                conditional_t<
                    std::is_same<T_, mpty&>::value, mpty&,
                    conditional_t<
                        std::is_same<T_, mpty&&>::value, mpty&&,
                        mpty>>>>>;

111
112
113
114
115
116
    operator mpty()        { return smhldr_ptr->lvalue_ref<mpty>(); }
    operator mpty&&() &&   { return smhldr_ptr->rvalue_ref<mpty>(); }
    operator mpty const&() { return smhldr_ptr->lvalue_ref<mpty>(); }
    operator mpty&()       { return smhldr_ptr->lvalue_ref<mpty>(); }
    operator mpty const*() { return smhldr_ptr->as_raw_ptr_unowned<mpty>(); }
    operator mpty*()       { return smhldr_ptr->as_raw_ptr_unowned<mpty>(); }
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
};

template <>
struct type_caster<std::shared_ptr<mpty>> {
    static constexpr auto name = _<std::shared_ptr<mpty>>();

    static handle cast(const std::shared_ptr<mpty>& /*src*/,
                       return_value_policy /*policy*/,
                       handle /*parent*/) {
        return str("cast_shmp").release();
    }

    template <typename> using cast_op_type = std::shared_ptr<mpty>;

    operator std::shared_ptr<mpty>() { return rtrn_mpty_shmp(); }

133
134
    bool load(handle src, bool /*convert*/) {
        if (!isinstance<mpty>(src)) return false;
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
        return true;
    }
};

template <>
struct type_caster<std::shared_ptr<mpty const>> {
    static constexpr auto name = _<std::shared_ptr<mpty const>>();

    static handle cast(const std::shared_ptr<mpty const>& /*src*/,
                       return_value_policy /*policy*/,
                       handle /*parent*/) {
        return str("cast_shcp").release();
    }

    template <typename> using cast_op_type = std::shared_ptr<mpty const>;

    operator std::shared_ptr<mpty const>() { return rtrn_mpty_shcp(); }

153
154
    bool load(handle src, bool /*convert*/) {
        if (!isinstance<mpty>(src)) return false;
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
        return true;
    }
};

template <>
struct type_caster<std::unique_ptr<mpty>> {
    static constexpr auto name = _<std::unique_ptr<mpty>>();

    static handle cast(std::unique_ptr<mpty>&& /*src*/,
                       return_value_policy /*policy*/,
                       handle /*parent*/) {
        return str("cast_uqmp").release();
    }

    template <typename> using cast_op_type = std::unique_ptr<mpty>;

    operator std::unique_ptr<mpty>() { return rtrn_mpty_uqmp(); }

173
174
    bool load(handle src, bool /*convert*/) {
        if (!isinstance<mpty>(src)) return false;
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
        return true;
    }
};

template <>
struct type_caster<std::unique_ptr<mpty const>> {
    static constexpr auto name = _<std::unique_ptr<mpty const>>();

    static handle cast(std::unique_ptr<mpty const>&& /*src*/,
                       return_value_policy /*policy*/,
                       handle /*parent*/) {
        return str("cast_uqcp").release();
    }

    template <typename> using cast_op_type = std::unique_ptr<mpty const>;

    operator std::unique_ptr<mpty const>() { return rtrn_mpty_uqcp(); }

193
194
    bool load(handle src, bool /*convert*/) {
        if (!isinstance<mpty>(src)) return false;
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
        return true;
    }
};

}  // namespace detail
}  // namespace pybind11

namespace pybind11_tests {
namespace classh_wip {

TEST_SUBMODULE(classh_wip, m) {
    namespace py = pybind11;

    py::classh<mpty>(m, "mpty")
        .def(py::init<>())
210
211
        .def(py::init([](const std::string& mtxt) {
            mpty obj; obj.mtxt = mtxt; return obj; }))
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
    ;

    m.def("rtrn_mpty_valu", rtrn_mpty_valu);
    m.def("rtrn_mpty_rref", rtrn_mpty_rref);
    m.def("rtrn_mpty_cref", rtrn_mpty_cref);
    m.def("rtrn_mpty_mref", rtrn_mpty_mref);
    m.def("rtrn_mpty_cptr", rtrn_mpty_cptr);
    m.def("rtrn_mpty_mptr", rtrn_mpty_mptr);

    m.def("pass_mpty_valu", pass_mpty_valu);
    m.def("pass_mpty_rref", pass_mpty_rref);
    m.def("pass_mpty_cref", pass_mpty_cref);
    m.def("pass_mpty_mref", pass_mpty_mref);
    m.def("pass_mpty_cptr", pass_mpty_cptr);
    m.def("pass_mpty_mptr", pass_mpty_mptr);

    m.def("rtrn_mpty_shmp", rtrn_mpty_shmp);
    m.def("rtrn_mpty_shcp", rtrn_mpty_shcp);

    m.def("pass_mpty_shmp", pass_mpty_shmp);
    m.def("pass_mpty_shcp", pass_mpty_shcp);

    m.def("rtrn_mpty_uqmp", rtrn_mpty_uqmp);
    m.def("rtrn_mpty_uqcp", rtrn_mpty_uqcp);

    m.def("pass_mpty_uqmp", pass_mpty_uqmp);
    m.def("pass_mpty_uqcp", pass_mpty_uqcp);
}

}  // namespace classh_wip
}  // namespace pybind11_tests