SWIG:从另一个模块返回对象的函数

SWIG: function returning an object from another module

本文关键字:对象 函数 返回 模块 另一个 SWIG      更新时间:2023-10-16

我想知道是否可以使用SWIG混合来自两个模块的对象,例如,模块a的函数是否可以返回模块B的对象?

我的用例如下:

class_a.hpp:

class ClassA
{
public:
    const OGRPolygon& get_geom() const;
    void set_geom(OGRPolygon* geom);
protected:
    OGRPolygon* _footprint;
};

class_a.cpp:

const OGRPolygon& ForCity::SPreC_cpp::ClassA::get_geom() const
{
    return *(this->_footprint);
}
void ForCity::SPreC_cpp::ClassA::set_geom(OGRPolygon* geom)
{
    this->_footprint = geom;
}

测试.i:

%module test
%include "class_A.hpp"

然后在Python中,我希望能够做一些类似的事情:

A = test.ClassA()
G = ogr.Geometry(ogr.wkbLinearRing)
# filling the geometry...
A.set_geom(G) # set the geometry of A
A.get_geom().GetArea() # use the geometry of A as a usual OGR geometry

有可能吗?请怎么做?

这可以通过在.i文件中包含%import ClassB指令来实现。此指令读取类型信息,但不生成绑定。