std::basic_sreambuf::setp 2 args vs 3 args

http://en.cppreference.com/w/cpp/io/basic_streambuf/setp
http://msdn.microsoft.com/en-us/library/vstudio/y23hd8dd(v=vs.90).aspx
My streambuf header:
255
256
257
258
259
260
261
262
263
264
265
266
267
	void setp(_Elem *_First, _Elem *_Last)
		{	// set pointers for write buffer
		*_IPfirst = _First;
		*_IPnext = _First;
		*_IPcount = (int)(_Last - _First);
		}

	void setp(_Elem *_First, _Elem *_Next, _Elem *_Last)
		{	// set pointers for write buffer, extended version
		*_IPfirst = _First;
		*_IPnext = _Next;
		*_IPcount = (int)(_Last - _Next);
		}
What's going on here? Even the tutorials I've read use code with the three-parameter version, so why is it that the two-parameter version is the actual standard? What's equivalent standard code for using the three parameter version?
Last edited on
Your streambuf header has a non-standard extension, that's pretty common.

If you want to set pptr() different from pbase() in portable manner, you can pbump() it after calling setp() (note that it's rarely needed - unlike in the get area where you can putback to access something to the left of the next pointer)

PS: found an example on SO demonstrating a socket-backed stream.. indeed, here pptr() needs to be set different from pbase()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int fdbuf::sync() {
    if (this->pbase() != this->pptr()) {
        std::streamsize size(this->pptr() - this->pbase());
        std::streamsize done(::write(this->fd_, this->outbuf_, size));
        // The code below assumes that it is success if the stream made
        // some progress. Depending on the needs it may be more
        // reasonable to consider it a success only if it managed to
        // write the entire buffer and, e.g., loop a couple of times
        // to try achieving this success.
        if (0 < done) {
            std::copy(this->pbase() + done, this->pptr(), this->pbase());
            this->setp(this->pbase(), this->epptr());
            this->pbump(size - done);
        }
    }
    return this->pptr() != this->epptr()? 0: -1;
}

http://stackoverflow.com/a/13542806/273767
Last edited on
While I agree that the second version is more convenient (like the three-parameter setg), I think someone decided to take some liberties with the standard library.

I think what one would have to do is repeatedly call pbump to advance the put pointer. Not exactly convenient or efficient, but I'm not sure what else could be done.

EDIT: Cubbi beat me by a long shot.

-Albatross
Last edited on
Thanks, I got it figured out.
Topic archived. No new replies allowed.