Skip to content

Commit

Permalink
move enumeration flags into a separate enum
Browse files Browse the repository at this point in the history
  • Loading branch information
wjakob committed Aug 27, 2024
1 parent 7225e07 commit 8a65e0e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
22 changes: 12 additions & 10 deletions include/nanobind/nb_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,10 @@ enum class type_flags : uint32_t {
/// The class implements __class_getitem__ similar to typing.Generic
is_generic = (1 << 15),

/// Is this an arithmetic enumeration?
is_arithmetic = (1 << 16),

/// Is the number type underlying the enumeration signed?
is_signed = (1 << 17),

/// Does the type implement a custom __new__ operator?
has_new = (1 << 18)
has_new = (1 << 16)

// No more bits bits available without needing a larger reorganization
// Two more bits bits available without needing a larger reorganization
};

/// Flags about a type that are only relevant when it is being created.
Expand Down Expand Up @@ -192,6 +186,14 @@ NB_INLINE void type_extra_apply(type_init_data &t, supplement<T>) {
t.supplement = sizeof(T);
}

enum class enum_flags : uint32_t {
/// Is this an arithmetic enumeration?
is_arithmetic = (1 << 1),

/// Is the number type underlying the enumeration signed?
is_signed = (1 << 2)
};

struct enum_init_data {
const std::type_info *type;
PyObject *scope;
Expand All @@ -201,7 +203,7 @@ struct enum_init_data {
};

NB_INLINE void enum_extra_apply(enum_init_data &e, is_arithmetic) {
e.flags |= (uint32_t) type_flags::is_arithmetic;
e.flags |= (uint32_t) enum_flags::is_arithmetic;
}

NB_INLINE void enum_extra_apply(enum_init_data &e, const char *doc) {
Expand Down Expand Up @@ -719,7 +721,7 @@ template <typename T> class enum_ : public object {
ed.scope = scope.ptr();
ed.name = name;
ed.flags = std::is_signed_v<Underlying>
? (uint32_t) detail::type_flags::is_signed
? (uint32_t) detail::enum_flags::is_signed
: 0;
(detail::enum_extra_apply(ed, extra), ...);
m_ptr = detail::enum_create(&ed);
Expand Down
8 changes: 4 additions & 4 deletions src/nb_enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ PyObject *enum_create(enum_init_data *ed) noexcept {

handle scope(ed->scope);

bool is_arithmetic = ed->flags & (uint32_t) type_flags::is_arithmetic;
bool is_arithmetic = ed->flags & (uint32_t) enum_flags::is_arithmetic;

str name(ed->name), qualname = name;
object modname;
Expand Down Expand Up @@ -99,7 +99,7 @@ void enum_append(PyObject *tp_, const char *name_, int64_t value_,
type_data *t = enum_get_type_data(tp);

object val;
if (t->flags & (uint32_t) type_flags::is_signed)
if (t->flags & (uint32_t) enum_flags::is_signed)
val = steal(PyLong_FromLongLong((long long) value_));
else
val = steal(PyLong_FromUnsignedLongLong((unsigned long long) value_));
Expand Down Expand Up @@ -161,7 +161,7 @@ bool enum_from_python(const std::type_info *tp, PyObject *o, int64_t *out, uint8
if (flags & (uint8_t) cast_flags::convert) {
enum_map *fwd = (enum_map *) t->enum_tbl.fwd;

if (t->flags & (uint32_t) type_flags::is_signed) {
if (t->flags & (uint32_t) enum_flags::is_signed) {
long long value = PyLong_AsLongLong(o);
if (value == -1 && PyErr_Occurred()) {
PyErr_Clear();
Expand Down Expand Up @@ -204,7 +204,7 @@ PyObject *enum_from_cpp(const std::type_info *tp, int64_t key) noexcept {
return value;
}

if (t->flags & (uint32_t) type_flags::is_signed)
if (t->flags & (uint32_t) enum_flags::is_signed)
PyErr_Format(PyExc_ValueError, "%lli is not a valid %s.",
(long long) key, t->name);
else
Expand Down

0 comments on commit 8a65e0e

Please sign in to comment.