Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为类类型创建输出类型映射?_Python_C++_Swig - Fatal编程技术网

Python 如何为类类型创建输出类型映射?

Python 如何为类类型创建输出类型映射?,python,c++,swig,Python,C++,Swig,我以前在尝试将SWIG的输出类型映射应用于类类型时遇到过问题,并询问了这个问题 我在那里得到的答案很有帮助,但仍然需要我要求SWIG做一些事情,比如: %apply exportedClassType& OUTPUT { exportedClassType& result }; 这在SWIG 3.0.6上似乎不适用,并显示以下消息: 警告453:无法应用(exportedClassType&OUTPUT)。未定义类型映射。 从以下方面来看: 请注意,typemaps.i文件的主

我以前在尝试将SWIG的输出类型映射应用于类类型时遇到过问题,并询问了这个问题

我在那里得到的答案很有帮助,但仍然需要我要求SWIG做一些事情,比如:

%apply exportedClassType& OUTPUT { exportedClassType& result };
这在SWIG 3.0.6上似乎不适用,并显示以下消息:

警告453:无法应用(exportedClassType&OUTPUT)。未定义类型映射。

从以下方面来看:

请注意,typemaps.i文件的主要用途是支持基本数据类型。编写这样的函数
void foo(条形*输出)
可能没有预期的效果,因为typemaps。我没有为Bar定义输出规则

这看起来不受支持。所以我想我的问题是,我需要在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
%应用exportedClassType和输出{exportedClassType和结果};
int getClassType(exportedClassType和result);
//接口_wrap.cxx
SWIGINTERN PyObject*\u wrap\u getClassType(PyObject*SwigunUsedPart(self),PyObject*args){
PyObject*resultobj=0;
exportedClassType*arg1=0;
void*argp1=0;
int res1=0;
PyObject*obj0=0;
int结果;
如果(!PyArg_ParseTuple(args,(char*)“O:getClassType”,&obj0))SWIG_失败;
res1=SWIG_ConvertPtr(obj0和argp1,SWIGTYPE_p_exportedClassType,0);
如果(!SWIG_IsOK(res1)){
SWIG_异常_失败(SWIG_ArgError(res1),“在方法”“getClassType”“中,参数”“1”“的类型为”“exportedClassType&”“”;
}
如果(!argp1){
SWIG\u异常\u失败(SWIG\u ValueError,方法“getClassType”中的“无效空引用”,类型为“exportedClassType&”的参数“1”;
}
arg1=重新解释类型(argp1);
结果=(int)getClassType(*arg1);
resultobj=从内部旋转(静态转换(结果));
返回结果j;
失败:
返回NULL;
}
//包装纸
def getClassType(结果):
return\u wrapper.getClassType(结果)
getClassType=\u 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
//interface.i
%应用bool&OUTPUT{bool&result};
int getSimpleType(bool&result);
//接口_wrap.cxx
SWIGINTERN PyObject*\u wrap\u getSimpleType(PyObject*SwigunUsedPart(self),PyObject*参数){
PyObject*resultobj=0;
bool*arg1=0;
bool-temp1;
int res1=SWIG_TMPOBJ;
int结果;
arg1=&temp1;
如果(!PyArg_ParseTuple(args,(char*)“:getSimpleType”))SWIG_失败;
结果=(int)getSimpleType(*arg1);
resultobj=从内部旋转(静态转换(结果));
如果(SWIG_IsTmpObj(res1)){
resultobj=SWIG_Python_AppendOutput(resultobj,SWIG_From_bool(*arg1));
}否则{
int new_flags=SWIG_IsNewObj(res1)?(SWIG_POINTER_OWN | 0):0;
resultobj=SWIG_Python_AppendOutput(resultobj,SWIG_NewPointerObj((void*)(arg1),SWIGTYPE_p_bool,new_标志));
}
返回结果j;
失败:
返回NULL;
}
//包装纸
def getSimpleType():
返回_wrapper.getSimpleType()
getSimpleType=\u wrapper.getSimpleType

我没有得到所需的输入/输出/参数化类型映射,因此为我的
exportedClassType
调用了
SWIG\u Python\u AppendOutput
等。谁能给我一些指点吗?没有双关语。

可以使用
in
argout
类型映射的组合来实现所需的行为。我已经把它放在一个宏
class\u output\u typemaps
中,这样就可以使用以下语法启用它

%class_output_typemaps(exportedClassType)
%apply (exportedClassType*& ARGOUT_OBJECT) {(exportedClassType *&result)}
下面给出了一个完整的例子

例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");}
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\u ext--inplace
构建之后,您可以使用

import example
myExportedClassType = example.getClassType()[1]

你能提供你想要导出的方法的原型吗。我通常包装构造函数并让SWIG处理多态性,但我已经设法创建了一个工厂,其中的输出是通过引用的
import example
myExportedClassType = example.getClassType()[1]