local_bindings.h 2.78 KB
Newer Older
1
2
3
#pragma once
#include "pybind11_tests.h"

4
5
#include <utility>

6
/// Simple class used to test py::local:
7
8
template <int>
class LocalBase {
9
public:
10
    explicit LocalBase(int i) : i(i) {}
11
12
13
    int i = -1;
};

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
18
19
20
21
using NonLocalType = LocalBase<1>;
/// A second non-local type (for stl_bind tests):
using NonLocal2 = LocalBase<2>;
/// Tests within-module, different-compilation-unit local definition conflict:
using LocalExternal = LocalBase<3>;
22
23
/// Mixed: registered local first, then global
using MixedLocalGlobal = LocalBase<4>;
24
/// Mixed: global first, then local
25
using MixedGlobalLocal = LocalBase<5>;
26

27
28
29
30
/// Registered with py::module_local only in the secondary module:
using ExternalType1 = LocalBase<6>;
using ExternalType2 = LocalBase<7>;

31
32
33
34
35
36
37
38
using LocalVec = std::vector<LocalType>;
using LocalVec2 = std::vector<NonLocal2>;
using LocalMap = std::unordered_map<std::string, LocalType>;
using NonLocalVec = std::vector<NonLocalType>;
using NonLocalVec2 = std::vector<NonLocal2>;
using NonLocalMap = std::unordered_map<std::string, NonLocalType>;
using NonLocalMap2 = std::unordered_map<std::string, uint8_t>;

39
40
41
// Exception that will be caught via the module local translator.
class LocalException : public std::exception {
public:
42
43
44
    explicit LocalException(const char *m) : message{m} {}
    const char *what() const noexcept override { return message.c_str(); }

45
46
47
48
49
50
51
private:
    std::string message = "";
};

// Exception that will be registered with register_local_exception_translator
class LocalSimpleException : public std::exception {
public:
52
53
54
    explicit LocalSimpleException(const char *m) : message{m} {}
    const char *what() const noexcept override { return message.c_str(); }

55
56
57
58
private:
    std::string message = "";
};

59
60
61
62
PYBIND11_MAKE_OPAQUE(LocalVec);
PYBIND11_MAKE_OPAQUE(LocalVec2);
PYBIND11_MAKE_OPAQUE(LocalMap);
PYBIND11_MAKE_OPAQUE(NonLocalVec);
63
// PYBIND11_MAKE_OPAQUE(NonLocalVec2); // same type as LocalVec2
64
65
66
PYBIND11_MAKE_OPAQUE(NonLocalMap);
PYBIND11_MAKE_OPAQUE(NonLocalMap2);

67
// Simple bindings (used with the above):
68
template <typename T, int Adjust = 0, typename... Args>
69
70
71
72
py::class_<T> bind_local(Args &&...args) {
    return py::class_<T>(std::forward<Args>(args)...).def(py::init<int>()).def("get", [](T &i) {
        return i.i + Adjust;
    });
73
};
74
75
76
77
78

// Simulate a foreign library base class (to match the example in the docs):
namespace pets {
class Pet {
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
86
87
88
89
90
91
92
struct MixGL {
    int i;
    explicit MixGL(int i) : i{i} {}
};
struct MixGL2 {
    int i;
    explicit MixGL2(int i) : i{i} {}
};