为C ++(指针)创建python的Swig包装器 [英] Creating swig wrapper for C++ (pointers) to python

查看:181
本文介绍了为C ++(指针)创建python的Swig包装器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是swig的一个新手,我正在尝试创建一个swig包装器,以便在python中使用一些C ++文件.我有以下C ++类.

I a very new to swig and I am trying to create a swig wrapper in order to use a few C++ files in python. I have the following C++ class.

以下是我要转换的代码片段:

The following is a snippet of the code that I am trying to convert:

/*packet_buffer.h*/
class CPacketBuffer {
 public:
    // construct based on given buffer, data is not copied
    CPacketBuffer(uint8_t* data, uint32_t length) {
        mpBuffer = data;
        mLength  = length;
        mHead    = 0;
        mTail    = length;
    }
    uint8_t* GetBuffer() {
        return (mpBuffer + mHead);
    }
    void Add(const uint8_t* data, uint32_t length) {
            if ((mTail + length) > mLength) {
            length = (mLength - mTail);
        }
//....
}

我一直在尝试编写一个example.i文件,该文件今天全天都在swig文档的帮助下接受指向typedefs(uint8_t *)的指针,但我一直没有成功.

I have been trying to write a example.i file that would accept pointers to typedefs(uint8_t *) all day today using help from swig documentation, but I have been unsuccessful.

以下是我尝试过的packet_buffer.i文件,该文件不起作用.

The following is a packet_buffer.i file that I have tried which doesn't work.

%module packet_buffer
%include typemaps.i
%apply unsigned char* {uint8_t*};
%apply unit8_t *INPUT {uint8_t *data};



%{
    #define SWIG_FILE_WITH_INIT
    #include "../include/packet_buffer.h"
%}
%include "../include/packet_buffer.h"

  1. 如何为使用指向typedef的指针的成员函数编写swig代码?
  2. 我可以编写可在代码中使用的通用%apply,还是必须为每个INPUT,OUTPUT参数编写详细信息?

推荐答案

如果我正确理解了这一点,那么您面临的问题不是它们是指针,而是它们可能是无界数组.

If I've understood this correctly the problem you're facing isn't that they're pointers, it's that they're potentially unbounded arrays.

您可以使用carrays.i 扭曲无边界的C数组.和%array_class"宏,例如:

You can warp an unbounded C array using carrays.i and the "%array_class" macro, e.g.:

%module packet
%include "stdint.i"

%{
    #include "packet.h"
%}

%include "carrays.i"
%array_class(uint8_t, buffer);

%include "packet.h"

然后将允许您在Py​​thon中编写类似的内容:

Would then allow you to in Python write something like:

a = packet.buffer(10000000) 
p = packet.CPacketBuffer(a.cast(), 10000000)

请注意,您需要确保缓冲区的寿命足够长-如果在不了解C ++代码的情况下释放Python对象,最终将导致不确定的行为.

Note that you'll need to ensure the life of the buffer is sufficient - if the Python object gets released without the C++ code being aware you'll end up with undefined behaviour.

您可以使用%array_class宏还创建的frompointer方法将uint8_t*指针(无界数组)转换为Python中的缓冲区实例,例如:

You can convert uint8_t* pointers (unbounded arrays) to buffer instances in Python using the frompointer methods that the %array_class macro also creates, e.g.:

r = packet.GetBuffer()
buf = packet.buffer_frompointer(r)

您可以添加其他Python代码以自动/隐藏大多数缓冲区之间的转换(如果需要),或者使用 MemoryViews 进行在C API方面与Python紧密集成.

You can add additional Python code to automate/hide most of the conversion between buffers if desired, or use MemoryViews to integrate tighter with Python on the C API side.

尽管一般来说,由于这是C ++,所以我建议为此使用std::vector-在Python端使用它比无边界数组要好得多,并且这样做的代价是安全性和简单性最小.

In general though since this is C++ I'd suggest using std::vector for this - it's much nicer to use on the Python side than the unbounded arrays and the cost is minimal for the safety and simplicity it gives you.

这篇关于为C ++(指针)创建python的Swig包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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