简体   繁体   English

如何使用swig将python类实例作为c ++函数的参数传递?

[英]how to pass python class instance as a parameter of c++ fuction using swig?

i'm trying translate c++ class interface to other languages using swig. 我正在尝试使用swig将c ++类接口转换为其他语言。 my c++ interface is like this: 我的c ++接口是这样的:

this is file mitprotocol.h 这是文件mitprotocol.h

class MitProtocolCallBack
{
public:
    virtual const string & TestFunc(deque<int> & param) = 0;
};

class MitProtocolInterface
{
public:
    virtual void ReleaseMe() = 0;
    virtual void SetCallBack(MitProtocolCallBack * mitProtocolCallBack) = 0;
    virtual void UnTar(const string & filePathAndName) = 0;
};

MitProtocolInterface * CreateMitProtocolInterface();

in c++ , i can use this interface like this: 在c ++中,我可以像这样使用此接口:

this is file test.cpp: 这是文件test.cpp:

class testclass : public MitProtocolCallBack
{

public:
    void playhaha()
    {
        mitProtocolInterface->UnTar("");
    }
private:
    string str_res;

public:
    virtual const string & TestFunc(deque<int> & param)
    {
        str_res = "abc";
        return str_res;
    }
private:
    MitProtocolInterface * mitProtocolInterface;

public:
    testclass()
    {
        mitProtocolInterface = CreateMitProtocolInterface();
        mitProtocolInterface->SetCallBack(this);
    }
    ~testclass()
    {
        mitProtocolInterface->ReleaseMe();
    }
};

void main()
{
    testclass haha;
    haha.playhaha();
}

then i tried wrap the c++ interface using swig: 然后我尝试使用swig包装c ++接口:

this is file mitprotocol.i: 这是文件mitprotocol.i:

%module mitprotocol

%include "std_string.i"

%include "std_deque.i"

%{
#include "mitprotocol.h"
%}

namespace std {
   %template(IntDeque) deque<int>;
}

%include "mitprotocol.h"

then i executed: 然后我执行了:

swig -c++ -python mitprotocol.i

then i got 2 files: 然后我有2个文件:

file mitprotocol.py: for python interface
file mitprotocol_wrap.cxx: to compile with other c++ source codes as a lib

then i tried to use the python interface: 然后我尝试使用python接口:

this is file test.py: 这是文件test.py:

import mitprotocol

class myclass(mitprotocol.MitProtocolCallBack):
    def __init__(self):
        self.mitProtocolInterface = mitprotocol.CreateMitProtocolInterface()
        self.mitProtocolInterface.SetCallBack(self)

    def __delete__(self):
        self.mitProtocolInterface.ReleaseMe()

    def TestFunc(self, param):
        print param
        return "aedfas"

    def playhaha(self):
        self.mitProtocolInterface.UnTar("")

ffsa = myclass()
ffsa.playhaha()

and finally i got an erro in line: 最后我遇到了错误:

self.mitProtocolInterface.SetCallBack(self)

the error is: 错误是:

TypeError: in method 'MitProtocolInterface_SetCallBack', 
argument 2 of type 'MitProtocolCallBack *'

i think it's crashed when trying pass python class instance to swig wrapped c++ interface. 我认为当尝试将python类实例传递给Swig包装的C ++接口时,它崩溃了。 anyone with any help ? 有任何帮助的人吗?

there are 2 crucial aspects: 有两个关键方面:

1.add polymorphism to .i file: 1.将多态性添加到.i文件中:

%module(directors="1") mitprotocol

%include "std_string.i";

%include "std_deque.i";

%{
#include "mitprotocol.h"
%}

namespace std {
   %template(IntDeque) deque<int>;
}

%feature("director") MitProtocolCallBack;

%include "../../mitprotocol/src/traffic/mitprotocol.h";

2.python app need call super class's init when init: 2. python应用在初始化时需要调用超类的init:

import mitprotocol

class myclass(mitprotocol.MitProtocolCallBack):
    def __init__(self):
        mitprotocol.MitProtocolCallBack.__init__(self)
        self.mitProtocolInterface = mitprotocol.CreateMitProtocolInterface()
        self.mitProtocolInterface.SetCallBack(self)

    def __delete__(self):
        self.mitProtocolInterface.ReleaseMe()

    def TestFunc(self, param):
        print param
        return "aedfas"

    def playhaha(self):
        self.mitProtocolInterface.UnTar("")

ffsa = myclass()
ffsa.playhaha()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM