2

I am trying to get "output pointers" (pointers passed into a method which are used for outputting data instead of using the return value) working with python bindings of C++ code (bindings created using SWIG), in where the output pointers are part of a method that is part of a class where cross-language polymorphism is occurring (using the SWIG directors feature).

Consider the following example:

// C++ code!
enum class AnEnum {
    FIRST_VAL,
    SECOND_VAL
}

class BaseClassCpp {
    virtual void Test(AnEnum * anEnum) = 0;
}

And in python I create a class which inherits from the base class in C++:

# Python code!
class ChildClassPy(SwigWrapper.BaseClassCpp):
    def Test(anEnum):
        anEnum = SwigWrapper.AnEnum_FIRST_VAL

Now, back in the C++ code, the python ChildClassPy object gets called using polymorphism:

// C++ code!
BaseClassCpp * polyObject = (code that gets python ChildClassPy object back into C++)

AnEnum anEnum;
polyObject->Test(&anEnum); // Cross-language polymorphism! Requires SWIG "directors" feature
// ENUM IS NOT SET CORRECTLY TO FIRST_VAL

How can I setup SWIG so that the anEnum variable in C++ is set correctly from the child class in Python?

I think part of the solution is to use the %typemap feature like so:

%typemap(directorargout) AnEnum * {
    // What goes here?
}

I might have to use a directorin typemap also.

Unfortunately, the documentation on director style typemaps is slim (see 36.5.6 Typemaps at http://www.swig.org/Doc3.0/Python.html#Python_nn46)

gbmhunter
  • 1,747
  • 3
  • 23
  • 24
  • In this post, the director feature is used for using a python implementation inside C++, https://stackoverflow.com/questions/9040669/how-can-i-implement-a-c-class-in-python-to-be-called-by-c/9042139#9042139 – Jens Munk Sep 10 '17 at 12:03

0 Answers0