1 #pragma once
2 #include "pybind11_tests.h"
3 
4 /// Simple class used to test py::local:
5 template <int> class LocalBase {
6 public:
LocalBase(int i)7     LocalBase(int i) : i(i) { }
8     int i = -1;
9 };
10 
11 /// Registered with py::module_local in both main and secondary modules:
12 using LocalType = LocalBase<0>;
13 /// Registered without py::module_local in both modules:
14 using NonLocalType = LocalBase<1>;
15 /// A second non-local type (for stl_bind tests):
16 using NonLocal2 = LocalBase<2>;
17 /// Tests within-module, different-compilation-unit local definition conflict:
18 using LocalExternal = LocalBase<3>;
19 /// Mixed: registered local first, then global
20 using MixedLocalGlobal = LocalBase<4>;
21 /// Mixed: global first, then local
22 using MixedGlobalLocal = LocalBase<5>;
23 
24 /// Registered with py::module_local only in the secondary module:
25 using ExternalType1 = LocalBase<6>;
26 using ExternalType2 = LocalBase<7>;
27 
28 using LocalVec = std::vector<LocalType>;
29 using LocalVec2 = std::vector<NonLocal2>;
30 using LocalMap = std::unordered_map<std::string, LocalType>;
31 using NonLocalVec = std::vector<NonLocalType>;
32 using NonLocalVec2 = std::vector<NonLocal2>;
33 using NonLocalMap = std::unordered_map<std::string, NonLocalType>;
34 using NonLocalMap2 = std::unordered_map<std::string, uint8_t>;
35 
36 PYBIND11_MAKE_OPAQUE(LocalVec);
37 PYBIND11_MAKE_OPAQUE(LocalVec2);
38 PYBIND11_MAKE_OPAQUE(LocalMap);
39 PYBIND11_MAKE_OPAQUE(NonLocalVec);
40 //PYBIND11_MAKE_OPAQUE(NonLocalVec2); // same type as LocalVec2
41 PYBIND11_MAKE_OPAQUE(NonLocalMap);
42 PYBIND11_MAKE_OPAQUE(NonLocalMap2);
43 
44 
45 // Simple bindings (used with the above):
46 template <typename T, int Adjust = 0, typename... Args>
bind_local(Args &&...args)47 py::class_<T> bind_local(Args && ...args) {
48     return py::class_<T>(std::forward<Args>(args)...)
49         .def(py::init<int>())
50         .def("get", [](T &i) { return i.i + Adjust; });
51 };
52 
53 // Simulate a foreign library base class (to match the example in the docs):
54 namespace pets {
55 class Pet {
56 public:
Pet(std::string name)57     Pet(std::string name) : name_(name) {}
58     std::string name_;
name()59     const std::string &name() { return name_; }
60 };
61 }
62 
MixGLMixGL63 struct MixGL { int i; MixGL(int i) : i{i} {} };
MixGL2MixGL264 struct MixGL2 { int i; MixGL2(int i) : i{i} {} };
65