Skip to content

Commit

Permalink
Shorter syntax from @YannickJadoul
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Oct 2, 2017
1 parent 00087fd commit 82312ca
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1378,13 +1378,12 @@ template <typename Type> class enum_ : public class_<Type> {
}, return_value_policy::copy);
def(init([](Scalar i) { return static_cast<Type>(i); }));
def(init([name, m_entries_ptr](std::string value) -> Type {
for (const auto &kv : reinterpret_borrow<dict>(m_entries_ptr)) {
std::string key = pybind11::cast<pybind11::str>(kv.first);
if (value == key) {
return pybind11::cast<Type>(kv.second);
}
}
throw value_error("\"" + value + "\" is not a valid value for enum type " + name);
pybind11::dict values = reinterpret_borrow<pybind11::dict>(m_entries_ptr);
pybind11::str key = pybind11::str(value);
if(values.contains(key))
return pybind11::cast<Type>(values[key]);
else
throw value_error("\"" + value + "\" is not a valid value for enum type " + name);
}));
def("__int__", [](Type value) { return (Scalar) value; });
#if PY_MAJOR_VERSION < 3
Expand Down

0 comments on commit 82312ca

Please sign in to comment.