MIP_SDK v2.0.0-141-g7a72ec3
MicroStrain Communications Library for embedded systems
Loading...
Searching...
No Matches
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)
 
size_t capacity () const
 Returns the total size of the buffer.
 
size_t offset () const
 Returns the current read or write offset.
 
size_t usedLength () const
 Returns the number of bytes read/written.
 
int remaining () const
 Returns the number of byte remaining (negative if overflowed).
 
bool isOverrun () const
 Returns true if offset has exceeded the size/capacity.
 
bool isOk () const
 Returns true if not overrun, i.e. !isOverrun().
 
bool isFinished () const
 Returns true if the entire buffer (and no more) has been read/written.
 
bool hasRemaining (size_t count=0) const
 Returns true if at least 'count' bytes remain unread/unwritten.
 
uint8_t * basePointer ()
 Returns a pointer to the start of the buffer.
 
const uint8_t * basePointer () const
 Returns a pointer to the start of the buffer.
 
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.
 
const uint8_t * pointer (size_t required_size) const
 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.
 
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().
 
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)
 
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.
 

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

◆ basePointer() [1/2]

uint8_t * microstrain::SerializerBase::basePointer ( )
inline

Returns a pointer to the start of the buffer.

◆ basePointer() [2/2]

const uint8_t * microstrain::SerializerBase::basePointer ( ) const
inline

Returns a pointer to the start of the buffer.

◆ capacity()

size_t microstrain::SerializerBase::capacity ( ) const
inline

Returns the total size of the buffer.

◆ 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));

◆ hasRemaining()

bool microstrain::SerializerBase::hasRemaining ( size_t  count = 0) const
inline

Returns true if at least 'count' bytes remain unread/unwritten.

◆ 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)

◆ isFinished()

bool microstrain::SerializerBase::isFinished ( ) const
inline

Returns true if the entire buffer (and no more) has been read/written.

◆ isOk()

bool microstrain::SerializerBase::isOk ( ) const
inline

Returns true if not overrun, i.e. !isOverrun().

◆ isOverrun()

bool microstrain::SerializerBase::isOverrun ( ) const
inline

Returns true if offset has exceeded the size/capacity.

◆ offset()

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

Returns the current read or write offset.

◆ pointer() [1/2]

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.

◆ pointer() [2/2]

const uint8_t * microstrain::SerializerBase::pointer ( size_t  required_size) const
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.

◆ remaining()

int microstrain::SerializerBase::remaining ( ) const
inline

Returns the number of byte remaining (negative if overflowed).

◆ 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.

◆ usedLength()

size_t microstrain::SerializerBase::usedLength ( ) const
inline

Returns the number of bytes read/written.


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