/* Copyright 2017 - 2026 R. Thomas * Copyright 2017 - 2026 Quarkslab * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PY_LIEF_ENUMS_WRAPPER_H #define PY_LIEF_ENUMS_WRAPPER_H #include #include #include "LIEF/visibility.h" namespace nb = nanobind; namespace LIEF { template class LIEF_LOCAL enum_ : public nanobind::enum_ { public: using Base = typename nanobind::enum_::Base; using Underlying = typename nanobind::enum_::Underlying; using nanobind::enum_::def_prop_ro; using nanobind::enum_::def_static; using nanobind::enum_::def; template NB_INLINE enum_(nanobind::handle scope, const char *name, const Extra &... extra) : nanobind::enum_{scope, name, extra...} { def_static("from_value", [] (Underlying i) -> Type { nb::object out = nb::cast(static_cast(i)); if (nb::isinstance(out)) { throw nb::value_error("Conversion error"); } return static_cast(i); }); def("__eq__", [](const Type &value, Underlying value2) { return (Underlying) value == value2; }); def("__eq__", [](const Type &value, Type value2) { return (Underlying) value == (Underlying) value2; }); def("__ne__", [](const Type &value, Underlying value2) { return (Underlying) value != value2; }); def("__ne__", [](const Type &value, Type value2) { return (Underlying) value != (Underlying) value2; }); def("__int__", [](const Type &value) { return (Underlying) value; }); } }; } #endif