File indexing completed on 2025-01-18 10:17:54
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <pybind11/stl.h>
0012 #include <pybind11/stl_bind.h>
0013
0014 #include "local_bindings.h"
0015 #include "pybind11_tests.h"
0016
0017 #include <numeric>
0018 #include <utility>
0019
0020 TEST_SUBMODULE(local_bindings, m) {
0021
0022 m.def("load_external1", [](ExternalType1 &e) { return e.i; });
0023 m.def("load_external2", [](ExternalType2 &e) { return e.i; });
0024
0025
0026
0027 bind_local<LocalType, -1>(m, "LocalType", py::module_local()).def("get3", [](LocalType &t) {
0028 return t.i + 3;
0029 });
0030
0031 m.def("local_value", [](LocalType &l) { return l.i; });
0032
0033
0034
0035
0036 bind_local<NonLocalType, 0>(m, "NonLocalType")
0037 .def(py::init<int>())
0038 .def("get", [](LocalType &i) { return i.i; });
0039
0040
0041
0042
0043
0044
0045
0046 m.def("register_local_external", [m]() {
0047 auto main = py::module_::import("pybind11_tests");
0048 if (py::hasattr(main, "class_")) {
0049 bind_local<LocalExternal, 7>(m, "LocalExternal", py::module_local());
0050 } else {
0051 throw std::runtime_error("test_class not enabled");
0052 }
0053 });
0054
0055
0056
0057 py::bind_vector<LocalVec>(m, "LocalVec");
0058 py::bind_map<LocalMap>(m, "LocalMap");
0059
0060 py::bind_vector<NonLocalVec>(m, "NonLocalVec");
0061 py::bind_map<NonLocalMap>(m, "NonLocalMap");
0062
0063
0064
0065 bind_local<NonLocal2, 10>(m, "NonLocal2");
0066 py::bind_vector<LocalVec2>(m, "LocalVec2", py::module_local());
0067 py::bind_map<NonLocalMap2>(m, "NonLocalMap2", py::module_local(false));
0068
0069
0070
0071
0072 m.def("register_mixed_global", [m]() {
0073 bind_local<MixedGlobalLocal, 100>(m, "MixedGlobalLocal", py::module_local(false));
0074 });
0075 m.def("register_mixed_local", [m]() {
0076 bind_local<MixedLocalGlobal, 1000>(m, "MixedLocalGlobal", py::module_local());
0077 });
0078 m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); });
0079 m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); });
0080
0081
0082 m.def("local_cpp_types_addr",
0083 []() { return (uintptr_t) &py::detail::get_local_internals().registered_types_cpp; });
0084
0085
0086 m.def("load_vector_via_caster",
0087 [](std::vector<int> v) { return std::accumulate(v.begin(), v.end(), 0); });
0088
0089
0090 m.def("return_self", [](LocalVec *v) { return v; });
0091 m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); });
0092
0093 class Cat : public pets::Pet {
0094 public:
0095 explicit Cat(std::string name) : Pet(std::move(name)) {}
0096 };
0097 py::class_<pets::Pet>(m, "Pet", py::module_local()).def("get_name", &pets::Pet::name);
0098
0099 py::class_<Cat, pets::Pet>(m, "Cat").def(py::init<std::string>());
0100 m.def("pet_name", [](pets::Pet &p) { return p.name(); });
0101
0102 py::class_<MixGL>(m, "MixGL").def(py::init<int>());
0103 m.def("get_gl_value", [](MixGL &o) { return o.i + 10; });
0104
0105 py::class_<MixGL2>(m, "MixGL2").def(py::init<int>());
0106 }