Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pybind11/pybind11.h>
- namespace py = pybind11;
- enum class Kind { Kind1 = 1, Kind2, Kind3 };
- py::object get_pybind11_metaclass()
- {
- auto &internals = py::detail::get_internals();
- return py::reinterpret_borrow<py::object>((PyObject*)internals.default_metaclass);
- }
- py::object get_standard_metaclass()
- {
- auto &internals = py::detail::get_internals();
- return py::reinterpret_borrow<py::object>((PyObject *)&PyType_Type);
- }
- py::object create_enum_metaclass()
- {
- auto pybind11_metaclass = get_pybind11_metaclass();
- auto standard_metaclass = get_standard_metaclass();
- py::dict attributes;
- attributes["__len__"] = py::cpp_function(
- [](py::object cls) {
- return cls.attr("_member_count")();
- }
- , py::is_method(py::none())
- );
- return standard_metaclass(std::string("pybind11_ext_enum")
- , py::make_tuple(pybind11_metaclass)
- , attributes);
- }
- PYBIND11_MODULE(so07, m)
- {
- auto enum_metaclass = create_enum_metaclass();
- py::enum_<Kind>(m, "Kind", py::metaclass(enum_metaclass))
- .value("Kind1", Kind::Kind1)
- .value("Kind2", Kind::Kind2)
- .value("Kind3", Kind::Kind3)
- .def_static("_member_count", []() {
- return 3;
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement