如何使用 Swig 将 void * 函数参数视为 Python 字符串 [英] How to use Swig to treat a void * function parameter as Python string

查看:31
本文介绍了如何使用 Swig 将 void * 函数参数视为 Python 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 C++ 代码:

Here is my c++ code:

struct Impl
{
  DT* data_ptr_;

  Impl(void* data_ptr)
    : data_ptr_((DT*)data_ptr)
  {
    //do something to decipher data
  }
};

Impl 类采用空指针作为输入参数.

Impl class takes a void pointer, as a input parameter.

我想要做的只是传递一个 Python 字符串(Python 2.x 中的二进制字符串)作为参数:

What I want to do is just pass a Python string(binary string as in Python 2.x) as parameter:

data = "I'm a stirng data!"
impl = Impl(data)

但是 Swig 生成的模块引发了这个

But Swig generated module raises this

TypeError: in method 'new_Impl', argument 1 of type 'void *'

我是 swig 的新手,现在已经在 SWIG 文档中搜索了一整天.唯一对我有用的是 swig 文档 8.3 C 字符串处理.但我的函数并没有真正采用大小整数.

I'm new to swig and have searched in SWIG documentation for a whole day now. The only thing worked for me is in swig document 8.3 C String Handling. But my function don't really take a size integer.

我觉得我的问题很简单,我一定漏掉了一些东西,请帮忙.

I think my problem is rather simple, I must missed something, please help.

谢谢!

推荐答案

我自己找到了解决方案.

I found a solution myself.

因为我只需要 void* 指针作为输入:

As I only needed the void* pointer as input:

%typemap(in) void* = char*;

会解决问题.

Swig 接受 char* 参数作为字符串类型,并将 void* 作为指针.所以使用 char* 输入类型就可以了.

Swig accept char* parameter as string type, and take void* just as an pointer. so use the char* input type will be okay.

这篇关于如何使用 Swig 将 void * 函数参数视为 Python 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆