[英]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.