Functions overloaded with enums

I’m writing python code to interface with pre-existing C++ code. Unfortunately, the C++ uses a number of enums, and functions are overloaded with these as inputs.

Unfortunately, since python just treats the enum values as integers, I get the same version of the overloaded function every time.

For example, with enums:
typedef enum e1 { e1var1, e1var2 } typeOne_t;
typedef enum e2 { e2var1, e2var2 } typeTwo_t;

and functions:
void MyFunc(typeOne_t in);
void MyFunc(typeTwo_t in);

A call to MyFunc from python will only ever access one of these functions, and I appear to be unable to determine which.

Is there a simple workaround to this? I suppose I could build an extra C++ library with differently named functions that convert to the appropriate enum type and make the calls, but this seems incredibly clunky.

Hi,

there’s a (rather clumsy) workaround possible using the ‘disp’ method that each function and method carries. The disp() call takes, as a string, the full signature of the method to select it (see the doc string of the method). You can use that to rebind the methods.

Example, C++ code:[code]#include

typedef enum e1 { e1var1, e1var2 } typeOne_t;
typedef enum e2 { e2var1, e2var2 } typeTwo_t;

void func(typeOne_t e) { std::cout << "received typeOne_t: " << (int)e << std::endl; }
void func(typeTwo_t e) { std::cout << "received typeTwo_t: " << (int)e << std::endl; }[/code]
and python usage:[code]% python

from ROOT import *
gROOT.LoadMacro(“func.cxx+”)
func(1)
received typeOne_t: 1
print func.doc
void ::func(typeOne_t e)
void ::func(typeTwo_t e)
func.disp(‘typeTwo_t e’)(1)
received typeTwo_t: 1
[/code]
All this to be improved in C++11, where enums are normal types …

HTH,
Wim

Hi,

That does work. I think the decision about whether that’s more or less clumsy than having an extra wrapper C++ library will be dependent on context. I suppose a better solution would be having a python wrapper module using your workaround.

Thanks a lot.