[Cython] Support for C++ scoped enums (PR #1603)
Ashwin Srinath
atrikut at gmail.com
Tue May 26 13:06:28 EDT 2020
Hello Cython Developers,
I just opened PR https://github.com/cython/cython/pull/3640, which
adds support for C++ scoped enumerations.
Feedback on the high-level approach used here is greatly welcome and
appreciated. In general, I used the advice in
https://groups.google.com/forum/#!topic/cython-users/OdvjbZ1_YKU as a
starting point.
One problem I haven't been to solve is coercing Python objects to and
from the enum class correctly. Currently the following utility
function does the conversion from C++->Python (CppConvert.pyx):
#################### enum.to_py ####################
cdef extern from "<type_traits>" namespace "std":
cdef cppclass underlying_type_t[T]:
pass
@cname("{{cname}}")
cdef object {{cname}}(const {{TYPE}}& x):
return <int><underlying_type_t[{{TYPE}}]>(x)
As you can see, the conversion goes
C++->underlying_type_t->int->Python, where ideally I'd like to
eliminate the conversion to `int` before obtaining a Python object.
Any ideas for how to solve this? If you prefer that this discussion
happen more broadly on GitHub, I'm happy to move it there.
Best,
Ashwin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cython-devel/attachments/20200526/f9850fae/attachment.html>
More information about the cython-devel
mailing list