Skip to content

shared_buffer

shared_buffer is the framework's universal data container for binary payloads. It is used across cache, TCP, WebSocket, and event systems. shared_buffer provides a single, reference-counted type that all these systems can use interchangeably — avoiding unnecessary copies and ensuring consistent memory management.

It wraps either a std::vector<uint8_t> or a FlatBuffers DetachedBuffer in a copy-on-write shared pointer.

Why shared_buffer?

Throughout the framework, binary data is passed between subsystems: cache stores key-value pairs, TCP sends raw bytes, WebSocket transmits FlatBuffers frames, and events carry payloads. Without a common type, each subsystem would need its own buffer management, leading to unnecessary copies and inconsistent APIs. shared_buffer solves this by providing a single type that:

  • Avoids copies — Multiple references share the same underlying data.
  • Supports FlatBuffers natively — DetachedBuffer can be moved directly into a shared_buffer.
  • Integrates with Boost.Asioas_asio() converts to asio::const_buffer for I/O operations.

Factory Methods

shared_buffer::copy(data, size) — From raw bytes

cpp
const char* raw = "hello";
auto buf = shared_buffer::copy(raw, 5);

shared_buffer::copy(str) — From string

cpp
auto buf = shared_buffer::copy(std::string("hello"));

shared_buffer::make(vec) — From vector (move)

cpp
std::vector<uint8_t> vec = {1, 2, 3, 4};
auto buf = shared_buffer::make(std::move(vec));

shared_buffer::make(buf) — From FlatBuffers

cpp
flatbuffers::DetachedBuffer fb_buf = builder.Release();
auto buf = shared_buffer::make(std::move(fb_buf));

Accessors

MethodReturn TypeDescription
data()const uint8_t*Raw pointer to buffer data
size()size_tBuffer size in bytes
empty()boolTrue if buffer is empty
to_string()std::stringCopy contents to string
as_asio()asio::const_bufferConvert for Boost.Asio operations
operator bool()boolTrue if buffer is non-empty
operator==(other)boolByte-wise equality comparison

Implicit Conversions

cpp
// From shared_ptr<vector<uint8_t>>
auto vec = std::make_shared<std::vector<uint8_t>>(std::initializer_list<uint8_t>{1,2,3});
shared_buffer buf(vec);

// From shared_ptr<DetachedBuffer>
auto detach = std::make_shared<flatbuffers::DetachedBuffer>(builder.Release());
shared_buffer buf(detach);

Next: Configuration.