streambuf, yet again

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

Is there any way to prevent the streambuf from calling overflow or
xsputn unless I specifically flush the stream?
 
J

John Harrison

Christopher Benson-Manica said:
Is there any way to prevent the streambuf from calling overflow or
xsputn unless I specifically flush the stream?

Its ostream that calls overflow or xsputn.

I can't think of any reasonable way to achieve that, but I could easily be
wrong. But I am sure it would be better to code this so that you don't much
care when overflow or xsputn is called. Why exactly does it matter?

Is your stream working now then? What was the problem? Before you were
complaining that overflow wasn't being called at all, not you are
complaining that its being called to often!

john
 
C

Christopher Benson-Manica

John Harrison said:
I can't think of any reasonable way to achieve that, but I could easily be
wrong. But I am sure it would be better to code this so that you don't much
care when overflow or xsputn is called. Why exactly does it matter?

Well, this is a deficiency of the class I'm trying to wrap -
TLSFile::Write() appends a '\n' to its argument before actually
writing to the file. So I have to code around it, convince my boss to
change that behavior (he seemed reluctant this morning), or just
forget wrapping the class with a stream at all.
Is your stream working now then? What was the problem? Before you were
complaining that overflow wasn't being called at all, not you are
complaining that its being called to often!

Well, it kind of is. It turns out that the first time through the
section of code in question, nothing happens. On subsequent trips
through that section of code, however, things work as expected
(although not as I would want - see above). Does that sound like a
specific kind of mistake to you?
 
J

John Harrison

Christopher Benson-Manica said:
Well, this is a deficiency of the class I'm trying to wrap -
TLSFile::Write() appends a '\n' to its argument before actually
writing to the file. So I have to code around it, convince my boss to
change that behavior (he seemed reluctant this morning), or just
forget wrapping the class with a stream at all.

Hmm awkward. And some people claim that object orientation promotes reusable
code. I'd work on your boss if I were you.

What you can do is create a real buffer (instead of using the unbuffered
mode you are using now). See Josuttis for details. Then when the buffer is
full and overflow is called you scan the characters in the buffer for
newlines. If you find any you can call TLSFile::Write with the characters
before the newline and just drop the newline. You won't be able to empty the
buffer entirely, just the portion that is before the last newline, but that
isn't a problem. If when overflow is called and the buffer is full and there
isn't a single newline in it, then you are either going to have to output a
spurious newline, or you are going to have to expand the buffer.
Well, it kind of is. It turns out that the first time through the
section of code in question, nothing happens. On subsequent trips
through that section of code, however, things work as expected
(although not as I would want - see above). Does that sound like a
specific kind of mistake to you?

Sorry, it doesn't.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top