Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

Streams

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

SyncReadStream

Supports buffer-oriented blocking reads.

SyncWriteStream

Supports buffer-oriented blocking writes.

SyncStream

A stream supporting buffer-oriented blocking reads and writes.

AsyncReadStream

Supports buffer-oriented asynchronous reads.

AsyncWriteStream

Supports buffer-oriented asynchronous writes.

AsyncStream

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

executor_type

An alias for the type of object returned by get_executor.

has_get_executor

Determine if the get_executor member function is present.

is_async_read_stream

Determine if a type meets the requirements of AsyncReadStream.

is_async_stream

Determine if a type meets the requirements of both AsyncReadStream and AsyncWriteStream.

is_async_write_stream

Determine if a type meets the requirements of AsyncWriteStream.

is_sync_read_stream

Determine if a type meets the requirements of SyncReadStream.

is_sync_stream

Determine if a type meets the requirements of both SyncReadStream and SyncWriteStream.

is_sync_write_stream

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()));
}

PrevUpHomeNext