Boost.Buffers
Boost.Buffers is a portable C++20 library providing containers and algorithms for describing contiguous buffers of arbitrary bytes.
What This Library Does
Buffers provides vocabulary types and algorithms for working with raw byte sequences, enabling interoperability between libraries that process unstructured data:
-
Buffer sequences - Zero-copy representations of scattered memory regions, compatible with Asio’s buffer concepts
-
Dynamic buffers - Resizable containers for streaming data with prepare/commit/consume semantics
-
Type-erased wrappers - Runtime polymorphism for buffer sequences and data sources without templates
-
Algorithms - Functions for copying, slicing, and measuring buffer sequences
What This Library Does Not Do
Buffers is not an I/O library. It does not include:
-
Networking or socket operations (use Asio or Boost.Asio)
-
File I/O (use standard streams or platform APIs)
-
Allocating containers that own memory (buffers reference external storage)
-
Serialization or parsing (buffers are untyped byte sequences)
The library provides the vocabulary types that I/O and parsing libraries use to exchange data efficiently.
Design Philosophy
Asio-compatible concepts. Buffer sequences follow the same requirements as Boost.Asio, enabling direct interoperability with networking code.
Cheap copies. Buffer sequences are lightweight handles that reference external memory. Copying a buffer sequence does not copy the underlying data.
Scatter/gather I/O. Buffer sequences can represent multiple non-contiguous memory regions, supporting efficient vectored I/O operations.
Minimal dependencies. The library requires C++20 and works without exceptions. It does not depend on Asio or Boost.Asio.
Quick Example
#include <boost/buffers.hpp>
#include <iostream>
using namespace boost::buffers;
int main()
{
// Create a buffer from a string literal
char data[] = "Hello, Buffers!";
mutable_buffer buf(data, sizeof(data) - 1);
// Measure and iterate
std::cout << "Size: " << size(buf) << " bytes\n";
// Use with a dynamic buffer
char storage[64];
circular_buffer cb(storage, sizeof(storage));
auto dest = cb.prepare(buf.size());
copy(dest, buf);
cb.commit(buf.size());
std::cout << "Buffered: " << cb.size() << " bytes\n";
}
Next Steps
-
Introduction - Understand buffer concepts and terminology
-
Algorithms - Learn to copy, slice, and measure buffers
-
Dynamic Buffers - Work with resizable buffer containers
-
API Reference - Complete function and class documentation