2020//
2121// Place this in PYBIND11_MODULE to register the enums declared by P11X_DECLARE_ENUM.
2222
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-
2823#define P11X_CAT2 (a, b ) a##b
2924#define P11X_CAT (a, b ) P11X_CAT2(a, b)
3025
@@ -51,7 +46,7 @@ namespace p11x {
5146
5247// Immediately converting the args to a vector outside of the lambda avoids
5348// name collisions.
54- #define P11X_DECLARE_ENUM (py_name, py_base_cls, ...) \
49+ #define P11X_DECLARE_ENUM (py_name, py_base_cls, cpp_type, ...) \
5550 namespace p11x { \
5651 namespace { \
5752 [[maybe_unused]] auto const P11X_CAT (enum_placeholder_, __COUNTER__) = \
@@ -68,10 +63,9 @@ namespace p11x {
6863 } \
6964 } \
7065 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)); \
7569 bool load (handle src, bool ) { \
7670 auto cls = p11x::enums.at (py_name); \
7771 PyObject* tmp = nullptr ; \
@@ -87,7 +81,7 @@ namespace p11x {
8781 } \
8882 static handle cast (decltype (value) obj, return_value_policy, handle) { \
8983 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 (); \
9185 } \
9286 }; \
9387 }
0 commit comments