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

Wrapper for Common::ByteArray implementing the Serializable API. More...

#include <common.h>

+ Inheritance diagram for HF::Common::SerializableHelper< Common::ByteArray >:
+ Collaboration diagram for HF::Common::SerializableHelper< Common::ByteArray >:

Public Member Functions

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...
 
- Public Member Functions inherited from HF::Common::Serializable
virtual ~Serializable ()
 Destructor.
 

Static Public Attributes

static constexpr uint16_t min_size = sizeof(uint8_t)
 Minimum pack/unpack required data size.
 

Detailed Description

template<>
struct HF::Common::SerializableHelper< Common::ByteArray >

Wrapper for Common::ByteArray implementing the Serializable API.

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

Member Function Documentation

◆ pack()

uint16_t HF::Common::SerializableHelper< Common::ByteArray >::pack ( Common::ByteArray 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.

Implements HF::Common::Serializable.

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

References HF::Common::SerializableHelper< T, typename >::data, HF_SERIALIZABLE_CHECK, HF::Common::SerializableHelper< T, typename >::size(), and HF::Common::ByteArray::write().

664  {
665  HF_SERIALIZABLE_CHECK(array, offset, size());
666 
667  uint16_t start = offset;
668 
669  offset += array.write(offset, (uint8_t) data.size());
670 
671  auto it = array.begin();
672  std::advance(it, offset);
673 
674  std::copy(data.begin(), data.end(), it);
675 
676  offset += data.size();
677 
678  return offset - start;
679  }
#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...
uint16_t size() const
Number bytes needed to serialize the message.
+ Here is the call graph for this function:

◆ size()

uint16_t HF::Common::SerializableHelper< Common::ByteArray >::size ( ) const
inlinevirtual

Number bytes needed to serialize the message.

Returns
number of bytes the message requires to be serialized.

Implements HF::Common::Serializable.

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

References HF::Common::SerializableHelper< T, typename >::data.

659  {
660  return min_size + data.size();
661  }
static constexpr uint16_t min_size
Minimum pack/unpack required data size.

◆ unpack()

uint16_t HF::Common::SerializableHelper< Common::ByteArray >::unpack ( const Common::ByteArray 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.

Implements HF::Common::Serializable.

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

References HF::Common::SerializableHelper< T, typename >::data, HF_SERIALIZABLE_CHECK, and HF::Common::ByteArray::read().

682  {
683  HF_SERIALIZABLE_CHECK(array, offset, min_size);
684 
685  uint16_t start = offset;
686 
687  uint8_t _size = 0;
688  offset += array.read(offset, _size);
689 
690  HF_SERIALIZABLE_CHECK(array, offset, _size);
691 
692  auto it = array.begin();
693  std::advance(it, offset);
694 
695  std::copy_n(it, _size, data.begin());
696 
697  offset += _size;
698 
699  return offset - start;
700  }
#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.
+ Here is the call graph for this function:

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