Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set the badbit of a stream by a customized streambuf

Tags:

c++

stream

std

I've a customized std::streambuf. Something like:

class mybuf : public std::streambuf {
  ...
  virtual std::streambuf::int_type underflow() {
    if(eof) {
     return traits_type::eof();
    }
    ...
  }
  ...
  std::istream stream(new mybuf(...));

Setting the eof flag of the stream is done by returning eof on the underflow() function.

How can I set the badbit flag of the stream? Should the overflow() function throw an exception?

like image 269
powerpete Avatar asked Oct 28 '22 11:10

powerpete


1 Answers

Throwing an exception is indeed the way to go.

Refer to the reference page for std::ios_base::iostate, and specifically this :

The badbit is set by the following standard library functions:

  • ...
  • Every stream I/O function if an exception is thrown by any member function of the associated stream buffer (e.g. sbumpc(), xsputn(), sgetc(), overflow(), etc)
  • ...
like image 145
Sander De Dycker Avatar answered Nov 15 '22 06:11

Sander De Dycker