DataSource

A DataSource represents a source of data that is available as a constant buffer sequence in memory. Unlike a ReadSource, which provides data through a streaming read interface, a data source exposes its entire contents directly through a data() member function.

Data sources are useful for representing objects whose binary representation is already available in memory and can be accessed without copying, such as strings, byte arrays, or memory-mapped files.

Requirements

  • D denotes a data source class.

  • c denotes a (possibly const) value of type D.

  • T denotes a type meeting the requirements for ConstBufferSequence.

Expression Type Semantics, Pre/Post-conditions

D(D&&)

Data sources must be nothrow move constructible.

c.data()

T

Returns a constant buffer sequence representing the data. This function must be noexcept. The returned buffer sequence remains valid for at least as long as the data source object exists and is not modified.

Example

struct my_data_source
{
    std::string data_;

    explicit my_data_source(std::string s) noexcept
        : data_(std::move(s))
    {
    }

    // Move constructor required
    my_data_source(my_data_source&&) noexcept = default;

    const_buffer data() const noexcept
    {
        return const_buffer(data_.data(), data_.size());
    }
};

static_assert(is_data_source<my_data_source>::value, "");

Comparison with ReadSource

Feature DataSource ReadSource

Data access

Direct via data() returning buffer sequence

Streaming via read() into caller-provided buffers

Memory

Data must be in memory

Data can be generated or streamed

Multiple reads

Implicit (buffer sequence can be iterated multiple times)

Requires rewind() if available

Size

Implicit (via buffers::size(data()))

Optional size() member

See Also

  • any_source - a type-erased wrapper that can hold either a DataSource or ReadSource

  • ReadSource - a streaming data source concept