How to apply SWIG OUTPUT typemaps for class types in Python?

后端 未结 3 828
青春惊慌失措
青春惊慌失措 2020-12-17 19:52

I am having some trouble generating a Python wrapper around a C++ library using SWIG (version 3.0.6).

My issue relates to applying the OUTPUT typemap, specifically i

3条回答
  •  心在旅途
    2020-12-17 20:13

    It is not an answer, just not enough of reputaiton for a comment :(

    Cause you need to use pointer in C++ and Python doesn't have pointers (so you could not do anything with your current 'result' in Python anyway).

    Could you add wrappers to hide pointers into .h as was suggested by @Jens Munk:

    class exportedClassType_ptr {
    public:
        exportedClassType* ptr;
        exportedClassType_ptr( exportedClassType& input ) {
            this->ptr = &input;
        }
    };
    
    int GetClassType( const char* name, exportedClassType_ptr& resultPointer ) {
        return GetClassType( name, resultPointer.ptr );
    }
    

    modify .i file to call new method:

    %apply exportedClassType_ptr& OUTPUT { exportedClassType_ptr& resultPointer };    
    int GetClassType( const char* name, exportedClassType_ptr& resultPointer );
    

    in Python write something like this:

    >>> realResult = projectWrapper.exportedClassType()
    >>> result = projectWrapper.exportedClassType_ptr(realResult)
    >>> projectWrapper.GetClassType("name", result)
    

    and use 'realResult' for future work.

提交回复
热议问题