1

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.

Kenny Peng
  • 1,891
  • 4
  • 18
  • 26

2 Answers2

1

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.

Nathan Kitchen
  • 4,799
  • 1
  • 24
  • 19
0

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

Community
  • 1
  • 1
Baget
  • 3,318
  • 1
  • 24
  • 44