2020-09-04 16:00:45 +02:00
|
|
|
#ifndef FSFW_SERIALIZE_SERIALLINKEDLISTADAPTER_H_
|
|
|
|
#define FSFW_SERIALIZE_SERIALLINKEDLISTADAPTER_H_
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-08-13 20:53:35 +02:00
|
|
|
#include "../container/SinglyLinkedList.h"
|
|
|
|
#include "SerializeAdapter.h"
|
|
|
|
#include "SerializeElement.h"
|
|
|
|
#include "SerializeIF.h"
|
2019-08-28 14:50:24 +02:00
|
|
|
|
2020-09-04 16:00:45 +02:00
|
|
|
/**
|
|
|
|
* @brief Implement the conversion of object data to data streams
|
|
|
|
* or vice-versa, using linked lists.
|
|
|
|
* @details
|
|
|
|
* An alternative to the AutoSerializeAdapter functions
|
|
|
|
* - All object members with a datatype are declared as
|
|
|
|
* SerializeElement<element_type> members inside the class
|
|
|
|
* implementing this adapter.
|
|
|
|
* - The element type can also be a SerialBufferAdapter to
|
|
|
|
* de-/serialize buffers.
|
|
|
|
* - The element type can also be a SerialFixedArrayListAdapter to
|
|
|
|
* de-/serialize buffers with a size header, which is scanned automatically.
|
|
|
|
*
|
|
|
|
* The sequence of objects is defined in the constructor by using
|
|
|
|
* the setStart and setNext functions.
|
|
|
|
*
|
|
|
|
* 1. The serialization process is done by instantiating the class and
|
|
|
|
* calling serialize after all SerializeElement entries have been set by
|
|
|
|
* using the constructor or setter functions. An additional size variable
|
|
|
|
* can be supplied which is calculated/incremented automatically.
|
|
|
|
* 2. The deserialization process is done by instantiating the class and
|
|
|
|
* supplying a buffer with the data which is converted into an object.
|
|
|
|
* The size of data to serialize can be supplied and is
|
|
|
|
* decremented in the function. Range checking is done internally.
|
|
|
|
* @author baetz
|
|
|
|
* @ingroup serialize
|
2019-08-28 14:50:24 +02:00
|
|
|
*/
|
2016-06-15 23:48:41 +02:00
|
|
|
template<typename T, typename count_t = uint8_t>
|
|
|
|
class SerialLinkedListAdapter: public SinglyLinkedList<T>, public SerializeIF {
|
|
|
|
public:
|
2020-09-04 16:00:45 +02:00
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
SerialLinkedListAdapter(typename LinkedElement<T>::Iterator start,
|
|
|
|
bool printCount = false) :
|
|
|
|
SinglyLinkedList<T>(start), printCount(printCount) {
|
|
|
|
}
|
2020-09-04 16:00:45 +02:00
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
SerialLinkedListAdapter(LinkedElement<T>* first, bool printCount = false) :
|
|
|
|
SinglyLinkedList<T>(first), printCount(printCount) {
|
|
|
|
|
|
|
|
}
|
2020-09-04 16:00:45 +02:00
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
SerialLinkedListAdapter(bool printCount = false) :
|
|
|
|
SinglyLinkedList<T>(), printCount(printCount) {
|
|
|
|
}
|
|
|
|
|
2020-04-21 21:34:03 +02:00
|
|
|
virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size,
|
|
|
|
size_t maxSize, Endianness streamEndianness) const override {
|
2016-06-15 23:48:41 +02:00
|
|
|
if (printCount) {
|
|
|
|
count_t mySize = SinglyLinkedList<T>::getSize();
|
2020-04-21 21:34:03 +02:00
|
|
|
ReturnValue_t result = SerializeAdapter::serialize(&mySize,
|
|
|
|
buffer, size, maxSize, streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 21:34:03 +02:00
|
|
|
return serialize(SinglyLinkedList<T>::start, buffer, size, maxSize,
|
|
|
|
streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static ReturnValue_t serialize(const LinkedElement<T>* element,
|
2020-04-21 21:34:03 +02:00
|
|
|
uint8_t** buffer, size_t* size, size_t maxSize,
|
|
|
|
Endianness streamEndianness) {
|
2016-06-15 23:48:41 +02:00
|
|
|
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
2020-09-04 16:00:45 +02:00
|
|
|
while ((result == HasReturnvaluesIF::RETURN_OK) and (element != nullptr)) {
|
2020-04-21 21:34:03 +02:00
|
|
|
result = element->value->serialize(buffer, size, maxSize,
|
|
|
|
streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
element = element->getNext();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2020-09-04 16:00:45 +02:00
|
|
|
|
2020-04-21 21:34:03 +02:00
|
|
|
virtual size_t getSerializedSize() const override {
|
2016-06-15 23:48:41 +02:00
|
|
|
if (printCount) {
|
|
|
|
return SerialLinkedListAdapter<T>::getSerializedSize()
|
|
|
|
+ sizeof(count_t);
|
|
|
|
} else {
|
|
|
|
return getSerializedSize(SinglyLinkedList<T>::start);
|
|
|
|
}
|
|
|
|
}
|
2020-09-04 16:00:45 +02:00
|
|
|
|
2020-04-21 21:34:03 +02:00
|
|
|
static size_t getSerializedSize(const LinkedElement<T> *element) {
|
|
|
|
size_t size = 0;
|
2020-09-04 16:00:45 +02:00
|
|
|
while (element != nullptr) {
|
2016-06-15 23:48:41 +02:00
|
|
|
size += element->value->getSerializedSize();
|
|
|
|
element = element->getNext();
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2020-09-04 16:00:45 +02:00
|
|
|
|
2020-04-21 21:34:03 +02:00
|
|
|
virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
|
|
|
|
Endianness streamEndianness) override {
|
2020-09-04 16:00:45 +02:00
|
|
|
return deSerialize(SinglyLinkedList<T>::start, buffer, size,
|
|
|
|
streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static ReturnValue_t deSerialize(LinkedElement<T>* element,
|
2020-04-21 21:34:03 +02:00
|
|
|
const uint8_t** buffer, size_t* size, Endianness streamEndianness) {
|
2016-06-15 23:48:41 +02:00
|
|
|
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
2020-09-04 16:00:45 +02:00
|
|
|
while ((result == HasReturnvaluesIF::RETURN_OK) and (element != nullptr)) {
|
2020-04-21 21:34:03 +02:00
|
|
|
result = element->value->deSerialize(buffer, size, streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
element = element->getNext();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-09-04 16:00:45 +02:00
|
|
|
/**
|
|
|
|
* Copying is forbidden by deleting the copy constructor and the copy
|
|
|
|
* assignment operator because of the pointers to the linked list members.
|
|
|
|
* Unless the child class implements an own copy constructor or
|
|
|
|
* copy assignment operator, these operation will throw a compiler error.
|
|
|
|
* @param
|
|
|
|
*/
|
|
|
|
SerialLinkedListAdapter(const SerialLinkedListAdapter &) = delete;
|
|
|
|
SerialLinkedListAdapter& operator=(const SerialLinkedListAdapter&) = delete;
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-09-04 16:00:45 +02:00
|
|
|
bool printCount;
|
2016-06-15 23:48:41 +02:00
|
|
|
};
|
|
|
|
|
2020-09-04 16:00:45 +02:00
|
|
|
#endif /* FSFW_SERIALIZE_SERIALLINKEDLISTADAPTER_H_ */
|