简体   繁体   English

使用swig包装类似读/写的函数

[英]using swig to wrap read/write like functions

This question is about using SWIG, to create a Python/C++ interface. 这个问题是关于使用SWIG创建Python / C ++接口的。

My C++ code, has some functions that model the classic unix read/write and I need to call these from Python. 我的C ++代码具有一些用于模拟经典unix读/写的函数,我需要从Python调用这些函数。 The C++ class is like this: C ++类是这样的:

class DEVICE {
    ... other stuff ...
    int write_bytes( const uint8_t *bytes, size_t nbytes, unsigned timeout_msecs );
    int read_bytes(  uint8_t *bytes, size_t nbytes, unsigned timeout_msecs );
}

These functions exactly model the unix read() and write(), but with a timeout parameter, these return the number of actual bytes transferred. 这些函数精确地对unix的read()和write()进行建模,但是带有超时参数,它们返回已传输的实际字节数。

I am looking for the "correct" python API I should provide. 我正在寻找应该提供的“正确” Python API。 I am also a python Noob .. so If I am doing something wrong with python please correct me. 我也是python Noob ..因此,如果我在python中做错了什么,请纠正我。

I think I can figure the WRITE side out .. 我想我可以弄清楚WRITE的一面..

Python WRITE: The Python function should have 2 parameters: An array of bytes, and a timeout value. Python WRITE:Python函数应具有2个参数:字节数组和超时值。 Since Python arrays contain their length ... the length is thus an implied parameter. 由于Python数组包含其长度...,因此长度是一个隐含参数。

C++ Code for WRITE: Since memory is coming from python, I only need to ask the array how many bytes it contains, get the raw byte pointer, and call my C function some how. C ++ WRITE代码:由于内存来自python,所以我只需要问数组包含多少字节,获取原始字节指针,然后以某种方式调用C函数即可。

The READ side ... I need help with. READ面...我需要帮助。

Python READ: Again, 2 parameters, an array of bytes and a timeout value. Python READ:同样,有2个参数,一个字节数组和一个超时值。 Here, since I do not know the actual number of bytes that will be read - I think my C code should allocate memory on behalf of Python (or is that wrong? I do not know). 在这里,由于我不知道将要读取的实际字节数-我认为我的C代码应该代表Python分配内存(否则那是错的吗?我不知道)。 Then - how do I set the resulting length? 然后-如何设置结果长度? I don't see/find an example of this. 我没有看到/找到这个例子。 Or do I make my user/victim pre-allocate the array? 还是让用户/受害者预先分配阵列? (that seems wrong) (这似乎是错误的)

I have looked in the LLDB code for examples, specifically - I see these %typemaps - Specifically: 我查看了LLDB代码中的示例,具体来说-我看到了这些%typemaps-具体来说:

https://github.com/carlokok/lldb/blob/master/scripts/Python/python-typemaps.swig#L134 https://github.com/carlokok/lldb/blob/master/scripts/Python/python-typemaps.swig#L134

I do not fully understand typemaps (I am also new to swig...) but they appear to be very close. 我不完全了解typemap(我也是Swig的新手...),但它们似乎非常接近。 But not exactly what I am looking for. 但不完全是我想要的。 Can somebody explain how these work? 有人可以解释这些工作原理吗? The SWIG docs are not helpful (reason: There are lots of 'snippits' of code, I cannot find a complete end to end coherent example) SWIG文档没有帮助(原因:有很多“代码段”代码,我找不到完整的端到端连贯示例)

Can somebody create a better (coherent) example? 有人可以创造一个更好的(连贯的)例子吗? Or point me to an example to read? 还是为我提供一个阅读范例?

There are two ways that I know of that you can do this: 我知道有两种方法可以做到这一点:

1: you can have the program redirect it's standard io streams to files, and then your python (or whatever program) program can just write to the input file to input, and read from the output file to get output. 1:您可以让程序将其标准io流重定向到文件,然后python(或任何程序)程序可以只写到输入文件进行输入,然后从输出文件读取以获取输出。

2: you can call the functions by useing argv and argc. 2:您可以使用argv和argc来调用函数。 While output can not (unless you redirect the program's output from an outside source) retrieve the program's output, it allows you to enter input. 尽管输出不能(除非您从外部源重定向程序的输出)检索程序的输出,但它允许您输入输入。

I havn't messed with redirecting streams (yet), but I presume it's as easy as opening up some file streams, storing the previous iostreams in new stream, and setting std::cout and std::cin to (std::istream) or (std::ostream) casts of std::ifstream or std::ofstream. 我还没有弄过重定向流的问题(但是),但我想这就像打开一些文件流,将先前的iostream存储在新流中,并将std :: cout和std :: cin设置为(std :: istream )或(std :: ostream)强制转换为std :: ifstream或std :: ofstream。 If it isn't, then you'll have to do some research. 如果不是,则必须进行一些研究。

Your case is already covered by the SWIG typemap library. 您的案例已被SWIG类型图库覆盖。 This means you should be able to use the %apply (Type* INPUT, size_t length) directive as explained in a couple of places in SWIG docs, such as section 8.3 . 这意味着您应该能够使用%apply (Type* INPUT, size_t length)伪指令,如SWIG文档中几个地方所述,例如8.3节 Since you don't show any code in your question, I suggest you give this a shot and if you have any trouble, update your question to show the code you tried and what happens, I or someone else may be able to provide a more detailed answer. 由于您没有在问题中显示任何代码,因此建议您尝试一下,如果遇到任何问题,请更新您的问题以显示您尝试过的代码以及发生了什么,我或其他人可能会提供更多信息。详细的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM