如何为类类型创建OUTPUT类型图?

时间:2015-10-29 12:03:09

标签: python c++ swig

我以前在尝试将SWIG的OUTPUT类型地图应用于班级类型时遇到了问题,并问了previous question

我在那里得到的答案很有帮助,但仍然要求我让SWIG做类似的事情:

%apply exportedClassType& OUTPUT { exportedClassType& result };

在SWIG 3.0.6上,这似乎对我有用,并带有以下信息:

Warning 453: Can't apply (exportedClassType &OUTPUT). No typemaps are defined.

the documentation看:

请注意,typemaps.i文件的主要用途是支持原始数据类型。写这样的函数 void foo(Bar *OUTPUT); 可能没有预期的效果,因为typemaps.i没有为Bar定义OUTPUT规则。

看起来不支持此功能。所以我想我的问题是,我需要在interface.i文件中定义什么样的组合图,以便生成类类型的包装器代码:

// interface.i
%apply exportedClassType& OUTPUT { exportedClassType& result };
int getClassType(exportedClassType& result);

// interface_wrap.cxx
SWIGINTERN PyObject *_wrap_getClassType(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
  PyObject *resultobj = 0;
  exportedClassType *arg1 = 0 ;
  void *argp1 = 0 ;
  int res1 = 0 ;
  PyObject * obj0 = 0 ;
  int result;

  if (!PyArg_ParseTuple(args,(char *)"O:getClassType",&obj0)) SWIG_fail;
  res1 = SWIG_ConvertPtr(obj0, &argp1, SWIGTYPE_p_exportedClassType,  0 );
  if (!SWIG_IsOK(res1)) {
    SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "getClassType" "', argument " "1"" of type '" "exportedClassType &""'"); 
  }
  if (!argp1) {
    SWIG_exception_fail(SWIG_ValueError, "invalid null reference " "in method '" "getClassType" "', argument " "1"" of type '" "exportedClassType &""'"); 
  }
  arg1 = reinterpret_cast< exportedClassType * >(argp1);
  result = (int)getClassType(*arg1);
  resultobj = SWIG_From_int(static_cast< int >(result));
  return resultobj;
fail:
  return NULL;
}

// wrapper.py
def getClassType(result):
    return _wrapper.getClassType(result)
getClassType = _wrapper.getClassType

更像这样的东西?

// interface.i
%apply bool& OUTPUT { bool& result };
int getSimpleType(bool& result); 

// interface_wrap.cxx
SWIGINTERN PyObject *_wrap_getSimpleType(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
  PyObject *resultobj = 0;
  bool *arg1 = 0 ;
  bool temp1 ;
  int res1 = SWIG_TMPOBJ ;
  int result;

  arg1 = &temp1;
  if (!PyArg_ParseTuple(args,(char *)":getSimpleType")) SWIG_fail;
  result = (int)getSimpleType(*arg1);
  resultobj = SWIG_From_int(static_cast< int >(result));
  if (SWIG_IsTmpObj(res1)) {
    resultobj = SWIG_Python_AppendOutput(resultobj, SWIG_From_bool((*arg1)));
  } else {
    int new_flags = SWIG_IsNewObj(res1) ? (SWIG_POINTER_OWN |  0 ) :  0 ;
    resultobj = SWIG_Python_AppendOutput(resultobj, SWIG_NewPointerObj((void*)(arg1), SWIGTYPE_p_bool, new_flags));
  }
  return resultobj;
fail:
  return NULL;
}

// wrapper.py
def getSimpleType():
    return _wrapper.getSimpleType()
getSimpleType = _wrapper.getSimpleType

我无法获得所需的输入/输出/ argout类型地图,因此我的SWIG_Python_AppendOutput会调用exportedClassType等。有人可以给我一些指示吗?没有双关语。

1 个答案:

答案 0 :(得分:1)

使用inargout类型地图的组合可以实现所需的行为。我已将它放在一个宏class_output_typemaps中,以便可以使用以下语法

启用它
%class_output_typemaps(exportedClassType)
%apply (exportedClassType*& ARGOUT_OBJECT) {(exportedClassType *&result)}

下面给出了一个完整的例子

example.h文件

#pragma once
class exportedClassType {
 public:
  exportedClassType();
  ~exportedClassType();
};
int getClassType(exportedClassType*& result);

example.cpp

#include "example.h"
#include <cstdio>

int getClassType(exportedClassType*& result) {
  result = new exportedClassType();
  return 0;
}

exportedClassType::exportedClassType() {printf("ctor\n");}
exportedClassType::~exportedClassType() {printf("dtor\n");}

example.i

%module example
%{
  #define SWIG_FILE_WITH_INIT
  #include "example.h"
%}

%include "typemaps.i"

/* %class_output_typemaps() macro
 *
 * It must be executed for each class CLASS_TYPE, which needs typemaps for output
 */
%define %class_output_typemaps(CLASS_TYPE)

%typemap(in, numinputs=0) (CLASS_TYPE *&ARGOUT_OBJECT) (CLASS_TYPE* temp) {
  $1 = &temp;
}

%typemap(argout) (CLASS_TYPE *&ARGOUT_OBJECT) {
  CLASS_TYPE* newObj;
  *(CLASS_TYPE**)&newObj = *$1;

  PyObject* temp = NULL;
  if (!PyList_Check($result)) {
    temp = $result;
    $result = PyList_New(1);
    PyList_SetItem($result, 0, temp);
  }

  temp = SWIG_NewPointerObj(SWIG_as_voidptr(newObj),
                $descriptor(CLASS_TYPE*),
                SWIG_POINTER_OWN | 0);

  PyList_Append($result, temp);
  Py_DECREF(temp);
}

%enddef    /* %class_output_typemaps() */

%class_output_typemaps(exportedClassType)

%apply (exportedClassType*& ARGOUT_OBJECT) {(exportedClassType *&result)}

%include "example.h"

setup.py

#!/usr/bin/env python
from distutils.core import setup, Extension
import numpy
import os

setup(name='test',
      version='1.0',
      ext_modules =[Extension('_example',
                              ['example.i', 'example.cpp'],
                              swig_opts=['-c++'],
                              include_dirs = ['.'])])

使用python setup.py build_ext --inplace构建后,您可以使用

在Python中创建对象
import example
myExportedClassType = example.getClassType()[1]
相关问题