DynamicBuffer
A DynamicBuffer represents memory storage that may be automatically resized as required, where the memory is divided into an input sequence followed by an output sequence. These memory regions are internal to the dynamic buffer, but direct access to the elements is provided to permit them to be efficiently used with I/O operations, such as the send or receive operations of a socket. Data written to the output sequence of a dynamic buffer object is appended to the input sequence of the same object.
Requirements
-
Ddenotes a dynamic buffer class. -
adenotes a value of typeD. -
cdenotes a (possibly const) value of typeD. -
ndenotes a value of typestd::size_t. -
Tdenotes a type meeting the requirements for ConstBufferSequence. -
Udenotes a type meeting the requirements for MutableBufferSequence.
| Expression | Type | Semantics, Pre/Post-conditions |
|---|---|---|
|
|
This type represents the memory associated with the input sequence. |
|
|
This type represents the memory associated with the output sequence. |
|
|
Returns the size, in bytes, of the input sequence. |
|
|
Returns the permitted maximum of the sum of the sizes of the input sequence and output sequence. |
|
|
Returns the maximum sum of the sizes of the input sequence and output sequence that the dynamic buffer can hold without requiring reallocation. |
|
|
Returns a constant buffer sequence |
|
|
Returns a mutable buffer sequence u representing the output sequence, and where
Throws: |
|
Appends |
|
|
Removes |
Example
#include <boost/buffers.hpp>
using namespace boost::buffers;
// Generic function that works with any DynamicBuffer
template<class DynamicBuffer>
void append_greeting(DynamicBuffer& db)
{
char const* msg = "Hello!";
std::size_t len = 6;
// Prepare space in the output sequence
auto dest = db.prepare(len);
// Copy data into the prepared space
copy(dest, const_buffer(msg, len));
// Move data from output to input sequence
db.commit(len);
}
int main()
{
// Works with circular_buffer
char storage1[64];
circular_buffer cb(storage1, sizeof(storage1));
append_greeting(cb);
// Works with flat_buffer
char storage2[64];
flat_buffer fb(storage2, sizeof(storage2));
append_greeting(fb);
// Works with string_buffer
std::string str;
string_buffer sb(&str);
append_greeting(sb);
}
See Also
-
Dynamic Buffers - Detailed guide to dynamic buffer types
-
ConstBufferSequence - Input sequence requirements
-
MutableBufferSequence - Output sequence requirements