Buffer

A Buffer object owns a single block of memory allocated on the heap. The memory block is freed when the Buffer is destroyed.

Buffer is implicitly convertible to BufferView and ConstBufferView and can be passed as an argument to any function that expects one of those types.

Buffer provides a resize() function, but resizing is a expensive operation that potentially involves copying the entire memory block, so it's not recommended to call it too many times. If you wish to create a Buffer whose size is not known ahead of time, consider creating a MemOutStream instead, then convert to Buffer at the final step by calling MemOutStream::moveToBuffer().

Header File

#include <ply-runtime/container/Buffer.h>

Also included from <ply-runtime/Base.h>.

Data Members

u8* bytes [code]

Pointer to the first byte.

u32 numBytes [code]

The number of bytes allocated.

Member Functions

Buffer() [code]

Constructs an empty Buffer.

Buffer(ConstBufferView other) [code]

Constructs a copy of other. A new memory block is allocated on the heap and the contents of other are copied into it.

Buffer(Buffer&& other) [code]

Move constructor. other is reset to an empty Buffer.

void operator=(ConstBufferView other) [code]
void operator=(const Buffer& other) [code]

Copy assignment operator. If this Buffer already owns a memory block, it is destroyed. A new memory block is allocated on the heap and the contents of other are copied into it.

void operator=(Buffer&& other) [code]

Move assignment operator. If this Buffer already owns a memory block, it is destroyed. other is reset to an empty state.

static Buffer allocate(u32 numBytes) [code]

Returns a Buffer that owns a newly allocated block of memory. The contents of the memory block are uninitialized.

static Buffer adopt(void* bytes, u32 numBytes) [code]

Returns a Buffer that takes ownership of the memory block at bytes. This memory block must have been allocated on the heap.

static Buffer fromChunks(ChunkCursor&& start) [code]
static Buffer fromChunks(ChunkCursor&& start, const ChunkCursor& end) [code]

Returns a Buffer that owns a newly allocated block of memory containing a copy of the data contained in some (possible multiple) chunks. The first form of fromChunks copies all the data from start up to the end of the chunk list. The second form copies all the data between start and end.

The first argument must be an rvalue reference to a ChunkCursor which might be moved from (reset to an empty state) as a result of this call. This requirement enables an optimization: If start is the sole reference to the start of a single ChunkListNode, no data is copied; instead, the chunk's memory block is truncated and ownership is passed directly to the Buffer. To satisfy this requirement, either use std::move() or pass a temporary copy of a ChunkCursor.

This function is used internally by MemOutStream::moveToBuffer().

operator const ConstBufferView&() const [code]
operator const BufferView&() [code]

Conversion operators. Makes Buffer implicitly convertible to BufferView and ConstBufferView.

const ConstBufferView& view() const [code]
const BufferView& view() [code]

Explicitly create a BufferView or ConstBufferView to the owned memory block.

explicit operator bool() const [code]

Explicit conversion to bool. Returns true if the length of the memory block is greater than 0. Allows you to use a Buffer in an if condition:

if (bin) {
    ...
}
bool isEmpty() const [code]

Returns true if the length of the memory block is 0.

void resize(u32 numBytes) [code]

Resize the owned memory block. If numBytes is greater than the current size, new space added to the memory block is left uninitialized.

u8* end() const [code]

Returns bytes + numBytes. This pointer is considered to point to the first byte after the memory block.

bool operator==(ConstBufferView other) const [code]

Tests whether the owned memory block exactly matches the contents of other.

void* release() [code]

Returns a pointer to the owned memory block while releasing ownership. The Buffer is reset to an empty state.

void clear() [code]

If this Buffer owns a memory block, it is destroyed and the Buffer is reset to an empty state.

template <typename Hasher>
void appendTo(Hasher& hasher) const [code]

Feeds the contents of the memory block to a hash function.