【问题标题】:Swig wrapper for function with different struct pointer具有不同结构指针的函数的 Swig 包装器
【发布时间】:2019-07-01 15:44:07
【问题描述】:

我想包装下面的 C 函数。注意,从 Foo_t * 到 Bar_t * 的类型转换:

void function(Foo_t * f) {
      Bar_t * b = (Bar_t *) f;  // casting is done in C original code
      //do sth with b
}

Swig 生成一个遵循此模式的包装器:

void wrap_function( Foo_t *foo ) {
     function(foo);
}

但在 python 中,我想使用 Bar_t 实例调用我的包装函数:

b = Bar_t()
function(b) 

所以,我开始了以下类型映射:

%typemap(in) Foo * {
  Bar_t *temp;
  int res0 = 0;
  Foo_t *arg = 0;

  res0 = SWIG_ConvertPtr($input, (void **) &temp, $descriptor(Bar_t *), 0|0);
    if (!SWIG_IsOK(res0)) {
        SWIG_exception_fail(SWIG_ArgError(res0), "in method '" "function" "', argument " "1"" of type '" "Bar_t *""'"); 
    } 
     $1 = (Foo_t *) temp;
     function(arg);
}

但是抛出异常!

如何从 Bar_t * 转换为 Foo_t * ?

【问题讨论】:

  • 通过不同类型的指针访问是未定义的行为。如果需要继承,请使用 C++。
  • 如果 Foo * 和 Bar * 的对齐要求相同,我应该得到原始指针。我无法修改 C 代码端。

标签: python c swig


【解决方案1】:

如果您制作类型映射以期望 Foo* 输入的 Python Bar_t 包装器,您将无法将 Foo* 传递给 Foo* 输入。相反,导出一个演员表助手。请注意%inline 表示法既实现又导出内容的包装器。

test.h

#ifdef _WIN32
#   ifdef EXPORT
#       define API __declspec(dllexport)
#   else
#       define API __declspec(dllimport)
#   endif
#else
#   define API
#endif

typedef struct Foo {
    int a;
} Foo_t;

typedef struct Bar {
    int b;
} Bar_t;

API void function(Foo_t * f);

test.c

#define EXPORT
#include <stdio.h>
#include "test.h"

API void function(Foo_t * f) {
      Bar_t * b = (Bar_t *) f;  // casting is done in C original code
      // do something with b
}

test.i

%module test

%{
#include "test.h"
%}

%inline %{
Foo_t* Foo_cast(Bar_t* bar) {
    return (Foo_t*)bar;
}
%}

%include "test.h"

测试:

>>> import test
>>> bar = test.Bar_t()
>>> test.function(test.Foo_cast(bar))
>>> foo = test.Foo_t()
>>> test.function(foo)
>>>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 2015-12-23
    相关资源
    最近更新 更多