Doubts about in_avail vs showmanyc

Nathan Myers ncm-nospam@cantrip.org
Wed Mar 5 05:09:00 GMT 2003


Paolo,

By the way,

On Tue, Mar 04, 2003 at 09:34:16PM +0100, Paolo Carlini wrote:
>     streamsize
>     in_avail()
>     {
>       streamsize __ret;
>       if (_M_in_cur && _M_in_cur < _M_in_end)
>         {
>           if (_M_pback_init)
>             {
>               size_t __save_len =  _M_pback_end_save - _M_pback_cur_save;
>               size_t __pback_len = _M_in_cur - _M_pback;
>               __ret = __save_len - __pback_len;
>             }
>           else
>             __ret = this->egptr() - this->gptr();
>         }
>       else
>         __ret = this->showmanyc();
>       return __ret;
>     }

This should end up as:

>     inline streamsize
>     in_avail()
>     {
>       streamsize __ret = this->egptr() - this->gptr();
>       return (__ret != 0) ? __ret : this->showmanyc();
>     }

In other words, in_avail() doesn't care if there's anything in some
putback cell; if there is, that's what's available without a call to
underflow(), and that's what this->gptr() points to, so that's all it
reports.  In general, you can cut and _cut_ and *cut* the members of
basic_streambuf<> until they are really easy to understand (and fast).
Put all complexity into the virtuals of the derived classes where 
speed is less important, because time spent there is amortized over
many characters.  

Note that there's no need to test for null pointers because if
one is 0, the others are 0 too and the difference is 0.

Nathan Myers
ncm-nospam@cantrip.org



More information about the Libstdc++ mailing list