Class BufferSerializer

Inheritance Relationships

Base Type

Derived Type

Class Documentation

class BufferSerializer : public gpa::serialization::MemoryBackedSerializer

Serializes/deserializes datas to/from a buffer.

For now the serializer has primary ownership of the backing buffer - this may change later if we decide to have it receive storage from another module.

Subclassed by gpa::serialization::PersistentResizableBufferSerializer

Public Types

enum [anonymous]

Values:

enumerator kDefaultCapacity
typedef std::function<bool(uint8_t *&userBuffer, size_t &capacity, size_t const minSize)> OnReallocateCallback

On input, gives the current buffer pointer and capacity. On output, if the user wants to use their own resized buffer, must return the new buffer pointer and new capacity.

Return

A true return value indicates that a resize has occurred.

Public Functions

BufferSerializer(size_t const initialCapacity = kDefaultCapacity)
BufferSerializer(uint8_t *buffer, size_t const capacity, OnReallocateCallback const &callback, size_t const storageOffset = 0)

Construct that takes in a buffer from the caller and uses it for reading/writing.

If the serializer runs out of space for writing, the user-given callback will be called (

Parameters

storageOffset -- Offset from the start of larger memory region, this buffer serializer is part of. Used for calculating proper value returned by @BufferSerializerGetAbsolutePosition

virtual ~BufferSerializer()
virtual size_t WriteRawData(void const *src, size_t size) final override

If the buffer is too small to accomodate the data, the underlying buffer is resized and writing continues as normal. Default behavior is a memcpy of the data.

virtual size_t ReadRawData(void *dst, size_t size) final override

virtual bool EnsureWriteCapacity(size_t const size) final override

virtual bool CheckAvailableData(size_t const size) const final override

virtual void Clear() final override

Does not affect capacity.

size_t GetSize(void) const

Returns the size of the buffer in bytes.

Size is the number of bytes that has been written to the buffer, not the total amount capacity.

size_t GetCapacity() const

Returns the capacity of the buffer in bytes.

Capacity is the number of bytes the buffer contains in total, not the amount written.

void Resize(size_t newSize)

Updates the buffer's size to a given number of bytes.

Can be used to "reserve" an amount of space ahead of time without writing directly. If the underlying capacity is not large enough, a reallocation is performed. The read/write position is set to the end of the newly allocated size. Reserved space may be larger than requested.

virtual bool Seek(int64_t offset, SeekOrigin origin = SeekOrigin::kBegin) final override

Seeks to the offset in the buffer in bytes.

The offset is based on the given seek origin. A positive number is forward, a negative is backward. If the requested offset is out-of-bounds, the current position is unchanged.

Returns

true on success and the current position is changed.

virtual size_t GetPosition() const final override

Returns the offset the current read/write cursor is at.

virtual uint64_t GetAbsolutePosition() const final override

virtual void *Get(uint64_t offset) const final override

uint8_t const *GetBuffer() const

Returns a pointer to the start of the buffer.