HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
HF::UID::ByteArray< _Class, _size, _type > Class Template Reference

Helper template parent class for all UIDs based on fixed number of bytes. More...

#include <uids.h>

+ Inheritance diagram for HF::UID::ByteArray< _Class, _size, _type >:
+ Collaboration diagram for HF::UID::ByteArray< _Class, _size, _type >:

Public Member Functions

 ByteArray (uint8_t _value[_size])
 Constructor. More...
 
 ByteArray (uint8_t _value)
 Constructor. More...
 
uint16_t size () const
 Number bytes needed to serialize the message. More...
 
uint16_t pack (Common::ByteArray &array, uint16_t offset=0) const
 Write the object on to a ByteArray so it can be sent over the network. More...
 
uint16_t unpack (const Common::ByteArray &array, uint16_t offset=0)
 Read a message from a ByteArray. More...
 
int compare (const UID_T &other) const
 Compare the current UID with the given UID. More...
 
uint8_t const & operator[] (uint16_t index) const
 Get byte at the given index. More...
 
uint8_t & operator[] (uint16_t index)
 Get byte at the given index. More...
 
uint8_t const & at (uint16_t index) const
 Get byte at the given index, asserting that the index is within the correct range. More...
 
uint8_t & at (uint16_t index)
 Get byte at the given index, asserting that the index is within the correct range. More...
 
void fill (uint8_t byte)
 Fill the underlining byte array with the given value. More...
 
void clear ()
 Fill the underlining byte array with 0.
 
- Public Member Functions inherited from HF::UID::Abstract< _type >
uint8_t type () const
 Type of the UID. More...
 
- Public Member Functions inherited from HF::Common::Serializable
virtual ~Serializable ()
 Destructor.
 
- Public Member Functions inherited from HF::Common::Cloneable< UID_T >
virtual UID_T * clone () const=0
 Create a clone object of the object where this method is being called. More...
 

Static Public Member Functions

static uint16_t length ()
 Return the number of bytes in the underlining byte array. More...
 

Static Public Attributes

static constexpr uint16_t min_size = UID_T::min_size + _size
 Minimum pack/unpack required data size.
 
- Static Public Attributes inherited from HF::UID::UID_T
static constexpr uint16_t min_size
 Minimum pack/unpack required data size.
 

Detailed Description

template<typename _Class, uint8_t _size, uint8_t _type>
class HF::UID::ByteArray< _Class, _size, _type >

Helper template parent class for all UIDs based on fixed number of bytes.

Template Parameters
_Classchild class being created.
_sizenumber of bytes in the UID.
_typeUID type value.

Definition at line 192 of file uids.h.

Constructor & Destructor Documentation

◆ ByteArray() [1/2]

template<typename _Class, uint8_t _size, uint8_t _type>
HF::UID::ByteArray< _Class, _size, _type >::ByteArray ( uint8_t  _value[_size])
inline

Constructor.

Parameters
[in]_valueinitial value for UID.

Definition at line 207 of file uids.h.

207  : Abstract<_type>()
208  {
209  memcpy(value, _value, _size * sizeof(uint8_t));
210  }

◆ ByteArray() [2/2]

template<typename _Class, uint8_t _size, uint8_t _type>
HF::UID::ByteArray< _Class, _size, _type >::ByteArray ( uint8_t  _value)
inline

Constructor.

Parameters
[in]_valueinitial fill value for UID.

Definition at line 217 of file uids.h.

217  : Abstract<_type>()
218  {
219  memset(value, _value, _size * sizeof(uint8_t));
220  }

Member Function Documentation

◆ at() [1/2]

template<typename _Class, uint8_t _size, uint8_t _type>
uint8_t const& HF::UID::ByteArray< _Class, _size, _type >::at ( uint16_t  index) const
inline

Get byte at the given index, asserting that the index is within the correct range.

Warning
the assert is only in effect if NDEBUG is not defined.
Parameters
[in]indexindex to get the byte from.
Returns
the byte at the given index.

Definition at line 320 of file uids.h.

321  {
322  assert(index < _size);
323  return value[index];
324  }

◆ at() [2/2]

template<typename _Class, uint8_t _size, uint8_t _type>
uint8_t& HF::UID::ByteArray< _Class, _size, _type >::at ( uint16_t  index)
inline

Get byte at the given index, asserting that the index is within the correct range.

Warning
the assert is only in effect if NDEBUG is not defined.
Parameters
[in]indexindex to get the byte from.
Returns
the byte at the given index.

Definition at line 336 of file uids.h.

337  {
338  assert(index < _size);
339  return value[index];
340  }

◆ compare()

template<typename _Class, uint8_t _size, uint8_t _type>
int HF::UID::ByteArray< _Class, _size, _type >::compare ( const UID_T other) const
inlinevirtual

Compare the current UID with the given UID.

This function returns a value less that 0 if the current UID object order is lower that the given UID, 0 if the UIDs represent the same entity and a value greater that 0 if current UID object is above the given UID.

Parameters
[in]othera pointer to a UID object to compare to.
Return values
<0the current UID is lower that the given UID.
0the current UID is the same as given UID.
>0the current UID is greater that the given UID.

Reimplemented from HF::UID::UID_T.

Definition at line 274 of file uids.h.

275  {
276  int res = Abstract<_type>::compare(other);
277 
278  return (res ==
279  0 ? memcmp(value, ((_Class *) &other)->value, sizeof(value)) : res);
280  }
virtual int compare(const UID_T &other) const
Compare the current UID with the given UID.
Definition: uids.h:88

◆ fill()

template<typename _Class, uint8_t _size, uint8_t _type>
void HF::UID::ByteArray< _Class, _size, _type >::fill ( uint8_t  byte)
inline

Fill the underlining byte array with the given value.

Parameters
bytethe value to be used to fill the byte array.

Definition at line 351 of file uids.h.

352  {
353  memset(value, byte, sizeof(value));
354  }

◆ length()

template<typename _Class, uint8_t _size, uint8_t _type>
static uint16_t HF::UID::ByteArray< _Class, _size, _type >::length ( )
inlinestatic

Return the number of bytes in the underlining byte array.

Returns
number of bytes in the underlining byte array.

Definition at line 369 of file uids.h.

370  {
371  return _size;
372  }

◆ operator[]() [1/2]

template<typename _Class, uint8_t _size, uint8_t _type>
uint8_t const& HF::UID::ByteArray< _Class, _size, _type >::operator[] ( uint16_t  index) const
inline

Get byte at the given index.

Parameters
[in]indexindex to get the byte from.
Returns
the byte at the given index.

Definition at line 293 of file uids.h.

294  {
295  return value[index];
296  }

◆ operator[]() [2/2]

template<typename _Class, uint8_t _size, uint8_t _type>
uint8_t& HF::UID::ByteArray< _Class, _size, _type >::operator[] ( uint16_t  index)
inline

Get byte at the given index.

Parameters
[in]indexindex to get the byte from.
Returns
the byte at the given index.

Definition at line 305 of file uids.h.

306  {
307  return value[index];
308  }

◆ pack()

template<typename _Class, uint8_t _size, uint8_t _type>
uint16_t HF::UID::ByteArray< _Class, _size, _type >::pack ( Common::ByteArray< _Class, _size, _type > &  array,
uint16_t  offset = 0 
) const
inlinevirtual

Write the object on to a ByteArray so it can be sent over the network.

The buffer passed in MUST have enough size to hold the serialized object, e.g.,

Serializable obj;
ByteArray payload(obj.size());
obj.pack(payload);
Parameters
[in,out]arrayByteArray reference to write the object to.
[in]offsetoffset to start writing to.
Returns
the number of bytes written.

Reimplemented from HF::UID::UID_T.

Definition at line 230 of file uids.h.

231  {
232  HF_SERIALIZABLE_CHECK(array, offset, min_size);
233 
234  offset += Abstract<_type>::pack(array, offset);
235 
236  offset += array.write(offset, (uint8_t) sizeof(value));
237 
238  for (uint8_t i = 0; i < sizeof(value); i++)
239  {
240  offset += array.write(offset, value[i]);
241  }
242 
243  return min_size;
244  }
#define HF_SERIALIZABLE_CHECK(__array, __offset, __size)
Helper macro to check if the given __array has enough size so __size bytes can be written/read from t...
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
Definition: uids.h:223
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
Definition: uids.h:104

◆ size()

template<typename _Class, uint8_t _size, uint8_t _type>
uint16_t HF::UID::ByteArray< _Class, _size, _type >::size ( ) const
inlinevirtual

Number bytes needed to serialize the message.

Returns
number of bytes the message requires to be serialized.

Reimplemented from HF::UID::UID_T.

Definition at line 225 of file uids.h.

Referenced by HF::UID::ByteArray< MAC, 6, MAC_UID >::unpack().

226  {
227  return min_size;
228  }
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
Definition: uids.h:223
+ Here is the caller graph for this function:

◆ unpack()

template<typename _Class, uint8_t _size, uint8_t _type>
uint16_t HF::UID::ByteArray< _Class, _size, _type >::unpack ( const Common::ByteArray< _Class, _size, _type > &  array,
uint16_t  offset = 0 
)
inlinevirtual

Read a message from a ByteArray.

Parameters
[in]arrayByteArray reference to read the message from.
[in]offsetoffset to start reading from.
Returns
the number of bytes read.

Reimplemented from HF::UID::UID_T.

Definition at line 246 of file uids.h.

247  {
248  HF_SERIALIZABLE_CHECK(array, offset, min_size);
249 
250  offset += Abstract<_type>::unpack(array, offset);
251 
252  uint8_t size;
253  offset += array.read(offset, size);
254 
255  assert(size == sizeof(value));
256 
257  if (size != sizeof(value))
258  {
259  return 0;
260  }
261 
262  for (uint8_t i = 0; i < size; i++)
263  {
264  offset += array.read(offset, value[i]);
265  }
266 
267  return min_size;
268  }
uint16_t size() const
Number bytes needed to serialize the message.
Definition: uids.h:225
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
Definition: uids.h:114
#define HF_SERIALIZABLE_CHECK(__array, __offset, __size)
Helper macro to check if the given __array has enough size so __size bytes can be written/read from t...
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
Definition: uids.h:223

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