omni::structuredlog::BlobWriter

Defined in omni/structuredlog/BinarySerializer.h

template<bool validate = false, OnBlobWriterValidationErrorFunc onValidationError = ignoreBlobWriterValidationError>
class BlobWriter

A class to build a binary blob.

The binary blob only has internal markers for variable length fields; to decode the binary blob, you will need some sort of external schema or fixed layout to decode the binary blob.

Param validate

If this parameter is true, then the length of the blob will be tracked while writing the blob and attempting to write past the end of the buffer will cause methods to fail. This is needed for the tests.

Param onValidationError

This is a callback that gets executed when a validation error is triggered, so logging can be called. Logging cannot be called directly from this class because it is used inside the logging system.

Public Functions

inline BlobWriter(void *buffer, size_t bytes)

Constructor.

Parameters
  • buffer[in] The buffer to write into. This buffer must be aligned to sizeof(void*).

  • bytes[in] The length of buffer.

template<typename T>
inline bool copy(T v)

Copy a primitive type element into the buffer.

Parameters

v[in] The value to copy into the buffer.

Returns

true if the value was successfully written.

Returns

false if the blob ran out of buffer space.

inline bool copy(const char *const *v, const uint16_t *stringLengths, uint16_t len)

Copy an array of strings into the buffer with pre-calculated lengths.

Note

This overload exists to avoid having to perform a strlen() on each string in the array twice (once when calculating the buffer size and once when writing the buffer).

Parameters
  • v[in] The array of strings to write into the buffer. This may be nullptr if len is 0. The elements of this array may be nullptr; each element of this must be a null terminated string otherwise.

  • stringLengths[in] The length of each string in v. These length must include the null terminator of the string. This may be nullptr if len is 0.

  • len[in] The number of elements in array v and array stringLengths.

Returns

true if the value was successfully written.

Returns

false if the blob ran out of buffer space.

inline bool copy(const char *const *v, uint16_t len)

Copy an array of strings into the buffer.

Parameters
  • v[in] The array of strings to write into the buffer. This may be nullptr if len is 0. The elements of this array may be nullptr; each element of this must be a null terminated string otherwise.

  • len[in] The number of elements in array v.

Returns

true if the value was successfully written.

Returns

false if the blob ran out of buffer space.

template<typename T>
inline bool copy(T *v, uint16_t len)

Copy an array of data into the buffer.

Parameters
  • v[in] The array of values to copy. This may also be a string. This may be nullptr if len is 0.

  • len[in] The number of elements in array v. If this is a string, this length includes the null terminator.

Returns

true if the value was successfully written.

Returns

false if the blob ran out of buffer space.

inline bool copy(const StringView &v)

Copy a StringView into the blob.

Parameters

v[in] The StringView to copy in.

Returns

true if the value was successfully written.

Returns

false if the blob ran out of buffer space.

inline bool copy(StringView &v)

Copy a StringView into the buffer.

Parameters

v[in] The string view to copy.

template<typename T>
inline bool copy(T *v, uint16_t actualLen, uint16_t fixedLen)

Copy an array of data into the buffer.

Parameters
  • v[in] The array of values to copy. This may also be a string. This may not be nullptr because that would imply fixedLen is 0, which is not a valid use case.

  • actualLen[in] The length of array v. This must be less than or equal to fixedLen.

  • fixedLen[in] The fixed length as specified by your data schema. If this is greater than actualLen, then the excess size at the end of the array will be zeroed.

Returns

true if the value was successfully written.

Returns

false if the blob ran out of buffer space.

template<typename T>
inline void alignBuffer()

Align the buffer for a given type.

Remark

This will advance the buffer so that the next write is aligned for the specified type.

Public Static Attributes

static constexpr uint32_t kVersion = 0

The version of binary blob ABI Headers that use these binary blobs should static assert on its version.

Do not modify the layout of the binary blob without incrementing this.