2

i'm trying translate c++ class interface to other languages using swig. my c++ interface is like this:

this is file 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:

this is file 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:

this is file 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:

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:

this is file 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. anyone with any help ?

1
  • i figured it myself, pls check the answer to these question Commented Oct 12, 2017 at 2:54

1 Answer 1

2

there are 2 crucial aspects:

1.add polymorphism to .i file:

%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:

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.