20
20
//
21
21
// Place this in PYBIND11_MODULE to register the enums declared by P11X_DECLARE_ENUM.
22
22
23
- // a1 includes the opening brace and a2 the closing brace.
24
- // This definition is compatible with older compiler versions compared to
25
- // #define P11X_ENUM_TYPE(...) decltype(std::map{std::pair __VA_ARGS__})::mapped_type
26
- #define P11X_ENUM_TYPE (a1, a2, ...) decltype (std::pair a1, a2)::second_type
27
-
28
23
#define P11X_CAT2 (a, b ) a##b
29
24
#define P11X_CAT (a, b ) P11X_CAT2(a, b)
30
25
@@ -51,7 +46,7 @@ namespace p11x {
51
46
52
47
// Immediately converting the args to a vector outside of the lambda avoids
53
48
// name collisions.
54
- #define P11X_DECLARE_ENUM (py_name, py_base_cls, ...) \
49
+ #define P11X_DECLARE_ENUM (py_name, py_base_cls, cpp_type, ...) \
55
50
namespace p11x { \
56
51
namespace { \
57
52
[[maybe_unused]] auto const P11X_CAT (enum_placeholder_, __COUNTER__) = \
@@ -68,10 +63,9 @@ namespace p11x {
68
63
} \
69
64
} \
70
65
namespace pybind11 ::detail { \
71
- template <> struct type_caster <P11X_ENUM_TYPE(__VA_ARGS__)> { \
72
- using type = P11X_ENUM_TYPE(__VA_ARGS__); \
73
- static_assert (std::is_enum_v<type>, " Not an enum" ); \
74
- PYBIND11_TYPE_CASTER (type, _(py_name)); \
66
+ template <> struct type_caster <cpp_type> { \
67
+ static_assert (std::is_enum_v<cpp_type>, " Not an enum" ); \
68
+ PYBIND11_TYPE_CASTER (cpp_type, _(py_name)); \
75
69
bool load (handle src, bool ) { \
76
70
auto cls = p11x::enums.at (py_name); \
77
71
PyObject* tmp = nullptr ; \
@@ -87,7 +81,7 @@ namespace p11x {
87
81
} \
88
82
static handle cast (decltype (value) obj, return_value_policy, handle) { \
89
83
auto cls = p11x::enums.at (py_name); \
90
- return cls (std::underlying_type_t <type >(obj)).inc_ref (); \
84
+ return cls (std::underlying_type_t <cpp_type >(obj)).inc_ref (); \
91
85
} \
92
86
}; \
93
87
}
0 commit comments