Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a C++ STL class that works like a pipe?

Tags:

c++

stl

In abstract terms a pipe is a stream/FIFO style container with a destructive read. It has a read() method that copies chunks of data all at once, and functions like get and put for single bytes. When the read or get returns though, the data that was copied out of the pipe is removed from the pipe's internal buffer - unlike a file or any other container type.

Most (all?) of the STL containers do not provide a similar read() in to a buffer command. Is there a fifo container type with a pop_many() member?

stringstream is about the closest thing I can think of since it maintains an internal read pointer and future reads will block until the stream fills up again. The container is empty from the API point of view, but the consumed data would have to be garbage collected manually.

Is there an equivalent container or stream class in C++ that does this, or is it a matter of roll-your-own (like in the stringstream example)?

like image 236
Mr Stinky Avatar asked Aug 15 '12 12:08

Mr Stinky


1 Answers

The comments have already mentioned std::deque which at first glance appears to be your best choice.

If that doesn't work for you, what about using a std::list<std::vector<unsigned char> >. You put chunks on one vector at a time, and you splice them out into another list when you're popping. You'd have to provide a small amount of convenience wrapper code, and this might not be sufficient if you don't want to read all the elements of one of the sub-vectors at once.

like image 56
Mark B Avatar answered Nov 08 '22 04:11

Mark B