简体   繁体   English

如何访问SWIG Python生成的抽象c ++类方法?

[英]How to access SWIG Python generated Abstract c++ Class Methods?

This could be something simple I'm missing but I can't find any explanation. 这可能是我想念的简单但我无法找到任何解释。

Given an abstract class, which is implemented elsewhere and its interface provided by an exported function: 给定一个抽象类,它在别处实现,其接口由导出函数提供:

class IFoo {
public:
    virtual ~IFoo(){}
    virtual void bar()=0;
};

extern IFoo* get_interface();

In c++ I would use this as: 在c ++中我会用它作为:

IFoo* foo = get_interface();
foo->bar();

If I SWIG this, I can import the module and assign get_interface() to a variable: 如果我SWIG这个,我可以导入模块并将get_interface()分配给变量:

import myfoo
foo = myfoo.get_interface()

But I can't access foo.bar(): 但我无法访问foo.bar():

>>> foo.bar()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'SwigPyObject' object has no attribute 'bar'

What am I missing? 我错过了什么?

Thanks! 谢谢!


Here is the full thing if the fragments above aren't clear enough: 如果上面的碎片不够清楚,这就是完整的事情:

The *.i file: * .i文件:

%module myfoo
#define SWIG_FILE_WITH_INIT

%include "myfoo.h"
%{
#include "myfoo.h"
%}

The myfoo.h header: myfoo.h标题:

class IFoo {
public:
    virtual ~IFoo(){}
    virtual void bar()=0;
    virtual void release()=0;
};

extern IFoo* get_interface();

The implementation file (myfoo.cpp) 实现文件(myfoo.cpp)

#include "myfoo.h"
#include <iostream>

class Foo : public IFoo {
public:
    Foo(){}
    ~Foo(){}
    void bar();
    void release();
};

void Foo::bar() {
    cout << "Foo::bar()..." << endl;
}

void Foo::release() {
    delete this:
}

IFoo* get_interface() {
    return new Foo();
}

I found the problem. 我发现了这个问题。 I had this within a build system generating a Ruby extension. 我在生成Ruby扩展的构建系统中有这个。 When I decided to add Python bindings, I just added a new (Xcode) target generating it just as I was doing for the Ruby extension. 当我决定添加Python绑定时,我刚刚添加了一个新的(Xcode)目标,就像我为Ruby扩展做的那样。 What I didn't realize was that swig also generates a module .py file with all the initialization code (unlike Ruby). 我没有意识到swig还生成了一个包含所有初始化代码的模块 .py文件(与Ruby不同)。 Once I found that, I simply moved the generated python file along with the extension dynamic library file and imported it instead. 一旦我发现了,我只是将生成的python文件与扩展动态库文件一起移动并导入它。 It all works now. 这一切现在都有效。

I found that by actually creating the above myfoo code by hand (as opposed to running my build system, which ignored the extra myfoo.py file alltogether). 我发现通过手动创建上面的myfoo代码(而不是运行我的构建系统,它忽略了额外的myfoo.py文件)。 Once I run swig by hand, I noticed it generated not only myfoo_wrapp.cpp but also myfoo.py. 一旦我手动运行swig,我注意到它不仅产生了myfoo_wrapp.cpp,还产生了myfoo.py。

Thanks for the effort. 谢谢你的努力。 For future reference, this has nothing to do with directors (I'm not trying to create a two-way interface). 为了将来参考,这与导演无关(我不是想创建一个双向接口)。 It was simply not knowing about the extra python initialization code generated by swig. 它根本不知道swig生成的额外python初始化代码。

Before: 之前:

myfoo.i
myfoo.h
myfoo.cpp

>swig -c++ -python myfoo.i

After: 后:

myfoo.i
myfoo.h
myfoo.cpp
myfoo.py    <== Missed this one
myfoo_wrap.cxx

Check that you have "directors" turned on in your .i file for your module: 检查您的模块的.i文件中是否已打开“控制器”:

%module(directors="1") myfoo

This is discussed in section 34.5 "Cross language polymorphism" of SWIG docs . 这将在SWIG文档的34.5“交叉语言多态性”中讨论。 If it is already on, put a comment and I will take a closer look. 如果它已经开启,请发表评论,我会仔细看看。

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

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