basic_streambuf::seekoff what should be returned when ios_base::in | ios_base::out is specified?
Asked Answered
H

2

8

27.6.3.4.2 Buffer management and positioning

pos_type seekoff(off_type off, ios_base::seekdir way,
    ios_base::openmode which = ios_base::in | ios_base::out);
  • Effects: Alters the stream positions within one or more of the controlled sequences in a way that is defined separately for each class derived from basic_streambuf in this Clause (27.8.2.4, 27.9.1.5).
  • Default behavior: Returns pos_type(off_type(-1)).

So far, so good. The basic_streambuf derivation I'm using can alter its position separately for ios_base::in and/or ios_base::out. But, what do I need to return when both are specified?

If you specify ios_base::in or ios_base::out, we would return new stream position of the specific sequence.

Hauteur answered 23/7, 2011 at 13:51 Comment(5)
"in a way that is defined separately for each class" is that a specification, or a joke?Roadside
@Roadside - Even if it sounds weird, it's indeed a specification. It allows derived classes to behave in a sensible way that may vary between different implementations and use cases. However, it's not always that intuitive how a implementation "should" behave.Hauteur
"it's indeed a specification" But it isn't. It's an allusion to some other specification somewhere else. "It allows derived classes to behave in a sensible way that may vary between different implementations and use cases." What's the point of this "specification"? What can be done knowing that seekoff alters the stream positions within one or more of the controlled sequences in a way that is defined separately for each derived class?Roadside
@Roadside - From a top-down perspective where you're not aware of the actual implementation: You don't know how the stream positions are altered. But this concept allows varying implementations, behaving like a streambuf, but stay flexible in position handling.Hauteur
What can you safely do with basic_streambuf::seekoff?Roadside
P
1

It is a bit up to your stream to define what happens. The built in streams differ, in that some can have separate read and write positions (stringstream) while others just have one (fstream).

If the user does a reposition and specify both in and out, perhaps you should move both. If it is a seek with a zero offset to get the current position, it is not unreasonable to fail if the positions differ.

Portsalut answered 23/7, 2011 at 14:46 Comment(1)
Another option would be to move both by the offset, but I don't know what I should return. I think you're right and it would be most reasonable to fail if the positions differ.Hauteur
A
0

Following seekoff from 27.8.2.4 it seems that you are expected to fail.

Check the condition table 130, which states that both input and output sequences should be positioned only if

(which & (ios_base::in | ios_base::out)) == (ios_base::in) | ios_base::out))
and way == either ios_base::beg or ios_base::end
Assai answered 23/7, 2011 at 22:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.