Can I expose a C++ enum to SWIG as a real entity rather than a set of constants so I can enumerate over them in python code?
Can I expose a C++ enum to SWIG as a real entity rather than
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I faced the same issue. I hope that SWIG soon supports C++11’s
enum class.Here’s a hack that convinces SWIG to put enums in a structure:
In
.cppcode you now must useMyEnum::Value1and in Python code it isMyEnum.Value1. Although convoluted, thetypedefprevents having to change existing code that uses the enum everywhere and the SWIG %rename makes the enum have the same name in the SWIG wrapper.In Python you can enumerate the values with a little code:
It’s not pretty, and I’d love to see a better solution.