File indexing completed on 2025-01-18 10:17:51
0001 #pragma once
0002 #include "pybind11_tests.h"
0003
0004 #include <utility>
0005
0006
0007 template <int>
0008 class LocalBase {
0009 public:
0010 explicit LocalBase(int i) : i(i) {}
0011 int i = -1;
0012 };
0013
0014
0015 using LocalType = LocalBase<0>;
0016
0017 using NonLocalType = LocalBase<1>;
0018
0019 using NonLocal2 = LocalBase<2>;
0020
0021 using LocalExternal = LocalBase<3>;
0022
0023 using MixedLocalGlobal = LocalBase<4>;
0024
0025 using MixedGlobalLocal = LocalBase<5>;
0026
0027
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
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
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
0064 PYBIND11_MAKE_OPAQUE(NonLocalMap);
0065 PYBIND11_MAKE_OPAQUE(NonLocalMap2);
0066
0067
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
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 }
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 };