程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在Python中为类类型应用SWIG OUTPUT类型映射?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何在Python中为类类型应用SWIG OUTPUT类型映射??

开发过程中遇到如何在Python中为类类型应用SWIG OUTPUT类型映射?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何在Python中为类类型应用SWIG OUTPUT类型映射?的解决方法建议,希望对你解决如何在Python中为类类型应用SWIG OUTPUT类型映射?有所启发或帮助;

这个问题似乎已经解决了很长时间了,所以我认为我最好为这个问题提供解决方案。OUTPUT类型映射仅适用于简单类型,因此可以通过将in和类型argout映射组合来提供解决方案。

虑一下这种情况,我们有一个SampleImpl实现C 类SampleBase,从技术上来说,它不是接口,因为它涉及虚拟析构函数的实现。假设我们有一个静态函数,该函数返回错误代码和接口的实现。后者是对指针的引用,这是上面的情况。

接口头:

// Sample.hpp
#pragma once
namespace Module {
  class SampleBase {
  public:
#ifndef SWIG
    // Hint to the progrAMMer to implement this function
    static int SampleCreate(SampleBase *&obj);
#endif
    virtual ~SampleBase() = default;
  };
}

实施标头:

// Sample_impl.hpp
#pragma once
#include "Sample.hpp"

namespace Module {
  class SampleImpl : public SampleBase {
  public:
    static int SampleCreate(Module::SampleBase *&obj);

    SampleImpl();
    virtual ~SampleImpl();
  private:
    float a;
  };
}

实现方式:

// Sample_impl.cpp
#include "Sample_impl.hpp"
#include <cstdio>

namespace Module {
  int SampleImpl::SampleCreate(Module::SampleBase*& obj) {
    obj = (SampleBase*) new SampleImpl();
    return 0;
  }
  SampleImpl::SampleImpl() {
    printf("SampleImpl::SampleImpl()\n");
  }

  SampleImpl::~SampleImpl() {
    printf("SampleImpl::~SampleImpl()\n");
  }
}

SWIG界面(使用argout类型映射)

// example.i
%module example
%{
  #define SWIG_file_WITH_INIT
  #include "Sample.hpp"
  #include "Sample_impl.hpp"
%}

%include "typemaps.i"

%typemap(in, numinputs=0) Module::SampleBase *&obj (Module::SampleBase *temp) {
  $1 = &temp;
}

%typemap(argout) Module::SampleBase *& {
  PyObject* temp = NulL;
  if (!PyList_check($result)) {
    temp = $result;
    $result = PyList_New(1);
    PyList_SetItem($result, 0, temp);

    // Create shadow object (do not use SWIG_POINTER_NEW)
    temp = SWIG_NewPointerObj(SWIG_as_voIDptr(*$1),
             $descriptor(Module::SampleBase*),
             SWIG_POINTER_OWN | 0);

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

在Python中的用法

import example

// CreaTing specialization
obj = example.SampleImpl()
del obj

// Creation of object using output typemap
errorCode, obj = example.SampleImpl_SampleCreate()
del obj

解决方法

我在使用SWIG(版本3.0.6)围绕C ++库生成Python包装程序时遇到了一些麻烦。

我的问题与应用OUTPUT类型映射有关,特别是在对类类型的指针/引用的情况下。

为了说明这一点,这是我想要的标准类型,它的工作原理是:

// .h
int add(const long arg1,const long arg2,long& resultLong);

// interface.i
%apply long& OUTPUT { long& resultLong };
int add(const long arg1,long& resultLong);

// projectWrapper.py
def add(arg1,arg2):
    return _projectWrapper.add(arg1,arg2)
addTerm = _projectWrapper.add

// usage
>>> result = projectWrapper.add(2,4)
>>> print result
[0,6L]

您不必传递“ resultLong”,但会自动将其附加到结果中。大!

但是,当输出类型是指向类类型的某种指针时,这似乎不像我期望的那样工作:

// .h
int GetClassType(const char* name,exportedClassType*& resultPointer);

class exportedClassType
{...}

// interface.i
%apply exportedClassType*& OUTPUT { exportedClassType*& resultPointer };    
int GetClassType(const char* name,exportedClassType*& resultPointer);

// projectWrapper.py
def GetClassType(name,resultPointer):
    return _projectWrapper.GetClassType(name,resultPointer)
GetClassType = _projectWrapper.GetClassType

问题似乎在于SWIG并未以与简单类型相同的方式对其进行处理。它仍然在包装的函数签名中显示为“输入”参数。

// attempted usage
>>> classType = projectWrapper.GetClassType("name")
TypeError: GetClassType() takes exactly 2 arguments (1 given)

>>> result = 0
>>> projectWrapper.GetClassType("name",result)
TraceBACk (most recent call last):
File "<input>",line 1,in <module>
TypeError: in method 'GetClassType',argument 2 of type 'exportedClassType *&'

有人可以告诉我我做错了什么,还是指出正确的方向吗?任何帮助表示感谢!谢谢

大佬总结

以上是大佬教程为你收集整理的如何在Python中为类类型应用SWIG OUTPUT类型映射?全部内容,希望文章能够帮你解决如何在Python中为类类型应用SWIG OUTPUT类型映射?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。