Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:51

0001 #pragma once
0002 #include "pybind11_tests.h"
0003 
0004 #include <utility>
0005 
0006 /// Simple class used to test py::local:
0007 template <int>
0008 class LocalBase {
0009 public:
0010     explicit LocalBase(int i) : i(i) {}
0011     int i = -1;
0012 };
0013 
0014 /// Registered with py::module_local in both main and secondary modules:
0015 using LocalType = LocalBase<0>;
0016 /// Registered without py::module_local in both modules:
0017 using NonLocalType = LocalBase<1>;
0018 /// A second non-local type (for stl_bind tests):
0019 using NonLocal2 = LocalBase<2>;
0020 /// Tests within-module, different-compilation-unit local definition conflict:
0021 using LocalExternal = LocalBase<3>;
0022 /// Mixed: registered local first, then global
0023 using MixedLocalGlobal = LocalBase<4>;
0024 /// Mixed: global first, then local
0025 using MixedGlobalLocal = LocalBase<5>;
0026 
0027 /// Registered with py::module_local only in the secondary module:
0028 using ExternalType1 = LocalBase<6>;
0029 using ExternalType2 = LocalBase<7>;
0030 
0031 using LocalVec = std::vector<LocalType>;
0032 using LocalVec2 = std::vector<NonLocal2>;
0033 using LocalMap = std::unordered_map<std::string, LocalType>;
0034 using NonLocalVec = std::vector<NonLocalType>;
0035 using NonLocalVec2 = std::vector<NonLocal2>;
0036 using NonLocalMap = std::unordered_map<std::string, NonLocalType>;
0037 using NonLocalMap2 = std::unordered_map<std::string, uint8_t>;
0038 
0039 // Exception that will be caught via the module local translator.
0040 class LocalException : public std::exception {
0041 public:
0042     explicit LocalException(const char *m) : message{m} {}
0043     const char *what() const noexcept override { return message.c_str(); }
0044 
0045 private:
0046     std::string message = "";
0047 };
0048 
0049 // Exception that will be registered with register_local_exception_translator
0050 class LocalSimpleException : public std::exception {
0051 public:
0052     explicit LocalSimpleException(const char *m) : message{m} {}
0053     const char *what() const noexcept override { return message.c_str(); }
0054 
0055 private:
0056     std::string message = "";
0057 };
0058 
0059 PYBIND11_MAKE_OPAQUE(LocalVec);
0060 PYBIND11_MAKE_OPAQUE(LocalVec2);
0061 PYBIND11_MAKE_OPAQUE(LocalMap);
0062 PYBIND11_MAKE_OPAQUE(NonLocalVec);
0063 // PYBIND11_MAKE_OPAQUE(NonLocalVec2); // same type as LocalVec2
0064 PYBIND11_MAKE_OPAQUE(NonLocalMap);
0065 PYBIND11_MAKE_OPAQUE(NonLocalMap2);
0066 
0067 // Simple bindings (used with the above):
0068 template <typename T, int Adjust = 0, typename... Args>
0069 py::class_<T> bind_local(Args &&...args) {
0070     return py::class_<T>(std::forward<Args>(args)...).def(py::init<int>()).def("get", [](T &i) {
0071         return i.i + Adjust;
0072     });
0073 };
0074 
0075 // Simulate a foreign library base class (to match the example in the docs):
0076 namespace pets {
0077 class Pet {
0078 public:
0079     explicit Pet(std::string name) : name_(std::move(name)) {}
0080     std::string name_;
0081     const std::string &name() const { return name_; }
0082 };
0083 } // namespace pets
0084 
0085 struct MixGL {
0086     int i;
0087     explicit MixGL(int i) : i{i} {}
0088 };
0089 struct MixGL2 {
0090     int i;
0091     explicit MixGL2(int i) : i{i} {}
0092 };