pabigot  0.1.1
C++ support classes
Public Types | Public Member Functions | Protected Attributes | Friends
pabigot::byteorder::octets_helper Class Reference

Infrastructure to fill an octet buffer with data. More...

#include </mnt/devel/pabigot-cxx/include/pabigot/byteorder.hpp>

Inheritance diagram for pabigot::byteorder::octets_helper:
pabigot::ble::gap::adv_data

Public Types

using size_type = std::size_t
 Type used for span values.
 

Public Member Functions

 octets_helper (uint8_t *begin, uint8_t *end)
 Reference an octet range into which data will be written. More...
 
 octets_helper (uint8_t *begin, size_type count)
 Reference an octet sequence into which data will be written. More...
 
void reset () noexcept
 Remove all content from the buffer. More...
 
const bool valid () const noexcept
 Indicates whether advance() caused an error. More...
 
void invalidate () noexcept
 Explicitly mark the buffer invalid. More...
 
const void * begin () const noexcept
 Get a pointer to the start of buffer. More...
 
const void * end () const noexcept
 Get a pointer to the end of the filled part of the buffer. More...
 
const size_type size () const noexcept
 Number of octets stored in the buffer. More...
 
const size_type available () const noexcept
 Number of unused octets available in the buffer. More...
 
const size_type max_size () const noexcept
 Maximum number of octets supported by the buffer.
 
void * advance (size_type s) noexcept
 Allocate a region and return a pointer to it or a nullptr if the advance went too far. More...
 
bool can_advance (size_type s) const noexcept
 Indicate whether advance() would succeed for a given span. More...
 
bool append (const void *sp, size_type span) noexcept
 Append a value to the buffer. More...
 
template<typename T >
bool append (const T &value) noexcept
 Append a value to the buffer. More...
 
template<typename T >
bool append_be (const T &value) noexcept
 As with append() but stores the value converted to big-endian byte order.
 
template<typename T >
bool append_le (const T &value) noexcept
 As with append() but stores the value converted to little-endian byte order.
 

Protected Attributes

uint8_t *const begin_
 
uint8_t *const end_
 
uint8_t * bp_ = nullptr
 

Friends

class oob_helper
 

Detailed Description

Infrastructure to fill an octet buffer with data.

This allows safe invocation for as much data is desired, allowing overrun detection at the end rather than on each addition.

Note
This structure references but does not own the memory of the buffer that it manages.

Constructor & Destructor Documentation

◆ octets_helper() [1/2]

pabigot::byteorder::octets_helper::octets_helper ( uint8_t *  begin,
uint8_t *  end 
)
inline

Reference an octet range into which data will be written.

Parameters
beginpointer to the first octet in the sequence.
endpointer just past the last octet available for the sequence.

◆ octets_helper() [2/2]

pabigot::byteorder::octets_helper::octets_helper ( uint8_t *  begin,
size_type  count 
)
inline

Reference an octet sequence into which data will be written.

Parameters
beginpointer to the first octet in the sequence.
countthe number of octets in the sequence.

Member Function Documentation

◆ advance()

void* pabigot::byteorder::octets_helper::advance ( size_type  s)
inlinenoexcept

Allocate a region and return a pointer to it or a nullptr if the advance went too far.

Parameters
sthe number of octets required.
Returns
a pointer into the allocated buffer that allows writing s octets. The pointer is null (and valid() will now return false) if the buffer had already become invalid, or did so as a result of advancing by s.

◆ append() [1/2]

template<typename T >
bool pabigot::byteorder::octets_helper::append ( const T &  value)
inlinenoexcept

Append a value to the buffer.

The byte-order of the value is native. If a non-native order is required use append_le() or append_be(), or use host_x_le() or host_x_be() on the argument.

Template Parameters
Tthe underlying type of the value.
Parameters
valuethe value to append.
Returns
as with advance()

◆ append() [2/2]

bool pabigot::byteorder::octets_helper::append ( const void *  sp,
size_type  span 
)
inlinenoexcept

Append a value to the buffer.

This invokes advance() and if the space reservation was successful stores the range.

Parameters
sppointer to the value to be appended.
spanthe number of octets to be appended.
Returns
as with advance()

◆ available()

const size_type pabigot::byteorder::octets_helper::available ( ) const
inlinenoexcept

Number of unused octets available in the buffer.

Note
The returned size is zero if not valid.

◆ begin()

const void* pabigot::byteorder::octets_helper::begin ( ) const
inlinenoexcept

Get a pointer to the start of buffer.

Returns
as described, but a null pointer if not valid.

◆ can_advance()

bool pabigot::byteorder::octets_helper::can_advance ( size_type  s) const
inlinenoexcept

Indicate whether advance() would succeed for a given span.

Parameters
sas with advance()
Returns
true if the buffer is valid and has sufficient space for s octets, otherwise false. A false return does not invalidate a valid buffer.

◆ end()

const void* pabigot::byteorder::octets_helper::end ( ) const
inlinenoexcept

Get a pointer to the end of the filled part of the buffer.

I.e. the span from [begin(), end()) has filled content.

Returns
as described, but a null pointer if not valid.

◆ invalidate()

void pabigot::byteorder::octets_helper::invalidate ( )
inlinenoexcept

Explicitly mark the buffer invalid.

This might be used by subclasses that use a more piece-wise conception of whether an allocation will fit.

◆ reset()

void pabigot::byteorder::octets_helper::reset ( )
inlinenoexcept

Remove all content from the buffer.

After this valid() will be true, the buffer will be zeroed, and the next write will begin at the buffer start.

◆ size()

const size_type pabigot::byteorder::octets_helper::size ( ) const
inlinenoexcept

Number of octets stored in the buffer.

Note
The returned size is zero if not valid.

◆ valid()

const bool pabigot::byteorder::octets_helper::valid ( ) const
inlinenoexcept

Indicates whether advance() caused an error.

Returns
false iff advance() was invoked to reserve more space than was available or invalidate() was called since the last reset().

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