使用swig将一个opaque类型暴露给python

时间:2013-03-27 11:15:46

标签: python c swig opaque-pointers

我正在尝试使用SWIG在C中包装opaque类型,但我无法理解如何。我有三个文件列出如下:

simplelib.c:

#include <assert.h>
#include <stdlib.h>
#include "simplelib.h"

struct _simplelib_my_type {
    double x;
    double y;
};

simplelib_MyType *
simplelib_mytype_create(double x, double y)
{
    simplelib_MyType *mt = NULL;
    if (mt = calloc(1, sizeof(*mt))) {
        mt->x;
        mt->y;
    }
    return mt;
}

void
simplelib_mytype_destroy(simplelib_MyType *mt)
{
    if (mt) {
        free(mt);
        mt = NULL;
    }
}

int
simplelib_mytype_calc(const simplelib_MyType *mt, double z, double *res)
{
    int ok = 0;
    assert(mt);
    if (z != 0.0) {
        *res = mt->x * mt->y / z;
        ok = 1;
    }
    return ok;
}

simplelib.h:

#ifndef SIMPLELIB_H
#define SIMPLELIB_H

typedef struct _simplelib_my_type simplelib_MyType;

simplelib_MyType *simplelib_mytype_create(double x, double y);
void simplelib_mytype_destroy(simplelib_MyType *mt);

int simplelib_mytype_calc(const simplelib_MyType *mt, double z, double *res);

#endif // SIMPLELIB_H

和我的界面文件simplelibswig.i:

%module simplelibswig
%{
    extern "C" {
        #include "simplelib.h"
    }
%}

%include "simplelib.h"

我使用CMake使用CMakeLists.txt构建所有内容:

project(simplelib)
cmake_minimum_required(VERSION 2.8)

find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})

find_package(PythonLibs)
include_directories(${PYTHON_INCLUDE_PATH})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
SET(CMAKE_SWIG_FLAGS "")

SET_SOURCE_FILES_PROPERTIES(simplelibswig.i PROPERTIES CPLUSPLUS ON)
SET_SOURCE_FILES_PROPERTIES(simplelibswig.i PROPERTIES SWIG_FLAGS "-includeall")


add_library(${PROJECT_NAME}
    simplelib.h
    simplelib.c
)

swig_add_module(simplelibswig python simplelibswig.i)
swig_link_libraries(simplelibswig ${PYTHON_LIBRARIES} ${PROJECT_NAME})

现在,我想做的是 1)将simplelib_MyType中的opaque类型重命名为MyType 2)使用%extend

使用构造函数/析构函数/方法公开类型

问题是上面没有公开构建的python模块中的类型。我希望接口文件将typedef公开为具有typedefed名称的类但不会发生。因此,我不能继续前进到上面的第1点和第2点。我做错了什么?

祝你好运, 里卡德

1 个答案:

答案 0 :(得分:1)

Swig需要一个opaque类型的定义,以允许它被暴露和扩展。即使是不准确的人也会这样做:

%module simplelibswig
%{
    extern "C" {
        struct _simplelib_my_type { int _unused; };
        #include "simplelib.h"
    }
%}

struct _simplelib_my_type { int _unused; };
%include "simplelib.h"

对于SWIG也管理不透明对象的内存,应该标记创建和销毁它们的函数:

%newobject simplelib_mytype_create;
%delobject simplelib_mytype_destroy;
%include "simplelib.h"

现在,如果Python代码调用simplelib_mytype_create,则返回值将由Python而不是C代码拥有,并且在销毁所有引用时将被释放。但是如果调用simplelib_mytype_destroy函数,Python将知道该对象已经被释放,并且不会再次释放它。

您现在也可以%extend%rename类型:

%module simplelibswig
%{
    struct _simplelib_my_type { int _unused; };
    #include "simplelib.h"
%}

%rename(opaque) _simplelib_my_type;

struct _simplelib_my_type { int _unused; };
%newobject simplelib_mytype_create;
%delobject simplelib_mytype_destroy;
%include "simplelib.h"

%extend _simplelib_my_type {
    _simplelib_my_type(double a,double b) { return simplelib_mytype_create(a,b); }
    %apply double *OUTPUT { double* res };
    int calc(double z, double *res) { return simplelib_mytype_calc($self,z,res); }
    ~_simplelib_my_type() { simplelib_mytype_destroy($self); }
}

输出

>>> import simplelibswig
>>> simplelibswig.opaque(1.2,3.4)
<simplelibswig.opaque; proxy of <Swig Object of type 'simplelib_MyType *' at 0x0000000002872DE0> >
>>> a=simplelibswig.opaque(1.2,3.4)
>>> a.calc(3.4)
[1, 1.2]
>>> a.calc(1.2)
[1, 3.4000000000000004]