libdoip  0.1.0
DoIP (Diagnostics over Internet Protocol) ISO 13400 C++17 Library
doip::ByteArray Struct Reference

A dynamic array of bytes with utility methods for network protocol handling. More...

#include <ByteArray.h>

Inheritance diagram for doip::ByteArray:
Collaboration diagram for doip::ByteArray:

Public Member Functions

 ByteArray ()=default
 Default constructor - creates an empty ByteArray. More...
 
 ByteArray (const uint8_t *data, size_t size)
 Constructs a ByteArray from a raw byte array. More...
 
 ByteArray (const std::initializer_list< uint8_t > &init_list)
 
void writeU16At (size_t index, uint16_t value)
 Writes a 16-bit unsigned integer in big-endian format at a specific index. More...
 
void writeU16BE (uint16_t value)
 Appends a 16-bit unsigned integer in big-endian format to the end. More...
 
void writeU32At (size_t index, uint32_t value)
 Writes a 32-bit unsigned integer in big-endian format at a specific index. More...
 
void writeU32BE (uint32_t value)
 Appends a 32-bit unsigned integer in big-endian format to the end. More...
 
template<typename E >
void writeEnum (E value)
 
template<typename E >
void writeEnumAt (size_t index, E value)
 Writes an enum class value at a specific index as its underlying integral type. More...
 
uint16_t readU16BE (size_t index) const
 Reads a 16-bit unsigned integer in big-endian format from a specific index. More...
 
uint32_t readU32BE (size_t index) const
 Reads a 32-bit unsigned integer in big-endian format from a specific index. More...
 
template<typename E >
readEnum (size_t index) const
 

Detailed Description

A dynamic array of bytes with utility methods for network protocol handling.

ByteArray extends std::vector<uint8_t> with convenient methods for reading and writing multi-byte integer values in big-endian format (network byte order). This is commonly used in DoIP and other network protocols.

Note
All multi-byte read/write operations use big-endian (network) byte order

Definition at line 60 of file ByteArray.h.

Constructor & Destructor Documentation

◆ ByteArray() [1/3]

doip::ByteArray::ByteArray ( )
default

Default constructor - creates an empty ByteArray.

Examples
/home/runner/work/libdoip/libdoip/inc/ByteArray.h.

◆ ByteArray() [2/3]

doip::ByteArray::ByteArray ( const uint8_t *  data,
size_t  size 
)
inlineexplicit

Constructs a ByteArray from a raw byte array.

Parameters
dataPointer to the source byte array
sizeNumber of bytes to copy from the source array

Definition at line 72 of file ByteArray.h.

◆ ByteArray() [3/3]

doip::ByteArray::ByteArray ( const std::initializer_list< uint8_t > &  init_list)
inline

Definition at line 82 of file ByteArray.h.

Member Function Documentation

◆ readEnum()

template<typename E >
E doip::ByteArray::readEnum ( size_t  index) const
inline
Examples
/home/runner/work/libdoip/libdoip/inc/ByteArray.h.

Definition at line 265 of file ByteArray.h.

References readU16BE(), and readU32BE().

◆ readU16BE()

uint16_t doip::ByteArray::readU16BE ( size_t  index) const
inline

Reads a 16-bit unsigned integer in big-endian format from a specific index.

Reads 2 bytes starting at the specified index and interprets them as a 16-bit unsigned integer in big-endian (network) byte order.

Parameters
indexStarting index to read from (must be < size() - 1)
Returns
uint16_t The 16-bit value read from the array
Exceptions
std::out_of_rangeif index + 1 >= size()
Examples
/home/runner/work/libdoip/libdoip/inc/ByteArray.h.

Definition at line 224 of file ByteArray.h.

References doip::util::readU16BE().

Referenced by readEnum().

◆ readU32BE()

uint32_t doip::ByteArray::readU32BE ( size_t  index) const
inline

Reads a 32-bit unsigned integer in big-endian format from a specific index.

Reads 4 bytes starting at the specified index and interprets them as a 32-bit unsigned integer in big-endian (network) byte order.

Parameters
indexStarting index to read from (must be < size() - 3)
Returns
uint32_t The 32-bit value read from the array
Exceptions
std::out_of_rangeif index + 3 >= size()
Examples
/home/runner/work/libdoip/libdoip/inc/ByteArray.h.

Definition at line 241 of file ByteArray.h.

References doip::util::readU32BE().

Referenced by doip::DoIPMessage::getPayloadLengthFromHeader(), and readEnum().

◆ writeEnum()

template<typename E >
void doip::ByteArray::writeEnum ( value)
inline

◆ writeEnumAt()

template<typename E >
void doip::ByteArray::writeEnumAt ( size_t  index,
value 
)
inline

Writes an enum class value at a specific index as its underlying integral type.

Overwrites bytes starting at the specified index with the enum value converted to its underlying integral type in big-endian format.

Template Parameters
EEnum class type
Parameters
indexStarting index where to write
valueThe enum value to write
Exceptions
std::out_of_rangeif not enough space at index
Examples
/home/runner/work/libdoip/libdoip/inc/ByteArray.h.

Definition at line 194 of file ByteArray.h.

References writeU16At(), and writeU32At().

◆ writeU16At()

void doip::ByteArray::writeU16At ( size_t  index,
uint16_t  value 
)
inline

Writes a 16-bit unsigned integer in big-endian format at a specific index.

Overwrites 2 bytes starting at the specified index with the value in big-endian (network) byte order.

Parameters
indexStarting index where to write (must be < size() - 1)
valueThe 16-bit value to write
Exceptions
std::out_of_rangeif index + 1 >= size()
Examples
/home/runner/work/libdoip/libdoip/inc/ByteArray.h.

Definition at line 94 of file ByteArray.h.

Referenced by writeEnumAt().

◆ writeU16BE()

void doip::ByteArray::writeU16BE ( uint16_t  value)
inline

◆ writeU32At()

void doip::ByteArray::writeU32At ( size_t  index,
uint32_t  value 
)
inline

Writes a 32-bit unsigned integer in big-endian format at a specific index.

Overwrites 4 bytes starting at the specified index with the value in big-endian (network) byte order.

Parameters
indexStarting index where to write (must be < size() - 3)
valueThe 32-bit value to write
Exceptions
std::out_of_rangeif index + 3 >= size()
Examples
/home/runner/work/libdoip/libdoip/inc/ByteArray.h.

Definition at line 125 of file ByteArray.h.

Referenced by writeEnumAt().

◆ writeU32BE()

void doip::ByteArray::writeU32BE ( uint32_t  value)
inline

Appends a 32-bit unsigned integer in big-endian format to the end.

Adds 4 bytes to the end of the ByteArray representing the value in big-endian (network) byte order.

Parameters
valueThe 32-bit value to append
Examples
/home/runner/work/libdoip/libdoip/inc/ByteArray.h.

Definition at line 143 of file ByteArray.h.

Referenced by doip::DoIPMessage::buildMessage(), and writeEnum().


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