Skip to content

Commit d34d378

Browse files
committed
enum_<> is no longer derived from class_<>
1 parent 2b941b3 commit d34d378

File tree

1 file changed

+47
-37
lines changed

1 file changed

+47
-37
lines changed

include/pybind11/pybind11.h

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,72 +1169,76 @@ class class_ : public detail::generic_type {
11691169
};
11701170

11711171
/// Binds C++ enumerations and enumeration classes to Python
1172-
template <typename Type> class enum_ : public class_<Type> {
1172+
template <typename Type> class enum_ {
11731173
public:
1174-
using class_<Type>::def;
1175-
using class_<Type>::def_property_readonly_static;
11761174
using Scalar = typename std::underlying_type<Type>::type;
11771175

11781176
template <typename... Extra>
11791177
enum_(const handle &scope, const char *name, const Extra&... extra)
1180-
: class_<Type>(scope, name, extra...), m_entries(), m_parent(scope) {
1178+
: cls(scope, name, extra...), m_entries(), m_parent(scope) {
11811179

11821180
constexpr bool is_arithmetic = detail::any_of<std::is_same<arithmetic, Extra>...>::value;
11831181

11841182
auto m_entries_ptr = m_entries.inc_ref().ptr();
1185-
def("__repr__", [name, m_entries_ptr](Type value) -> pybind11::str {
1183+
1184+
cls
1185+
.def("__repr__", [name, m_entries_ptr](Type value) -> pybind11::str {
11861186
for (const auto &kv : reinterpret_borrow<dict>(m_entries_ptr)) {
11871187
if (pybind11::cast<Type>(kv.second) == value)
11881188
return pybind11::str("{}.{}").format(name, kv.first);
11891189
}
11901190
return pybind11::str("{}.???").format(name);
1191-
});
1192-
def_property_readonly_static("__members__", [m_entries_ptr](object /* self */) {
1191+
})
1192+
.def_property_readonly_static("__members__", [m_entries_ptr](object /* self */) {
11931193
dict m;
11941194
for (const auto &kv : reinterpret_borrow<dict>(m_entries_ptr))
11951195
m[kv.first] = kv.second;
11961196
return m;
1197-
}, return_value_policy::copy);
1198-
def("__init__", [](Type& value, Scalar i) { value = (Type)i; });
1199-
def("__int__", [](Type value) { return (Scalar) value; });
1197+
}, return_value_policy::copy)
1198+
.def("__init__", [](Type& value, Scalar i) { value = (Type)i; })
1199+
.def("__int__", [](Type value) { return (Scalar) value; })
12001200
#if PY_MAJOR_VERSION < 3
1201-
def("__long__", [](Type value) { return (Scalar) value; });
1201+
.def("__long__", [](Type value) { return (Scalar) value; })
12021202
#endif
1203-
def("__eq__", [](const Type &value, Type *value2) { return value2 && value == *value2; });
1204-
def("__ne__", [](const Type &value, Type *value2) { return !value2 || value != *value2; });
1203+
.def("__eq__", [](const Type &value, Type *value2) { return value2 && value == *value2; })
1204+
.def("__ne__", [](const Type &value, Type *value2) { return !value2 || value != *value2; });
12051205
if (is_arithmetic) {
1206-
def("__lt__", [](const Type &value, Type *value2) { return value2 && value < *value2; });
1207-
def("__gt__", [](const Type &value, Type *value2) { return value2 && value > *value2; });
1208-
def("__le__", [](const Type &value, Type *value2) { return value2 && value <= *value2; });
1209-
def("__ge__", [](const Type &value, Type *value2) { return value2 && value >= *value2; });
1206+
cls
1207+
.def("__lt__", [](const Type &value, Type *value2) { return value2 && value < *value2; })
1208+
.def("__gt__", [](const Type &value, Type *value2) { return value2 && value > *value2; })
1209+
.def("__le__", [](const Type &value, Type *value2) { return value2 && value <= *value2; })
1210+
.def("__ge__", [](const Type &value, Type *value2) { return value2 && value >= *value2; });
12101211
}
12111212
if (std::is_convertible<Type, Scalar>::value) {
12121213
// Don't provide comparison with the underlying type if the enum isn't convertible,
12131214
// i.e. if Type is a scoped enum, mirroring the C++ behaviour. (NB: we explicitly
12141215
// convert Type to Scalar below anyway because this needs to compile).
1215-
def("__eq__", [](const Type &value, Scalar value2) { return (Scalar) value == value2; });
1216-
def("__ne__", [](const Type &value, Scalar value2) { return (Scalar) value != value2; });
1216+
cls
1217+
.def("__eq__", [](const Type &value, Scalar value2) { return (Scalar) value == value2; })
1218+
.def("__ne__", [](const Type &value, Scalar value2) { return (Scalar) value != value2; });
12171219
if (is_arithmetic) {
1218-
def("__lt__", [](const Type &value, Scalar value2) { return (Scalar) value < value2; });
1219-
def("__gt__", [](const Type &value, Scalar value2) { return (Scalar) value > value2; });
1220-
def("__le__", [](const Type &value, Scalar value2) { return (Scalar) value <= value2; });
1221-
def("__ge__", [](const Type &value, Scalar value2) { return (Scalar) value >= value2; });
1222-
def("__invert__", [](const Type &value) { return ~((Scalar) value); });
1223-
def("__and__", [](const Type &value, Scalar value2) { return (Scalar) value & value2; });
1224-
def("__or__", [](const Type &value, Scalar value2) { return (Scalar) value | value2; });
1225-
def("__xor__", [](const Type &value, Scalar value2) { return (Scalar) value ^ value2; });
1226-
def("__rand__", [](const Type &value, Scalar value2) { return (Scalar) value & value2; });
1227-
def("__ror__", [](const Type &value, Scalar value2) { return (Scalar) value | value2; });
1228-
def("__rxor__", [](const Type &value, Scalar value2) { return (Scalar) value ^ value2; });
1229-
def("__and__", [](const Type &value, const Type &value2) { return (Scalar) value & (Scalar) value2; });
1230-
def("__or__", [](const Type &value, const Type &value2) { return (Scalar) value | (Scalar) value2; });
1231-
def("__xor__", [](const Type &value, const Type &value2) { return (Scalar) value ^ (Scalar) value2; });
1220+
cls
1221+
.def("__lt__", [](const Type &value, Scalar value2) { return (Scalar) value < value2; })
1222+
.def("__gt__", [](const Type &value, Scalar value2) { return (Scalar) value > value2; })
1223+
.def("__le__", [](const Type &value, Scalar value2) { return (Scalar) value <= value2; })
1224+
.def("__ge__", [](const Type &value, Scalar value2) { return (Scalar) value >= value2; })
1225+
.def("__invert__", [](const Type &value) { return ~((Scalar) value); })
1226+
.def("__and__", [](const Type &value, Scalar value2) { return (Scalar) value & value2; })
1227+
.def("__or__", [](const Type &value, Scalar value2) { return (Scalar) value | value2; })
1228+
.def("__xor__", [](const Type &value, Scalar value2) { return (Scalar) value ^ value2; })
1229+
.def("__rand__", [](const Type &value, Scalar value2) { return (Scalar) value & value2; })
1230+
.def("__ror__", [](const Type &value, Scalar value2) { return (Scalar) value | value2; })
1231+
.def("__rxor__", [](const Type &value, Scalar value2) { return (Scalar) value ^ value2; })
1232+
.def("__and__", [](const Type &value, const Type &value2) { return (Scalar) value & (Scalar) value2; })
1233+
.def("__or__", [](const Type &value, const Type &value2) { return (Scalar) value | (Scalar) value2; })
1234+
.def("__xor__", [](const Type &value, const Type &value2) { return (Scalar) value ^ (Scalar) value2; });
12321235
}
12331236
}
1234-
def("__hash__", [](const Type &value) { return (Scalar) value; });
1237+
cls
1238+
.def("__hash__", [](const Type &value) { return (Scalar) value; })
12351239
// Pickling and unpickling -- needed for use with the 'multiprocessing' module
1236-
def("__getstate__", [](const Type &value) { return pybind11::make_tuple((Scalar) value); });
1237-
def("__setstate__", [](Type &p, tuple t) { new (&p) Type((Type) t[0].cast<Scalar>()); });
1240+
.def("__getstate__", [](const Type &value) { return pybind11::make_tuple((Scalar) value); })
1241+
.def("__setstate__", [](Type &p, tuple t) { new (&p) Type((Type) t[0].cast<Scalar>()); });
12381242
}
12391243

12401244
/// Export enumeration entries into the parent scope
@@ -1247,12 +1251,18 @@ template <typename Type> class enum_ : public class_<Type> {
12471251
/// Add an enumeration entry
12481252
enum_& value(char const* name, Type value) {
12491253
auto v = pybind11::cast(value, return_value_policy::copy);
1250-
this->attr(name) = v;
1254+
cls.attr(name) = v;
12511255
m_entries[pybind11::str(name)] = v;
12521256
return *this;
12531257
}
12541258

1259+
/// Get the associated class object.
1260+
class_<Type> into_class() && {
1261+
return cls;
1262+
}
1263+
12551264
private:
1265+
class_<Type> cls;
12561266
dict m_entries;
12571267
handle m_parent;
12581268
};

0 commit comments

Comments
 (0)