0

I'm trying to wrap an opaque type in C using SWIG but I cant understand how to. I have three files listed below:

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

and my interface file simplelibswig.i:

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

%include "simplelib.h"

I build everything using CMake, using this 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})

Now, what I would like to do is to 1) rename the opaque type from simplelib_MyType to MyType 2) expose the type with constructor/destructor/method using %extend

The problem is that the above does not expose the type in the built python module. I would expect the interface file to expose the typedef as a class with the typedefed name but that is not happening. Thus I can't move on to point 1 and 2 above. What am I doing wrong?

Best regards, Rickard

Rickard
  • 1,754
  • 1
  • 12
  • 17

1 Answers1

1

Swig needs a definition for your opaque type to allow it to be exposed and extended. Even an inaccurate one will do:

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

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

For SWIG to also manage the memory of the opaque object, the functions that create and destroy them should be marked so:

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

Now if Python code calls simplelib_mytype_create the return value will be owned by Python instead of the C code, and will be freed when it all references are destroyed. But if the simplelib_mytype_destroy function is called, Python will know that the object is already freed and won't free it again.

You can %extend and %rename the type now as well:

%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); }
}

Output

>>> 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]
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251