%include typemaps.i %{ template < class _Elem, class _Traits> class basic_ProxyStreambuf : public std::basic_streambuf<_Elem, _Traits> { public: #if defined(SWIGRUBY) typedef VALUE handle_t; #endif protected: typedef std::basic_streambuf<_Elem, _Traits> super_t; typedef std::streamsize streamsize; typedef typename super_t::int_type int_type; handle_t handle; int_type in_buf; bool in_buf_ready; public: handle_t get_handle() const { return handle; } basic_ProxyStreambuf(handle_t _handle) throw(std::ios_base::failure) : super_t(), handle(_handle), in_buf_ready(false) { } ~basic_ProxyStreambuf() { //std::cerr << "~()" << std::endl; } protected: void update_in_buf(){ in_buf_ready = true; #if defined(SWIGRUBY) VALUE v; if((v = rb_io_getc(handle)) != Qnil){ in_buf = FIX2INT(v); //std::cerr << "received!" << std::end; return; } #endif in_buf = _Traits::eof(); } int_type overflow(int_type c = _Traits::eof()){ //std::cerr << "overflow()" << std::endl; if(c != _Traits::eof()){ #if defined(SWIGRUBY) char _c(NUM2CHR(INT2FIX(c))); if(rb_io_write(handle, rb_str_new(&_c, 1))){ return _Traits::not_eof(c); } #endif } return (_Traits::eof()); } int_type underflow(){ //std::cerr << "underflow()" << std::endl; if(!in_buf_ready){update_in_buf();} return in_buf; } int_type uflow(){ //std::cerr << "uflow()" << std::endl; update_in_buf(); return in_buf; } }; typedef basic_ProxyStreambuf > ProxyStreambuf; template class iProxyStream : public std::istream{ protected: typedef std::istream super_t; buf_t buf; public: iProxyStream(typename buf_t::handle_t handle) throw(std::ios_base::failure) : buf(handle), super_t(&buf){} ~iProxyStream(){} buf_t &buffer(){return buf;} }; template class oProxyStream : public std::ostream{ protected: typedef std::ostream super_t; buf_t buf; public: oProxyStream(typename buf_t::handle_t handle) throw(std::ios_base::failure) : buf(handle), super_t(&buf){ //std::cerr << "(ctr)()" << std::endl; } ~oProxyStream(){} buf_t &buffer(){return buf;} }; %} %typemap(in)(std::istream &){ $1 = new iProxyStream<>($input); } %typemap(freearg)(std::istream &){ // SWIG 10.5.8 delete $1; } %typemap(in)(std::ostream &){ $1 = new oProxyStream<>($input); } %typemap(freearg)(std::ostream &){ // SWIG 10.5.8 delete $1; }