I am trying to make sense of the code posted by @dietmar-kühl at:
I do not understand how I am supposed to handle error from within underflow. I have checked the documentation without much luck:
Here is a pseudo code:
class customstreambuf : public std::streambuf
{
    socketstream* stream_;
public:
    explicit customstreambuf(socketstream* stream) : stream_(stream)
    {
    }
    int underflow() override
    {
        if (this->gptr() == this->egptr())
        {
            // fills internal buffer, return num bytes read or -1 on error:
            const int size = this->stream_->fillbuffer();
            if (size < 0)
            {
                throw std::runtime_error("what should I do here?");
            }
            this->setg(this->stream_->buffer_, this->stream_->buffer_, this->stream_->buffer_ + size);
        }
        return this->gptr() == this->egptr()
                   ? std::char_traits<char>::eof()
                   : std::char_traits<char>::to_int_type(*this->gptr());
    }
};
Other posts are relatively evasive for error conditions:
How should I handle error in std::streambuf subclass ? throw an exception (which type) or do something else ?
Just discovered the following awkward post:
which seems kind of mixed java/c++ code (what's IOException ?)
I also found the following:
Which also gives a relative elusive Throwing an exception is indeed the way to go.. Would it make sense to throw something like:
if (size < 0)
{
    throw "socket stream error";
}
Aucun commentaire:
Enregistrer un commentaire