$stdin compatibility with std::istream using swig, C++, and Ruby
Question
I have a function in C++ that takes in an std::istream as the input:
class Foo {
Foo(std::istream &);
}
Using SWIG, I've bound it to Ruby, but Ruby's $stdin variable is fundamentally different from anything like the stream classes in C++, so I'm not sure how to either 1) expose the C++ class to Ruby in a way that I can use $stdin, or 2) convert $stdin into something the C++ class can understand.
Anyone have experience with binding iostreams in C++ to Ruby?
Thanks.
Solution
You can use an instance of std::istream that implements its operations with Ruby methods on $stdin called through the C interface (e.g., using rb_funcall). You can't do it by deriving a class from std::istream itself, because its methods are not virtual; instead you'll need to derive from std::stream_buf and instantiate an istream that uses your stream buffer.
OTHER TIPS
maybe you can use C style File Descriptors instead of istream and then "convert" it to C++ stream,
I think you can use the answers in this question