github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/pybind11/tests/local_bindings.h (about) 1 #pragma once 2 #include "pybind11_tests.h" 3 4 #include <utility> 5 6 /// Simple class used to test py::local: 7 template <int> 8 class LocalBase { 9 public: 10 explicit LocalBase(int i) : i(i) {} 11 int i = -1; 12 }; 13 14 /// Registered with py::module_local in both main and secondary modules: 15 using LocalType = LocalBase<0>; 16 /// Registered without py::module_local in both modules: 17 using NonLocalType = LocalBase<1>; 18 /// A second non-local type (for stl_bind tests): 19 using NonLocal2 = LocalBase<2>; 20 /// Tests within-module, different-compilation-unit local definition conflict: 21 using LocalExternal = LocalBase<3>; 22 /// Mixed: registered local first, then global 23 using MixedLocalGlobal = LocalBase<4>; 24 /// Mixed: global first, then local 25 using MixedGlobalLocal = LocalBase<5>; 26 27 /// Registered with py::module_local only in the secondary module: 28 using ExternalType1 = LocalBase<6>; 29 using ExternalType2 = LocalBase<7>; 30 31 using LocalVec = std::vector<LocalType>; 32 using LocalVec2 = std::vector<NonLocal2>; 33 using LocalMap = std::unordered_map<std::string, LocalType>; 34 using NonLocalVec = std::vector<NonLocalType>; 35 using NonLocalVec2 = std::vector<NonLocal2>; 36 using NonLocalMap = std::unordered_map<std::string, NonLocalType>; 37 using NonLocalMap2 = std::unordered_map<std::string, uint8_t>; 38 39 // Exception that will be caught via the module local translator. 40 class LocalException : public std::exception { 41 public: 42 explicit LocalException(const char *m) : message{m} {} 43 const char *what() const noexcept override { return message.c_str(); } 44 45 private: 46 std::string message = ""; 47 }; 48 49 // Exception that will be registered with register_local_exception_translator 50 class LocalSimpleException : public std::exception { 51 public: 52 explicit LocalSimpleException(const char *m) : message{m} {} 53 const char *what() const noexcept override { return message.c_str(); } 54 55 private: 56 std::string message = ""; 57 }; 58 59 PYBIND11_MAKE_OPAQUE(LocalVec); 60 PYBIND11_MAKE_OPAQUE(LocalVec2); 61 PYBIND11_MAKE_OPAQUE(LocalMap); 62 PYBIND11_MAKE_OPAQUE(NonLocalVec); 63 // PYBIND11_MAKE_OPAQUE(NonLocalVec2); // same type as LocalVec2 64 PYBIND11_MAKE_OPAQUE(NonLocalMap); 65 PYBIND11_MAKE_OPAQUE(NonLocalMap2); 66 67 // Simple bindings (used with the above): 68 template <typename T, int Adjust = 0, typename... Args> 69 py::class_<T> bind_local(Args &&...args) { 70 return py::class_<T>(std::forward<Args>(args)...).def(py::init<int>()).def("get", [](T &i) { 71 return i.i + Adjust; 72 }); 73 }; 74 75 // Simulate a foreign library base class (to match the example in the docs): 76 namespace pets { 77 class Pet { 78 public: 79 explicit Pet(std::string name) : name_(std::move(name)) {} 80 std::string name_; 81 const std::string &name() const { return name_; } 82 }; 83 } // namespace pets 84 85 struct MixGL { 86 int i; 87 explicit MixGL(int i) : i{i} {} 88 }; 89 struct MixGL2 { 90 int i; 91 explicit MixGL2(int i) : i{i} {} 92 };