Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | #include "pybind11_tests.h" |
| 3 | |
| 4 | /// Simple class used to test py::local: |
| 5 | template <int> class LocalBase { |
| 6 | public: |
| 7 | LocalBase(int i) : i(i) { } |
| 8 | int i = -1; |
| 9 | }; |
| 10 | |
| 11 | /// Registered with py::local in both main and secondary modules: |
| 12 | using LocalType = LocalBase<0>; |
| 13 | /// Registered without py::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>; |
Jason Rhinelander | 4b15923 | 2017-08-04 13:05:12 -0400 | [diff] [blame] | 19 | /// Mixed: registered local first, then global |
| 20 | using MixedLocalGlobal = LocalBase<4>; |
Jason Rhinelander | 5e14aa6 | 2017-08-17 11:38:05 -0400 | [diff] [blame^] | 21 | /// Mixed: global first, then local |
Jason Rhinelander | 4b15923 | 2017-08-04 13:05:12 -0400 | [diff] [blame] | 22 | using MixedGlobalLocal = LocalBase<5>; |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 23 | |
Dean Moldovan | 8d3cedb | 2017-08-13 03:03:06 +0200 | [diff] [blame] | 24 | using LocalVec = std::vector<LocalType>; |
| 25 | using LocalVec2 = std::vector<NonLocal2>; |
| 26 | using LocalMap = std::unordered_map<std::string, LocalType>; |
| 27 | using NonLocalVec = std::vector<NonLocalType>; |
| 28 | using NonLocalVec2 = std::vector<NonLocal2>; |
| 29 | using NonLocalMap = std::unordered_map<std::string, NonLocalType>; |
| 30 | using NonLocalMap2 = std::unordered_map<std::string, uint8_t>; |
| 31 | |
Jason Rhinelander | 5e14aa6 | 2017-08-17 11:38:05 -0400 | [diff] [blame^] | 32 | PYBIND11_MAKE_OPAQUE(LocalVec); |
| 33 | PYBIND11_MAKE_OPAQUE(LocalVec2); |
| 34 | PYBIND11_MAKE_OPAQUE(LocalMap); |
| 35 | PYBIND11_MAKE_OPAQUE(NonLocalVec); |
| 36 | //PYBIND11_MAKE_OPAQUE(NonLocalVec2); // same type as LocalVec2 |
| 37 | PYBIND11_MAKE_OPAQUE(NonLocalMap); |
| 38 | PYBIND11_MAKE_OPAQUE(NonLocalMap2); |
| 39 | |
| 40 | |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 41 | // Simple bindings (used with the above): |
| 42 | template <typename T, int Adjust, typename... Args> |
| 43 | py::class_<T> bind_local(Args && ...args) { |
| 44 | return py::class_<T>(std::forward<Args>(args)...) |
| 45 | .def(py::init<int>()) |
| 46 | .def("get", [](T &i) { return i.i + Adjust; }); |
| 47 | }; |
Jason Rhinelander | 5e14aa6 | 2017-08-17 11:38:05 -0400 | [diff] [blame^] | 48 | |
| 49 | // Simulate a foreign library base class (to match the example in the docs): |
| 50 | namespace pets { |
| 51 | class Pet { |
| 52 | public: |
| 53 | Pet(std::string name) : name_(name) {} |
| 54 | std::string name_; |
| 55 | const std::string &name() { return name_; } |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | struct MixGL { int i; MixGL(int i) : i{i} {} }; |
| 60 | struct MixGL2 { int i; MixGL2(int i) : i{i} {} }; |