简体   繁体   English

如何使用SWIG和CGO将io.Reader转换为std :: istream?

[英]How do I convert an io.Reader to a std::istream with SWIG & CGO?

I'm trying to use SWIG to create a Go wrapper for a C++ API that uses std::istream to read binary data. 我正在尝试使用SWIG为使用std::istream读取二进制数据的C ++ API创建Go包装器。 I'd like to be able to pass an io.Reader to these APIs, but I'm not sure how to create a mapping between it and std::istream . 我希望能够将io.Reader传递给这些API,但是我不确定如何在它与std::istream之间创建映射。 I know I need to implement a std::streambuf subclass and I assume the rest will involve directors and typemaps, but I'm not familiar enough with SWIG to figure out the right combination. 我知道我需要实现一个std::streambuf子类,并且我认为其余的都将涉及Director和Typemap,但是我对SWIG不够熟悉,无法找出正确的组合。

Any ideas? 有任何想法吗?

io.Reader is too general to be passed to C functions -- it might not be backed on a real file at all (it's just a class that implements a Read(...) function) io.Reader太笼统了,无法传递给C函数-它可能根本不备份在真实文件上(它只是实现Read(...)函数的类)

What you could do (so long as you aren't on windows) is use os.Pipe() to give you a ture FH object, but sadly the stock std::*stream doesn't have any way of creating a stream from an open file handle. 您可以做的事情(只要您不在Windows上)就是使用os.Pipe()给您一个真正的FH对象,但是可惜的是std :: * stream没有任何方法可以从打开文件句柄。

The pipe bit looks something like this: 管道位看起来像这样:

func wrapReader(r io.Reader) uintptr {
    pr, pw, err := os.Pipe()
    if err != nil {
        panic(err)
    }

    go func () {
        _, _ io.Copy(pw, r)
        _ = pw.Close()
    }()

    return pr
}

If you combine that some of the code in this answer How to construct a c++ fstream from a POSIX file descriptor? 如果您在此答案中结合了一些代码,那么如何从POSIX文件描述符构造c ++ fstream? you might get what you need 您可能会得到所需的东西

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

相关问题 如何将C ++库与CGO和Swig链接? - How do I link a C++ library with CGO and Swig? 如何将QByteArray转换为std :: istream或std :: ifstream? - How to convert QByteArray to std::istream or std::ifstream? 如何将std :: istream转换为std :: wistream - How to convert a std::istream to std::wistream 我如何使用std :: istream读取bool值 - How do i read bool value using std::istream 如何将 std::ifstream 转换为 std::basic_istream<chart, traits> &amp;?</chart,> - How to convert a std::ifstream to std::basic_istream<CharT, Traits>&? 如何从 std::vector 转换<float>到 std::istream?</float> - How to convert from std::vector<float> to std::istream? 如何使用 swig/cgo 调试“退出状态 3221225477”? - How to debug “exit status 3221225477” with swig/cgo? 如何通过在 c++ 中接收 std::istream object 来读取和解析逗号分隔的用户的输入? - How do I read and parse input from a user that is comma separated by receiving an std::istream object in c++? 使用std :: istream :: operator &gt;&gt;处理无符号类型时,如何区分从下溢中提取失败? - With std::istream::operator>> working on unsigned types, how do I tell apart a failed extraction from an underflow? 使用 std::istream_iterator 时,它似乎跳过空文件行 - 如果可能,我该如何避免这种情况? - When using std::istream_iterator it seems to skip empty file lines - how do I avoid this if possible?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM