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
-
Ddenotes a data source class. -
cdenotes a (possibly const) value of typeD. -
Tdenotes a type meeting the requirements for ConstBufferSequence.
| Expression | Type | Semantics, Pre/Post-conditions |
|---|---|---|
|
Data sources must be nothrow move constructible. |
|
|
|
Returns a constant buffer sequence representing the data. This function must
be |
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 |
Streaming via |
Memory |
Data must be in memory |
Data can be generated or streamed |
Multiple reads |
Implicit (buffer sequence can be iterated multiple times) |
Requires |
Size |
Implicit (via |
Optional |
See Also
-
any_source- a type-erased wrapper that can hold either a DataSource or ReadSource -
ReadSource - a streaming data source concept