|
| 1 | +#ifndef MPL_ENUMS_H |
| 2 | +#define MPL_ENUMS_H |
| 3 | + |
| 4 | +#include <pybind11/pybind11.h> |
| 5 | + |
| 6 | +// Extension for pybind11: Pythonic enums. |
| 7 | +// This allows creating classes based on ``enum.*`` types. |
| 8 | +// This code was copied from mplcairo, with some slight tweaks. |
| 9 | +// The API is: |
| 10 | +// |
| 11 | +// - P11X_DECLARE_ENUM(py_name: str, py_base_cls: str, ...: {str, enum value}): |
| 12 | +// py_name: The name to expose in the module. |
| 13 | +// py_base_cls: The name of the enum base class to use. |
| 14 | +// ...: The enum name/value pairs to expose. |
| 15 | +// |
| 16 | +// Use this macro to declare an enum and its values. |
| 17 | +// |
| 18 | +// - py11x::bind_enums(m: pybind11::module): |
| 19 | +// m: The module to use to register the enum classes. |
| 20 | +// |
| 21 | +// Place this in PYBIND11_MODULE to register the enums declared by P11X_DECLARE_ENUM. |
| 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 | +#define P11X_CAT2(a, b) a##b |
| 29 | +#define P11X_CAT(a, b) P11X_CAT2(a, b) |
| 30 | + |
| 31 | +namespace p11x { |
| 32 | + namespace { |
| 33 | + namespace py = pybind11; |
| 34 | + |
| 35 | + // Holder is (py_base_cls, [(name, value), ...]) before module init; |
| 36 | + // converted to the Python class object after init. |
| 37 | + auto enums = std::unordered_map<std::string, py::object>{}; |
| 38 | + |
| 39 | + auto bind_enums(py::module mod) -> void |
| 40 | + { |
| 41 | + for (auto& [py_name, spec]: enums) { |
| 42 | + auto const& [py_base_cls, pairs] = |
| 43 | + spec.cast<std::pair<std::string, py::object>>(); |
| 44 | + mod.attr(py::cast(py_name)) = spec = |
| 45 | + py::module::import("enum").attr(py_base_cls.c_str())( |
| 46 | + py_name, pairs, py::arg("module") = mod.attr("__name__")); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// Immediately converting the args to a vector outside of the lambda avoids |
| 53 | +// name collisions. |
| 54 | +#define P11X_DECLARE_ENUM(py_name, py_base_cls, ...) \ |
| 55 | + namespace p11x { \ |
| 56 | + namespace { \ |
| 57 | + [[maybe_unused]] auto const P11X_CAT(enum_placeholder_, __COUNTER__) = \ |
| 58 | + [](auto args) { \ |
| 59 | + py::gil_scoped_acquire gil; \ |
| 60 | + using int_t = std::underlying_type_t<decltype(args[0].second)>; \ |
| 61 | + auto pairs = std::vector<std::pair<std::string, int_t>>{}; \ |
| 62 | + for (auto& [k, v]: args) { \ |
| 63 | + pairs.emplace_back(k, int_t(v)); \ |
| 64 | + } \ |
| 65 | + p11x::enums[py_name] = pybind11::cast(std::pair{py_base_cls, pairs}); \ |
| 66 | + return 0; \ |
| 67 | + } (std::vector{std::pair __VA_ARGS__}); \ |
| 68 | + } \ |
| 69 | + } \ |
| 70 | + 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)); \ |
| 75 | + bool load(handle src, bool) { \ |
| 76 | + auto cls = p11x::enums.at(py_name); \ |
| 77 | + PyObject* tmp = nullptr; \ |
| 78 | + if (pybind11::isinstance(src, cls) \ |
| 79 | + && (tmp = PyNumber_Index(src.attr("value").ptr()))) { \ |
| 80 | + auto ival = PyLong_AsLong(tmp); \ |
| 81 | + value = decltype(value)(ival); \ |
| 82 | + Py_DECREF(tmp); \ |
| 83 | + return !(ival == -1 && !PyErr_Occurred()); \ |
| 84 | + } else { \ |
| 85 | + return false; \ |
| 86 | + } \ |
| 87 | + } \ |
| 88 | + static handle cast(decltype(value) obj, return_value_policy, handle) { \ |
| 89 | + auto cls = p11x::enums.at(py_name); \ |
| 90 | + return cls(std::underlying_type_t<type>(obj)).inc_ref(); \ |
| 91 | + } \ |
| 92 | + }; \ |
| 93 | + } |
| 94 | + |
| 95 | +#endif /* MPL_ENUMS_H */ |
0 commit comments