...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 reliably transferred as an ordered
sequence of bytes. 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 networking types tcp::socket
and net::ssl::stream
support both SyncStream
and AsyncStream.
All stream algorithms in Beast are declared as template functions using these
concepts:
Table 1.4. 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.5. Type Traits and Metafunctions
Name |
Description |
---|---|
An alias for the type of object returned by |
|
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 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 type requirements not met"); net::write(stream, net::const_buffer(s.data(), s.size())); }