|
| 1 | +// #include <pybind11/native_enum.h> |
| 2 | + |
| 3 | +#include "pybind11_tests.h" |
| 4 | + |
| 5 | +namespace test_native_enum { |
| 6 | + |
| 7 | +// https://en.cppreference.com/w/cpp/language/enum |
| 8 | + |
| 9 | +// enum that takes 16 bits |
| 10 | +enum smallenum : std::int16_t { a, b, c }; |
| 11 | + |
| 12 | +// color may be red (value 0), yellow (value 1), green (value 20), or blue (value 21) |
| 13 | +enum color { red, yellow, green = 20, blue }; |
| 14 | + |
| 15 | +// altitude may be altitude::high or altitude::low |
| 16 | +enum class altitude : char { |
| 17 | + high = 'h', |
| 18 | + low = 'l', // trailing comma only allowed after CWG518 |
| 19 | +}; |
| 20 | + |
| 21 | +// the constant d is 0, the constant e is 1, the constant f is 3 |
| 22 | +enum { d, e, f = e + 2 }; |
| 23 | + |
| 24 | +int pass_color(color e) { return static_cast<int>(e); } |
| 25 | +color return_color(int i) { return static_cast<color>(i); } |
| 26 | + |
| 27 | +py::handle wrap_color(py::module_ m) { |
| 28 | + auto enum_module = py::module_::import("enum"); |
| 29 | + auto int_enum = enum_module.attr("IntEnum"); |
| 30 | + using u_t = std::underlying_type<color>::type; |
| 31 | + auto members = py::make_tuple(py::make_tuple("red", static_cast<u_t>(color::red)), |
| 32 | + py::make_tuple("yellow", static_cast<u_t>(color::yellow)), |
| 33 | + py::make_tuple("green", static_cast<u_t>(color::green)), |
| 34 | + py::make_tuple("blue", static_cast<u_t>(color::blue))); |
| 35 | + auto int_enum_color = int_enum("color", members); |
| 36 | + int_enum_color.attr("__module__") = m; |
| 37 | + m.attr("color") = int_enum_color; |
| 38 | + return int_enum_color.release(); // Intentionally leak Python reference. |
| 39 | +} |
| 40 | + |
| 41 | +} // namespace test_native_enum |
| 42 | + |
| 43 | +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) |
| 44 | +PYBIND11_NAMESPACE_BEGIN(detail) |
| 45 | + |
| 46 | +using namespace test_native_enum; |
| 47 | + |
| 48 | +template <> |
| 49 | +struct type_caster<color> { |
| 50 | + static handle native_type; |
| 51 | + |
| 52 | + static handle cast(const color &src, return_value_policy /* policy */, handle /* parent */) { |
| 53 | + auto u_v = static_cast<std::underlying_type<color>::type>(src); |
| 54 | + return native_type(u_v).release(); |
| 55 | + } |
| 56 | + |
| 57 | + bool load(handle src, bool /* convert */) { |
| 58 | + if (!isinstance(src, native_type)) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + value = static_cast<color>(py::cast<std::underlying_type<color>::type>(src.attr("value"))); |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + PYBIND11_TYPE_CASTER(color, const_name("<enum 'color'>")); |
| 66 | +}; |
| 67 | + |
| 68 | +handle type_caster<color>::native_type = nullptr; |
| 69 | + |
| 70 | +PYBIND11_NAMESPACE_END(detail) |
| 71 | +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) |
| 72 | + |
| 73 | +TEST_SUBMODULE(native_enum, m) { |
| 74 | + using namespace test_native_enum; |
| 75 | + |
| 76 | + py::detail::type_caster<color>::native_type = wrap_color(m); |
| 77 | + |
| 78 | + m.def("pass_color", pass_color); |
| 79 | + m.def("return_color", return_color); |
| 80 | +} |
0 commit comments