...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
A Stream
is a communication channel where data is transferred as an ordered sequence
of octet buffers. Streams are either synchronous or asynchronous, and may
allow reading, writing, or both. Note that a particular type may model more
than one concept. For example, the Asio types boost::asio::ip::tcp::socket
and boost::asio::ssl::stream
support both SyncStream and AsyncStream. All stream algorithms in Beast
are declared as template functions using these concepts:
Table 1.2. Stream Concepts
Concept |
Description |
---|---|
Supports buffer-oriented blocking reads. |
|
Supports buffer-oriented blocking writes. |
|
A stream supporting buffer-oriented blocking reads and writes. |
|
Supports buffer-oriented asynchronous reads. |
|
Supports buffer-oriented asynchronous writes. |
|
A stream supporting buffer-oriented asynchronous reads and writes. |
These template metafunctions check whether a given type meets the requirements for the various stream concepts, and some additional useful utilities. The library uses these type checks internally and also provides them as public interfaces so users may use the same techniques to augment their own code. The use of these type checks helps provide more concise errors during compilation:
Table 1.3. Stream Type Checks
Name |
Description |
---|---|
Returns |
|
Determine if the |
|
Determine if a type meets the requirements of AsyncReadStream. |
|
Determine if a type meets the requirements of both AsyncReadStream and AsyncWriteStream. |
|
Determine if a type meets the requirements of AsyncWriteStream. |
|
Determine if a type meets the requirements of CompletionHandler, and is callable with a specified signature. |
|
Determine if a type meets the requirements of SyncReadStream. |
|
Determine if a type meets the requirements of both SyncReadStream and SyncWriteStream. |
|
Determine if a type meets the requirements of SyncWriteStream. |
Using the type checks with static_assert
on function or class template types will provide users with helpful error
messages and prevent undefined behaviors. This example shows how a template
function which writes to a synchronous stream may check its argument:
template<class SyncWriteStream> void write_string(SyncWriteStream& stream, string_view s) { static_assert(is_sync_write_stream<SyncWriteStream>::value, "SyncWriteStream requirements not met"); boost::asio::write(stream, boost::asio::const_buffer(s.data(), s.size())); }