SWIG 结构指针作为输出参数

2024-04-13

我有一个结构:

struct some_struct_s {
   int arg1;
   int arg2;
};

我有一个 C 函数:

int func(some_struct_s *output);

两者都是%included进入我的 SWIG 文件。

I want some_struct_s *output被视为输出参数。 Python 示例:

int_val, some_struct_output = func()

POD 类型手册中介绍了“输出参数”(第 10.1.3 节),但非 POD 类型未介绍。

我如何告诉 SWIG 我想要some_struct_s *output作为输出参数?


来自文档 http://www.swig.org/Doc3.0/SWIGDocumentation.html#Typemaps_nn32:

11.5.7 “argout”类型映射

“argout”类型映射用于从参数返回值。这最常用于为需要返回多个值的 C/C++ 函数编写包装器。 “argout”类型映射几乎总是与“in”类型映射结合在一起——可能会忽略输入值......

这是您的代码的完整示例(为简洁起见,没有错误检查):

%module test

// Declare an input typemap that suppresses requiring any input and
// declare a temporary stack variable to hold the return data.
%typemap(in,numinputs=0) some_struct_s* (some_struct_s tmp) %{
    $1 = &tmp;
%}

// Declare an output argument typemap.  In this case, we'll use
// a tuple to hold the structure data (no error checking).
%typemap(argout) some_struct_s* (PyObject* o) %{
    o = PyTuple_New(2);
    PyTuple_SET_ITEM(o,0,PyLong_FromLong($1->arg1));
    PyTuple_SET_ITEM(o,1,PyLong_FromLong($1->arg2));
    $result = SWIG_Python_AppendOutput($result,o);
%}

// Instead of a header file, we'll just declare this code inline.
// This includes the code in the wrapper, as well as telling SWIG
// to create wrappers in the target language.
%inline %{

struct some_struct_s {
   int arg1;
   int arg2;
};

int func(some_struct_s *output) {
    output->arg1 = 1;
    output->arg2 = 2;
    return 0;
}

%}

下面演示。请注意,int返回值零以及作为元组的输出参数都作为列表返回。

>>> import test
>>> test.func()
[0, (1, 2)]

如果您不需要类型映射,您还可以注入代码来创建对象并将其返回以对用户隐藏它:

%module test

%rename(_func) func; // Give the wrapper a different name

%inline %{

struct some_struct_s {
   int arg1;
   int arg2;
};

int func(struct some_struct_s *output)
{
    output->arg1 = 1;
    output->arg2 = 2;
    return 0;
}

%}

// Declare your interface
%pythoncode %{
def func():
    s = some_struct_s()
    r = _func(s)
    return r, s
%}

Demo:

>>> import test
>>> r, s = test.func()
>>> r
0
>>> s
<test.some_struct_s; proxy of <Swig Object of type 'some_struct_s *' at 0x000001511D70A880> >
>>> s.arg1
1
>>> s.arg2
2

如果您仔细选择 SWIG 宏,您可以使类型映射语言变得不可知:

%module test

%typemap(in,numinputs=0) struct some_struct_s *output %{
    $1 = malloc(sizeof(struct some_struct_s));
%}

%typemap(argout) struct some_struct_s* output {
    // The last parameter passes ownership of the pointer
    // to Python so it will be freed when the object's
    // reference count goes to zero.
    %append_output(SWIG_NewPointerObj($1, $1_descriptor, SWIG_POINTER_OWN));
}

%inline %{

struct some_struct_s {
   int arg1;
   int arg2;
};

int func(struct some_struct_s *output)
{
    output->arg1 = 1;
    output->arg2 = 2;
    return 0;
}

%}

Demo:

>>> import test
>>> r, s = test.func()
>>> r
0
>>> s
<test.some_struct_s; proxy of <Swig Object of type 'some_struct_s *' at 0x000001DD0425A700> >
>>> s.arg1
1
>>> s.arg2
2
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SWIG 结构指针作为输出参数 的相关文章

随机推荐

  • 裁剪以适合 svg 图案

    我有一些图案 每个图案中都有一个图像 我需要将图像缩放到其容器 即路径 的完整宽度或高度 同时保留其比例 本质上 如果您设置它们 它们的行为就需要像 html 图像一样min width 100 min height 100 我以前没有太多
  • swfupload 不允许我从一台服务器上传到另一台服务器

    我们有两个网络服务器 并且我们在这两个服务器之间专门执行任务 我们决定将所有 asp aspx 页面放入一台服务器中 并将 upload aspx sql 服务器放入另一台服务器中 这是服务器的名称 http server1 somecom
  • 为什么活动需要代表?为什么我们需要事件?

    过去几周我对发生的事情感到困惑 我了解代表如何工作 但不了解其详细工作方式 但足以了解这一点delegate datatype是单个演员委托 delegate void是一个多播委托 对方法的引用列表 我知道委托类型编译为类 但不幸的是我仍
  • 如何制作 npm 安装包并忽略一个(或所有)对等依赖项?

    I have email protected cdn cgi l email protection安装 我要安装vuex module decorators latest 其对等依赖性为vuex 3 not gt 3 我有一种感觉 这会很好
  • 从其他文件调用 php 类

    我正在研究一些自定义帖子类型 我完成了第一个 并意识到我正在使用的元框代码可以被其他未来的自定义帖子类型 以及页面 帖子等 重复使用 因此 我将该类放入其自己的 php 文件中 并将其包含在我的子主题的functions php 文件中 我
  • Haskell:无法导入 System.Random

    我已经按照找到的说明在 MacOS Mojave 上安装了 Haskellhere https www haskell org platform 即使用stack命令 然而 import System Random 带入ghci错误信息找不
  • 如何在 Oracle/SQL 中检索给定序列中的行?

    我有一个带有主键 MY PK 的表 MY TABLE 然后 我有一个有序主键列表 例如 17 13 35 2 9 现在我想检索具有这些主键的所有行 并以与给定键列表相同的方式保持行的顺序 我最初做的是 SELECT FROM MY TABL
  • 如何知道是否发生欠拟合或过拟合?

    我正在尝试用两个类进行图像分类 我有 1000 张具有平衡类别的图像 当我训练模型时 我得到了较低的恒定验证准确性 但验证损失却在减少 这是过度拟合或欠拟合的迹象吗 我还应该注意到 我正在尝试使用新类和不同的数据集重新训练 Inceptio
  • 无需外部库即可在 C# 中播放动态创建的简单声音

    我需要能够使用 C 动态生成波形并播放它 无需任何外部库 也无需在硬盘上存储声音文件 延迟不是问题 声音将在应用程序需要之前生成 实际上 如果不是微软表示 64 位版本的 Windows 不支持 Console Beep 方法 它可能会满足
  • 调用 getNextException 查看原因:How to make Hibernate / JPA show the DB server message for an exception

    我正在使用 Postgresql Hibernate 和 JPA 每当数据库中出现异常时 我都会得到类似的信息 这不是很有帮助 因为它没有显示数据库服务器上真正出了什么问题 Caused by java sql BatchUpdateExc
  • Java Swing:如何停止不需要的 Shift-Tab 击键操作

    当我在 JPanel 中有一个 JTextField 并且它具有焦点时 按 tab 不会执行任何操作 但按 shift tab 会导致焦点丢失 FocusEvent getOppositeComponent 为 null 如果 JPanel
  • 为 std::string 实现派生复制和移动构造函数?

    我正在尝试使用 Embarcaderos clang 32 位编译器编译 VTK 7 0 库 但是 我最后收到一个链接错误 如下所示 56 Linking CXX shared library bin vtkCommonDataModel
  • 如何监控每行 stdout 是 Bash 中最后一个输出行的时间以进行基准测试?

    例如 假设我有以下脚本 echo a sleep 1 echo b sleep 3 echo c sleep 2 其输出 a b c 当运行该脚本时 可能通过管道 我想获得类似的信息 1 00 a 3 00 b 2 00 c 因为线a是 s
  • 您可以使用 MacRuby 为 Mac App Store 开发应用程序吗?

    我对 Objective C 有一些基本的了解 但更喜欢 Ruby 所以我正在考虑使用 MacRuby 是否可以使用 MacRuby 为 Mac App Store 开发应用程序 还是必须使用 Objective C 请注意 我现在不太关心
  • 具有枚举功能的 Unity UI Onclick 检查器

    我有个问题 这是我的检查器窗口 在 On Click 窗口的情况下 我想设置枚举类型的参数 不是字符串或整数 换句话说 我想用 无效GoToNext DATA TYPE类型 但这并没有显示出来 即使我将枚举设置为 SerializedFie
  • 空 div 之间的间隙

    我尝试制作这样的 div 网格http jsfiddle net hGadw http jsfiddle net hGadw div div class inner top left nbsp div div class inner top
  • 当列是因子时,R data.table 将“NULL”替换为“NA”

    我通过 ODBC 从 SQL 数据库中提取一些数据 列自动设置为factor 它类似于以下内容 library RODBC library data table data lt data table sqlQuery channel que
  • 在“grep”结果中包含标头

    有没有一种方法可以将 head 1 和 grep 命令组合成一个目录中的所有文件 并将输出重定向到输出文件 我可以使用 sed 来完成此操作 但它似乎不如 grep 快 sed n 1p 6330162 p infile txt gt ou
  • 在没有 ResetEvent 的情况下调用 setEvent

    如果使用 setEvent 设置手动重置事件但未使用 ResetEvent 重置 会发生什么情况 并且该事件被触发多次 即当事件被处理时 事件再次被设置 以下是示例任务 void foo SetEvent hEvent1 void foo1
  • SWIG 结构指针作为输出参数

    我有一个结构 struct some struct s int arg1 int arg2 我有一个 C 函数 int func some struct s output 两者都是 included进入我的 SWIG 文件 I want s