1

I have the following enum defined in C++ API:

typedef enum RESULT_ENUM
{
    SUCCESS,
    ERR_INVALID_PORT_DEFINITION,
    ERR_TOO_MANY_SAMPLES,
    ERR_RECORDING_THREAD_ALREADY_RUNNING,
    ERR_RECORDING_WITHOUT_APPLY_SETTINGS,
    ...
}RESULT;

I have a program in C++ that uses the API and creating:

RESULT res;

Then it uses functions from the API to set values inside res, for example:

res = SetProfile(APP_PROFILE);
res = SetDynamicImageFilter(filterType);
res = StartCalibration();

I want to create a Python program that does the same (literally), using ctypes. How do I translate RESULT res; in a pythonic way? How do I make it contain the desired results from the functions?

EDIT:

Those functions return values that match the RESULT enumerators. I want to get those enumerators in Python, How can I do that? I'm currently getting numbers corresponding to the enumerators values.

galah92
  • 3,621
  • 2
  • 29
  • 55
  • Recreating an `enum` in Python is a bit clumsy as it doesn't fit well into the language's design. See [this post](http://stackoverflow.com/questions/1546355/using-enums-in-ctypes-structure) for examples. – Tim Jun 07 '16 at 08:32
  • In C++, `res` is changing internally by the API and is accessible by the C++ program. Is there a way to mimic that behavior in Python? Like creating a `ctypes` variable that will change according to the C++ API? – galah92 Jun 07 '16 at 08:39

1 Answers1

0

The name to value mapping is not compiled into the binary.

All ctypes code that needs the value of a enum hard codes that value in the python.

If you wrap the C++ code in a python extension you can choose to expose the enum values as python symbols of your module.

If you control the C++ implementation you are calling you could add a helper fucntion to return the value of the enum you need.

Barry Scott
  • 799
  • 6
  • 13
  • I don't control the C++ API. So I guess that what I'm looking for is exposing the enum values as python symbols. How can I do that? – galah92 Jun 07 '16 at 10:21