Custom flush implementation

2019-06-21 22:27发布

问题:

I'm trying to follow the logic of this question to create a custom streambuf in Rcpp. Someone contributed the basic behaviour that allows us to write things like

Rcout << "some text" ;

where we implemented xsputn and overflow to redirect to Rprintf function.

std::streamsize Rcpp::Rstreambuf::xsputn(const char *s, std::streamsize num ) {
    Rprintf( "%.*s", num, s );
    return num;
}

int Rcpp::Rstreambuf::overflow(int c ) {
    if (c != EOF) {
        Rprintf( "%.1s", &c );
    }
    return c;
}

I would like to implement flushing too, i.e. support this syntax:

Rcout << "some text" << std::flush ;

Which method do I need to implement so that the flush manipulator works on my custom stream ?

回答1:

It is sync() function (like in filebuf):

protected:
virtual int sync()

Base version of base_streambuf<>::sync() does nothing, one must overwrite it to make some synchronization with underlying stream.



标签: c++ iostream