HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
HF::Common::ByteArray Struct Reference

This class represents a byte array. More...

#include <common.h>

Inherits std::vector< T >.

+ Collaboration diagram for HF::Common::ByteArray:

Public Member Functions

 ByteArray (uint16_t size=0)
 Create a byte array with the given initial size. More...
 
 ByteArray (const uint8_t data[], const uint16_t size)
 Create a byte array with the given initial data. More...
 
 ByteArray (std::initializer_list< uint8_t > raw)
 Create byte array from the values in the given list. More...
 
virtual ~ByteArray ()
 Destructor.
 
uint16_t write (uint16_t offset, uint8_t data)
 Write a byte into the array at the given offset. More...
 
uint16_t write (uint16_t offset, uint16_t data)
 Write a word in the big endian format into the array at the given offset. More...
 
uint16_t write (uint16_t offset, uint32_t data)
 Write a double-word in big endian format into the array at the given offset. More...
 
uint16_t write (uint16_t offset, bool data)
 Write a byte into the array at the given offset. (uint16_t, uint8_t) More...
 
template<typename T >
uint16_t write (uint16_t offset, T data)
 Write a byte into the array at the given offset. (uint16_t, uint8_t) More...
 
uint16_t read (uint16_t offset, uint8_t &data) const
 Read the byte at offset into data. More...
 
uint16_t read (uint16_t offset, uint16_t &data) const
 Read the word in big-endian format at offset into data. More...
 
uint16_t read (uint16_t offset, uint32_t &data) const
 Read the double-word in big-endian format at offset into data. More...
 
uint16_t read (uint16_t offset, bool &data) const
 Read the byte at offset into data. (uint16_t, uint8_t) More...
 
template<typename T >
uint16_t read (uint16_t offset, T &data) const
 Read the byte at offset into data. (uint16_t, uint8_t) More...
 
bool available (uint16_t offset, uint16_t expected) const
 Check if the array as at least expected bytes available from the given offset. More...
 
uint16_t available (uint16_t offset) const
 Return the number of data bytes available from the given offset. More...
 
void extend (uint16_t _size)
 Extend the byte array by the given size. More...
 
void ensure (uint16_t _offset, uint16_t _size)
 Ensure that the byte array can hold the number of bytes given in _size, from the given offset. More...
 

Detailed Description

This class represents a byte array.

The method in this class are used to serialize the messages to be sent over the network, converting between the host's endianness and the big-endian network format.

Definition at line 239 of file inc/hanfun/common.h.

Constructor & Destructor Documentation

◆ ByteArray() [1/3]

HF::Common::ByteArray::ByteArray ( uint16_t  size = 0)

Create a byte array with the given initial size.

Parameters
[in]sizethe initial size of the byte array.

◆ ByteArray() [2/3]

HF::Common::ByteArray::ByteArray ( const uint8_t  data[],
const uint16_t  size 
)

Create a byte array with the given initial data.

Parameters
[in]datadata to initialize the byte array with.
[in]sizesize in bytes of the data.

◆ ByteArray() [3/3]

HF::Common::ByteArray::ByteArray ( std::initializer_list< uint8_t >  raw)
inline

Create byte array from the values in the given list.

Parameters
[in]rawvalues to add to the byte array.

Definition at line 261 of file inc/hanfun/common.h.

261  : vector(raw)
262  {}

Member Function Documentation

◆ available() [1/2]

bool HF::Common::ByteArray::available ( uint16_t  offset,
uint16_t  expected 
) const
inline

Check if the array as at least expected bytes available from the given offset.

Parameters
[in]offsetthe offset from where to start counting.
[in]expectedthe number of byte required.
Return values
trueif enough data is available,
falseotherwise.

Definition at line 377 of file inc/hanfun/common.h.

Referenced by ensure().

378  {
379  return expected <= available(offset);
380  }
bool available(uint16_t offset, uint16_t expected) const
Check if the array as at least expected bytes available from the given offset.
+ Here is the caller graph for this function:

◆ available() [2/2]

uint16_t HF::Common::ByteArray::available ( uint16_t  offset) const
inline

Return the number of data bytes available from the given offset.

Parameters
[in]offsetthe offset from where to start counting.
Returns
number of data bytes available from the given offset.

Definition at line 389 of file inc/hanfun/common.h.

390  {
391  return (size() >= offset ? size() - offset : 0);
392  }

◆ ensure()

void HF::Common::ByteArray::ensure ( uint16_t  _offset,
uint16_t  _size 
)
inline

Ensure that the byte array can hold the number of bytes given in _size, from the given offset.

Parameters
[in]_offsetoffset index to check from.
[in]_sizenumber of bytes to ensure that exist.

Definition at line 424 of file inc/hanfun/common.h.

References available().

425  {
426  if (!available(_offset, _size))
427  {
428  _size = _size - available(_offset) + std::max<int16_t>(
429  (int16_t) (_offset - size()), 0);
430  vector<uint8_t>::reserve(size() + _size);
431  std::fill_n(back_inserter(*this), _size, 0);
432  }
433  }
bool available(uint16_t offset, uint16_t expected) const
Check if the array as at least expected bytes available from the given offset.
+ Here is the call graph for this function:

◆ extend()

void HF::Common::ByteArray::extend ( uint16_t  _size)
inline

Extend the byte array by the given size.

This is the same as calling : array.reserve(array.size() + _size)

Parameters
[in]_sizenumber of bytes to extent the array by.

Definition at line 412 of file inc/hanfun/common.h.

413  {
414  vector<uint8_t>::reserve(size() + _size);
415  }

◆ read() [1/5]

◆ read() [2/5]

uint16_t HF::Common::ByteArray::read ( uint16_t  offset,
uint16_t &  data 
) const

Read the word in big-endian format at offset into data.

Parameters
[in]offsetoffset to read the word from.
[out]datareference to save the read value to.
Returns
number of bytes read (2).

◆ read() [3/5]

uint16_t HF::Common::ByteArray::read ( uint16_t  offset,
uint32_t &  data 
) const

Read the double-word in big-endian format at offset into data.

Parameters
[in]offsetoffset to read the double-word from.
[out]datareference to save the read value to.
Returns
number of bytes read (4).

◆ read() [4/5]

uint16_t HF::Common::ByteArray::read ( uint16_t  offset,
bool &  data 
) const
inline

Read the byte at offset into data. (uint16_t, uint8_t)

Parameters
[in]offsetoffset to read the byte from.
[out]datareference to save the read value to.
Returns
number of bytes read (1). (uint16_t, uint8_t)

Definition at line 344 of file inc/hanfun/common.h.

References read().

345  {
346  uint8_t temp;
347  uint16_t result = read(offset, temp);
348 
349  data = (temp & 0x01) != 0;
350 
351  return result;
352  }
uint16_t read(uint16_t offset, uint8_t &data) const
Read the byte at offset into data.
+ Here is the call graph for this function:

◆ read() [5/5]

template<typename T >
uint16_t HF::Common::ByteArray::read ( uint16_t  offset,
T &  data 
) const
inline

Read the byte at offset into data. (uint16_t, uint8_t)

Parameters
[in]offsetoffset to read the byte from.
[out]datareference to save the read value to.
Returns
number of bytes read (1). (uint16_t, uint8_t)

Definition at line 356 of file inc/hanfun/common.h.

References read().

357  {
358  typedef typename std::make_unsigned<T>::type Type;
359  Type temp;
360  uint16_t result = read(offset, temp);
361 
362  data = static_cast<T>(temp);
363 
364  return result;
365  }
Type
Types of reports send from the server to the client.
uint16_t read(uint16_t offset, uint8_t &data) const
Read the byte at offset into data.
+ Here is the call graph for this function:

◆ write() [1/5]

◆ write() [2/5]

uint16_t HF::Common::ByteArray::write ( uint16_t  offset,
uint16_t  data 
)

Write a word in the big endian format into the array at the given offset.

Parameters
[in]offsetoffset to write the word to.
[in]dataword value to write to the array.
Returns
number of bytes written (2).

◆ write() [3/5]

uint16_t HF::Common::ByteArray::write ( uint16_t  offset,
uint32_t  data 
)

Write a double-word in big endian format into the array at the given offset.

Parameters
[in]offsetoffset to write the double-word to.
[in]datadouble-word value to write to the array.
Returns
number of bytes written (4).

◆ write() [4/5]

uint16_t HF::Common::ByteArray::write ( uint16_t  offset,
bool  data 
)
inline

Write a byte into the array at the given offset. (uint16_t, uint8_t)

Parameters
[in]offsetoffset to write the byte to.
[in]databyte value to write to the array.
Returns
number of bytes written (1). (uint16_t, uint8_t)

Definition at line 300 of file inc/hanfun/common.h.

References write().

301  {
302  return write(offset, static_cast<uint8_t>(data ? 0x01 : 0x00));
303  }
uint16_t write(uint16_t offset, uint8_t data)
Write a byte into the array at the given offset.
+ Here is the call graph for this function:

◆ write() [5/5]

template<typename T >
uint16_t HF::Common::ByteArray::write ( uint16_t  offset,
data 
)
inline

Write a byte into the array at the given offset. (uint16_t, uint8_t)

Parameters
[in]offsetoffset to write the byte to.
[in]databyte value to write to the array.
Returns
number of bytes written (1). (uint16_t, uint8_t)

Definition at line 307 of file inc/hanfun/common.h.

References write().

308  {
309  typedef typename std::make_unsigned<T>::type Type;
310  return write(offset, static_cast<Type>(data));
311  }
Type
Types of reports send from the server to the client.
uint16_t write(uint16_t offset, uint8_t data)
Write a byte into the array at the given offset.
+ Here is the call graph for this function:

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