I have two functions:
Get (std::ostream* os) and Put (std::istream& is)
The way get functions is, it has multiple streams underneath from which it gathers the data and sends it out via the output stream os.
Put on the other hand, writes the data from input stream into multiple streams underneath which eventually stores the data.
Now, I wanted to write a piece of code to get the data using GET and do a PUT of that data.
However, I am not sure how to write to a istream or how to implement this piece of connector code. Im new to using streams and im trying to understand them too. Any help would be appreciated.
Thanks, Sethu
It seems, you want to create something that channels the data written to Get()
's std::ostream
to Put()
's std::istream
. To do so you'll need to write a suitable stream which may need to deal with connecting multiple threads. If it is sufficient to read data from the stream written to by Get()
after returning from Get()
, you can just write to an std::ostringstream
and make the written bytes available to Put()
using an std::istringstream
. I'm assuming you want a more direct connection and describe the general approach.
The underlying mechanics of streams are implement in stream buffers, i.e., classes derived from std::streambuf
(or, if you want to support other character types like wchar_t
or char32_t
, the class template std::basic_streambuf
. The key functions to override are
overflow(int_type)
which is called for an output stream whenever the stream buffer's buffer is full.sync()
which is called for output streams when the stream's buffer needs to be flushed.underflow()
which is called for input stream if the stream's buffer is drained.The details of how to implement the processing are a tad bit more involved. I have written many answers explaining various uses creating stream buffers. For a stream connecting an output stream to an input stream have a look at this answer.