Buffers
This page introduces the buffer types that represent contiguous memory regions.
Code snippets assume using namespace boost::capy; is in effect.
|
What are Buffers?
A buffer is a view into a contiguous region of memory. Capy provides two fundamental buffer types:
| Type | Description |
|---|---|
|
A view into read-only memory |
|
A view into writable memory |
Buffers do not own memory. They are lightweight handles (pointer + size) that refer to memory owned elsewhere.
const_buffer
A const_buffer represents a contiguous region of read-only bytes:
char const data[] = "Hello, World!";
const_buffer buf(data, sizeof(data) - 1);
void const* p = buf.data(); // Pointer to first byte
std::size_t n = buf.size(); // Number of bytes
mutable_buffer
A mutable_buffer represents a contiguous region of writable bytes:
char data[1024];
mutable_buffer buf(data, sizeof(data));
void* p = buf.data(); // Pointer to first byte
std::size_t n = buf.size(); // Number of bytes
When to Use Each
Use const_buffer when:
-
You need to read data without modifying it
-
Passing data to functions that only read
-
Creating views of string literals or constant data
Use mutable_buffer when:
-
You need to write data into the buffer
-
Receiving data from I/O operations
-
The underlying memory must be modified
Summary
| Type | Purpose |
|---|---|
|
Read-only view of contiguous bytes |
|
Writable view of contiguous bytes |
Next Steps
-
Buffer Sequences — Work with multiple buffers
-
Dynamic Buffers — Growable buffer containers