MIP_SDK  v3.0.0
MicroStrain Communications Library for embedded systems
Public Member Functions | List of all members
microstrain::SerializerBase Class Reference

Represents a view of a buffer of bytes of known capacity. More...

#include <serializer.hpp>

Inheritance diagram for microstrain::SerializerBase:
microstrain::Serializer< E > mip::PacketView::AllocatedField

Public Member Functions

 SerializerBase ()=default
 
 SerializerBase (uint8_t *ptr, size_t capacity, size_t offset=0)
 
 SerializerBase (const uint8_t *ptr, size_t size, size_t offset=0)
 
 SerializerBase (microstrain::Span< const uint8_t > buffer, size_t offset=0)
 
uint8_t * pointer (size_t required_size)
 Obtains a pointer to the current offset for reading/writing a value of the specified size. This function does NOT advance the offset value. Generally, you should use getPtrAndAdvance() instead. More...
 
uint8_t * getPtrAndAdvance (size_t size)
 Obtains a pointer to the current offset for reading/writing a value of specified size, and post-increments the offset by that size. Use this function just like pointer(). More...
 
void invalidate ()
 Marks the buffer as invalid, i.e. overrun/error state. All further accesses via pointer(), getPtrAndAdvance(), etc. will fail. (basePointer() and capacity() remain valid) More...
 
size_t setOffset (size_t offset)
 Sets a new offset and returns the old value. This can be used to save/restore the current offset. Calling setOffset() after an overrun with an in-range (i.e. non-overrun) value restores the non-overrun status. More...
 

Detailed Description

Represents a view of a buffer of bytes of known capacity.

The class maintains 3 attributes:

This class works for either reading or writing data. As data is written/ read, the offset is incremented. Through functions such as pointer() and getPtrAndAdvance(), the next readable/writable location can be accessed. Both of these take a 'required_size' parameter indicating the amount of data intending to be read/written. If sufficient data/space remains, a valid pointer is returned. Otherwise, NULL is returned. The offset is incremented in getPtrAndAdvance() regardless. This means the offset can exceed the size/capacity of the buffer. This is an "overrun" condition, and it can be checked with the isOk(), isFinished(), or hasRemaining() functions. This behavior prevents bugs where an object is partially read/written in the case where a larger parameter (say u32) fails but then a smaller one (say u8) succeeds. With this class an overflowing access will cause every following access to fail. Note that "overrun" here does NOT mean that an out-of-bounds access has occurred.

Note
This class can be constructed with a const buffer, however constness is not enforced. It is up to the user to ensure const buffers are not written.

Constructor & Destructor Documentation

◆ SerializerBase() [1/4]

microstrain::SerializerBase::SerializerBase ( )
default

◆ SerializerBase() [2/4]

microstrain::SerializerBase::SerializerBase ( uint8_t *  ptr,
size_t  capacity,
size_t  offset = 0 
)
inline

◆ SerializerBase() [3/4]

microstrain::SerializerBase::SerializerBase ( const uint8_t *  ptr,
size_t  size,
size_t  offset = 0 
)
inline

◆ SerializerBase() [4/4]

microstrain::SerializerBase::SerializerBase ( microstrain::Span< const uint8_t >  buffer,
size_t  offset = 0 
)
inline

Member Function Documentation

◆ getPtrAndAdvance()

uint8_t* microstrain::SerializerBase::getPtrAndAdvance ( size_t  size)
inline

Obtains a pointer to the current offset for reading/writing a value of specified size, and post-increments the offset by that size. Use this function just like pointer().

Parameters
sizeHow many bytes will be read/written to the pointer. The offset is increased by this amount.
Returns
A valid pointer if size bytes are available. NULL otherwise. Example usage:
uint32_t value = 5;
if(uint8_t* ptr = serializer.getPointerAndAdvance(sizeof(value)))
std::memcpy(ptr, &value, sizeof(value));

◆ invalidate()

void microstrain::SerializerBase::invalidate ( )
inline

Marks the buffer as invalid, i.e. overrun/error state. All further accesses via pointer(), getPtrAndAdvance(), etc. will fail. (basePointer() and capacity() remain valid)

◆ pointer()

uint8_t* microstrain::SerializerBase::pointer ( size_t  required_size)
inline

Obtains a pointer to the current offset for reading/writing a value of the specified size. This function does NOT advance the offset value. Generally, you should use getPtrAndAdvance() instead.

Parameters
required_sizeHow many bytes will be read/written to the pointer.
Returns
A valid pointer if required_size bytes are available. NULL otherwise.

◆ setOffset()

size_t microstrain::SerializerBase::setOffset ( size_t  offset)
inline

Sets a new offset and returns the old value. This can be used to save/restore the current offset. Calling setOffset() after an overrun with an in-range (i.e. non-overrun) value restores the non-overrun status.

Parameters
offsetNew offset. Allowed to exceed the size, i.e. an overrun state.
Returns
The old offset value.

The documentation for this class was generated from the following file: