112391Sjason@lowepower.com/*
212391Sjason@lowepower.com    tests/test_local_bindings.cpp -- tests the py::module_local class feature which makes a class
312391Sjason@lowepower.com                                     binding local to the module in which it is defined.
412391Sjason@lowepower.com
512391Sjason@lowepower.com    Copyright (c) 2017 Jason Rhinelander <jason@imaginary.ca>
612391Sjason@lowepower.com
712391Sjason@lowepower.com    All rights reserved. Use of this source code is governed by a
812391Sjason@lowepower.com    BSD-style license that can be found in the LICENSE file.
912391Sjason@lowepower.com*/
1012391Sjason@lowepower.com
1112391Sjason@lowepower.com#include "pybind11_tests.h"
1212391Sjason@lowepower.com#include "local_bindings.h"
1312391Sjason@lowepower.com#include <pybind11/stl.h>
1412391Sjason@lowepower.com#include <pybind11/stl_bind.h>
1512391Sjason@lowepower.com#include <numeric>
1612391Sjason@lowepower.com
1712391Sjason@lowepower.comTEST_SUBMODULE(local_bindings, m) {
1812391Sjason@lowepower.com    // test_load_external
1912391Sjason@lowepower.com    m.def("load_external1", [](ExternalType1 &e) { return e.i; });
2012391Sjason@lowepower.com    m.def("load_external2", [](ExternalType2 &e) { return e.i; });
2112391Sjason@lowepower.com
2212391Sjason@lowepower.com    // test_local_bindings
2312391Sjason@lowepower.com    // Register a class with py::module_local:
2412391Sjason@lowepower.com    bind_local<LocalType, -1>(m, "LocalType", py::module_local())
2512391Sjason@lowepower.com        .def("get3", [](LocalType &t) { return t.i + 3; })
2612391Sjason@lowepower.com        ;
2712391Sjason@lowepower.com
2812391Sjason@lowepower.com    m.def("local_value", [](LocalType &l) { return l.i; });
2912391Sjason@lowepower.com
3012391Sjason@lowepower.com    // test_nonlocal_failure
3112391Sjason@lowepower.com    // The main pybind11 test module is loaded first, so this registration will succeed (the second
3212391Sjason@lowepower.com    // one, in pybind11_cross_module_tests.cpp, is designed to fail):
3312391Sjason@lowepower.com    bind_local<NonLocalType, 0>(m, "NonLocalType")
3412391Sjason@lowepower.com        .def(py::init<int>())
3512391Sjason@lowepower.com        .def("get", [](LocalType &i) { return i.i; })
3612391Sjason@lowepower.com        ;
3712391Sjason@lowepower.com
3812391Sjason@lowepower.com    // test_duplicate_local
3912391Sjason@lowepower.com    // py::module_local declarations should be visible across compilation units that get linked together;
4012391Sjason@lowepower.com    // this tries to register a duplicate local.  It depends on a definition in test_class.cpp and
4112391Sjason@lowepower.com    // should raise a runtime error from the duplicate definition attempt.  If test_class isn't
4212391Sjason@lowepower.com    // available it *also* throws a runtime error (with "test_class not enabled" as value).
4312391Sjason@lowepower.com    m.def("register_local_external", [m]() {
4412391Sjason@lowepower.com        auto main = py::module::import("pybind11_tests");
4512391Sjason@lowepower.com        if (py::hasattr(main, "class_")) {
4612391Sjason@lowepower.com            bind_local<LocalExternal, 7>(m, "LocalExternal", py::module_local());
4712391Sjason@lowepower.com        }
4812391Sjason@lowepower.com        else throw std::runtime_error("test_class not enabled");
4912391Sjason@lowepower.com    });
5012391Sjason@lowepower.com
5112391Sjason@lowepower.com    // test_stl_bind_local
5212391Sjason@lowepower.com    // stl_bind.h binders defaults to py::module_local if the types are local or converting:
5312391Sjason@lowepower.com    py::bind_vector<LocalVec>(m, "LocalVec");
5412391Sjason@lowepower.com    py::bind_map<LocalMap>(m, "LocalMap");
5512391Sjason@lowepower.com    // and global if the type (or one of the types, for the map) is global:
5612391Sjason@lowepower.com    py::bind_vector<NonLocalVec>(m, "NonLocalVec");
5712391Sjason@lowepower.com    py::bind_map<NonLocalMap>(m, "NonLocalMap");
5812391Sjason@lowepower.com
5912391Sjason@lowepower.com    // test_stl_bind_global
6012391Sjason@lowepower.com    // They can, however, be overridden to global using `py::module_local(false)`:
6112391Sjason@lowepower.com    bind_local<NonLocal2, 10>(m, "NonLocal2");
6212391Sjason@lowepower.com    py::bind_vector<LocalVec2>(m, "LocalVec2", py::module_local());
6312391Sjason@lowepower.com    py::bind_map<NonLocalMap2>(m, "NonLocalMap2", py::module_local(false));
6412391Sjason@lowepower.com
6512391Sjason@lowepower.com    // test_mixed_local_global
6612391Sjason@lowepower.com    // We try this both with the global type registered first and vice versa (the order shouldn't
6712391Sjason@lowepower.com    // matter).
6812391Sjason@lowepower.com    m.def("register_mixed_global", [m]() {
6912391Sjason@lowepower.com        bind_local<MixedGlobalLocal, 100>(m, "MixedGlobalLocal", py::module_local(false));
7012391Sjason@lowepower.com    });
7112391Sjason@lowepower.com    m.def("register_mixed_local", [m]() {
7212391Sjason@lowepower.com        bind_local<MixedLocalGlobal, 1000>(m, "MixedLocalGlobal", py::module_local());
7312391Sjason@lowepower.com    });
7412391Sjason@lowepower.com    m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); });
7512391Sjason@lowepower.com    m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); });
7612391Sjason@lowepower.com
7712391Sjason@lowepower.com    // test_internal_locals_differ
7812391Sjason@lowepower.com    m.def("local_cpp_types_addr", []() { return (uintptr_t) &py::detail::registered_local_types_cpp(); });
7912391Sjason@lowepower.com
8012391Sjason@lowepower.com    // test_stl_caster_vs_stl_bind
8112391Sjason@lowepower.com    m.def("load_vector_via_caster", [](std::vector<int> v) {
8212391Sjason@lowepower.com        return std::accumulate(v.begin(), v.end(), 0);
8312391Sjason@lowepower.com    });
8412391Sjason@lowepower.com
8512391Sjason@lowepower.com    // test_cross_module_calls
8612391Sjason@lowepower.com    m.def("return_self", [](LocalVec *v) { return v; });
8712391Sjason@lowepower.com    m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); });
8812391Sjason@lowepower.com
8912391Sjason@lowepower.com    class Cat : public pets::Pet { public: Cat(std::string name) : Pet(name) {}; };
9012391Sjason@lowepower.com    py::class_<pets::Pet>(m, "Pet", py::module_local())
9112391Sjason@lowepower.com        .def("get_name", &pets::Pet::name);
9212391Sjason@lowepower.com    // Binding for local extending class:
9312391Sjason@lowepower.com    py::class_<Cat, pets::Pet>(m, "Cat")
9412391Sjason@lowepower.com        .def(py::init<std::string>());
9512391Sjason@lowepower.com    m.def("pet_name", [](pets::Pet &p) { return p.name(); });
9612391Sjason@lowepower.com
9712391Sjason@lowepower.com    py::class_<MixGL>(m, "MixGL").def(py::init<int>());
9812391Sjason@lowepower.com    m.def("get_gl_value", [](MixGL &o) { return o.i + 10; });
9912391Sjason@lowepower.com
10012391Sjason@lowepower.com    py::class_<MixGL2>(m, "MixGL2").def(py::init<int>());
10112391Sjason@lowepower.com}
102