Skip to content

Commit e5010de

Browse files
simplify
1 parent 2cbed02 commit e5010de

File tree

1 file changed

+2
-23
lines changed

1 file changed

+2
-23
lines changed

include/pybind11/pybind11.h

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,23 +1570,6 @@ inline str enum_name(handle arg) {
15701570
return "???";
15711571
}
15721572

1573-
template <typename Type, typename Scalar, bool is_convertible = true>
1574-
struct enum_value {
1575-
static Scalar run(handle arg) {
1576-
Type value = pybind11::cast<Type>(arg);
1577-
return static_cast<Scalar>(value);
1578-
}
1579-
};
1580-
1581-
template <typename Type, typename Scalar>
1582-
struct enum_value<Type, Scalar, false> {
1583-
static Scalar run(handle) {
1584-
throw pybind11::cast_error(
1585-
"Enum for " + type_id<Type>() + " is not convertible to " +
1586-
type_id<Scalar>());
1587-
}
1588-
};
1589-
15901573
struct enum_base {
15911574
enum_base(handle base, handle parent) : m_base(base), m_parent(parent) { }
15921575

@@ -1737,6 +1720,7 @@ template <typename Type> class enum_ : public class_<Type> {
17371720
using Base = class_<Type>;
17381721
using Base::def;
17391722
using Base::attr;
1723+
using Base::def_property;
17401724
using Base::def_property_readonly;
17411725
using Base::def_property_readonly_static;
17421726
using Scalar = typename std::underlying_type<Type>::type;
@@ -1746,15 +1730,10 @@ template <typename Type> class enum_ : public class_<Type> {
17461730
: class_<Type>(scope, name, extra...), m_base(*this, scope) {
17471731
constexpr bool is_arithmetic = detail::any_of<std::is_same<arithmetic, Extra>...>::value;
17481732
constexpr bool is_convertible = std::is_convertible<Type, Scalar>::value;
1749-
auto property = handle((PyObject *) &PyProperty_Type);
17501733
m_base.init(is_arithmetic, is_convertible);
17511734

1752-
attr("value") = property(
1753-
cpp_function(
1754-
&detail::enum_value<Type, Scalar, is_convertible>::run,
1755-
pybind11::name("value"), is_method(*this)));
1756-
17571735
def(init([](Scalar i) { return static_cast<Type>(i); }), arg("value"));
1736+
def_property("value", [](Type value) { return (Scalar) value; }, nullptr);
17581737
def("__int__", [](Type value) { return (Scalar) value; });
17591738
#if PY_MAJOR_VERSION < 3
17601739
def("__long__", [](Type value) { return (Scalar) value; });

0 commit comments

Comments
 (0)