While working on a network library I recently noticed that having a pendant to basic_streambuf::in_avail in conjunction with a writesome function would be pretty handy for asynchronous I/O.
I have searched the web and checked several C++ references if there is any function which meets these demands, but it seems I had no luck. The only source which mentions similar functionality is Boost's Asio library, however the description clearly states that the function call will block until at least one byte has been sent which does not reflect my desired behaviour.
To elaborate on my question, I created a rough draft based on the C++ N3337 publication.
27.6.3.2.5 Put area [streambuf.pub.put]
streamsize in_depart();
Returns: If a write position is available, returns
epptr() - pptr()
. Otherwise returnsshowmanycp()
.27.6.3.4.5 Put area [streambuf.virt.put]
streamsize showmanycp();
Returns: An estimate of the number of characters which can be written to the sequence, or
-1
. If it returns a positive value, then successive calls tooverflow()
will not returntraits::eof()
until at least that number of characters have been written to the stream. Ifshowmanycp()
returns-1
, then calls tooverflow()
will fail.Default behavior: Returns zero.
Remarks: Uses
traits::eof()
.27.7.3.7 Unformatted output functions [ostream.unformatted]
streamsize writesome(char_type* s, streamsize n);
Effects: Behaves as an unformatted output function (as described in 27.7.3.7, paragraph 1). After constructing a sentry object, if
!good()
callssetstate(failbit)
which may throw an exception, and return. Otherwise writesn
characters designated bys
. Ifrdbuf()->in_depart() == -1
, callssetstate(badbit)
(which may throwios_base::failure
(27.5.5.4)), and writes no characters;— If
rdbuf()->in_depart() == 0
, writes no characters.— If
rdbuf()->in_depart() > 0
, writesmin(rdbuf()->in_depart(), n))
characters.Returns: The number of characters written.