class Core::IO::Buffer

Overview

Buffer class managing a heap memory block. More…

#include <corebuffer.h>

class Buffer
{
public:
    // construction

    Buffer (void* buffer = nullptr, uint32 size = 0, bool copy = true);
    Buffer (uint32 size, bool initWithZero = true);

    // methods

    Buffer& take (Buffer& buffer);
    void* getAddress ();
    const void* getAddress () const;
    void* getAddressAligned ();
    const void* getAddressAligned () const;
    bool isNull () const;
    uint32 getSize () const;
    uint32 getAlignment () const;
    bool resize (uint32 newSize);
    void setAlignment (uint32 alignment);
    bool setValidSize (uint32 newSize);
    void zeroFill ();
    void byteFill (uint8 value);
    uint32 copyFrom (const void* src, uint32 srcSize);
    uint32 copyFrom (uint32 dstOffset, const void* src, uint32 srcSize);
    uint32 copyTo (void* dst, uint32 dstSize) const;

    template  <class T>
    T* as ();

    template  <class T>
    const T* as () const;

    operator void * ();
    operator char * ();
    operator const void * () const;
    operator const char * () const;
    bool operator == (const Buffer& buffer) const;
};

Detailed Documentation

Buffer class managing a heap memory block.

Construction

Buffer (void* buffer = nullptr, uint32 size = 0, bool copy = true)

[HEAVY/LIGHT] Construct buffer with existing data.

Copies or just wraps the data.

Buffer (uint32 size, bool initWithZero = true)

[HEAVY] Construct buffer with given size.

Always allocates a new heap block.

Methods

Buffer& take (Buffer& buffer)

Take memory ownership from other buffer.

void* getAddress ()

Get memory address (writable).

const void* getAddress () const

Get memory address (read-only).

void* getAddressAligned ()

Get aligned memory address (writable).

const void* getAddressAligned () const

Get aligned memory address (read-only).

bool isNull () const

Check if memory address is null, i.e.

nothing allocated.

uint32 getSize () const

Get size of allocated memory.

uint32 getAlignment () const

Get memory alignment.

bool resize (uint32 newSize)

Resize buffer to new size.

Returns false when (re-)allocation fails.

void setAlignment (uint32 alignment)

Set memory alignment, must be power of two.

bool setValidSize (uint32 newSize)

Manipulate valid size without reallocation (must be <= current size).

void zeroFill ()

Fill memory with zeros.

void byteFill (uint8 value)

Fill memory with byte value.

uint32 copyFrom (const void* src, uint32 srcSize)

Copy data from source to internal memory.

uint32 copyFrom (uint32 dstOffset, const void* src, uint32 srcSize)

Copy data from source to internal memory at given offset.

uint32 copyTo (void* dst, uint32 dstSize) const

Copy internal data to destination buffer.

template  <class T>
T* as ()

Return typed memory address (writable).

template  <class T>
const T* as () const

Return typed memory address (read-only).