2020-11-20 05:51:11 +00:00
|
|
|
#include "pybind11_tests.h"
|
|
|
|
|
2020-11-20 19:24:40 +00:00
|
|
|
#include <pybind11/vptr_holder.h>
|
2020-11-20 05:51:11 +00:00
|
|
|
|
2020-11-20 19:24:40 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <variant>
|
2020-11-20 05:51:11 +00:00
|
|
|
|
2020-11-20 19:24:40 +00:00
|
|
|
namespace pybind11_tests {
|
2020-11-20 05:51:11 +00:00
|
|
|
|
2020-11-20 19:24:40 +00:00
|
|
|
using pybind11::vptr;
|
2020-11-20 05:51:11 +00:00
|
|
|
|
2020-11-20 19:24:40 +00:00
|
|
|
vptr<double> from_raw() { return vptr<double>{new double{3}}; }
|
2020-11-20 05:51:11 +00:00
|
|
|
|
2020-11-20 19:24:40 +00:00
|
|
|
vptr<double> from_unique() {
|
|
|
|
return vptr<double>{std::unique_ptr<double>(new double{5})};
|
2020-11-20 05:51:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 19:24:40 +00:00
|
|
|
vptr<double> from_shared() {
|
|
|
|
return vptr<double>{std::shared_ptr<double>(new double{7})};
|
2020-11-20 05:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_SUBMODULE(variant_unique_shared, m) {
|
|
|
|
|
|
|
|
m.def("from_raw", from_raw);
|
|
|
|
m.def("from_unique", from_unique);
|
|
|
|
m.def("from_shared", from_shared);
|
|
|
|
|
2020-11-20 19:24:40 +00:00
|
|
|
py::class_<vptr<double>>(m, "vptr_double")
|
2020-11-20 05:51:11 +00:00
|
|
|
.def(py::init<>())
|
2020-11-20 19:24:40 +00:00
|
|
|
.def("ownership_type", &vptr<double>::ownership_type)
|
2020-11-20 05:51:11 +00:00
|
|
|
.def("get_value",
|
2020-11-20 19:24:40 +00:00
|
|
|
[](vptr<double> &v) {
|
2020-11-20 05:51:11 +00:00
|
|
|
auto p = v.get();
|
|
|
|
if (p)
|
|
|
|
return *p;
|
|
|
|
return -1.;
|
|
|
|
})
|
|
|
|
.def("get_unique",
|
2020-11-20 19:24:40 +00:00
|
|
|
[](vptr<double> &v) {
|
2020-11-20 05:51:11 +00:00
|
|
|
v.get_unique();
|
|
|
|
return;
|
|
|
|
})
|
2020-12-04 22:17:17 +00:00
|
|
|
.def("get_shared",
|
|
|
|
[](vptr<double> &v) {
|
|
|
|
v.get_shared();
|
|
|
|
return;
|
|
|
|
})
|
|
|
|
.def("disown_unique", [](vptr<double> &v) {
|
|
|
|
v.get_unique().reset();
|
2020-11-20 05:51:11 +00:00
|
|
|
return;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace pybind11_tests
|