fsfw/serialize/SerialArrayListAdapter.h

86 lines
2.4 KiB
C
Raw Normal View History

2020-07-28 13:04:58 +02:00
#ifndef FRAMEWORK_SERIALIZE_SERIALARRAYLISTADAPTER_H_
#define FRAMEWORK_SERIALIZE_SERIALARRAYLISTADAPTER_H_
#include <framework/container/ArrayList.h>
#include <framework/serialize/SerializeIF.h>
#include <utility>
/**
2020-07-28 13:04:58 +02:00
* @ingroup serialize
* @author baetz
*/
template<typename T, typename count_t = uint8_t>
class SerialArrayListAdapter : public SerializeIF {
public:
SerialArrayListAdapter(ArrayList<T, count_t> *adaptee) : adaptee(adaptee) {
}
virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size,
size_t maxSize, Endianness streamEndianness) const {
return serialize(adaptee, buffer, size, maxSize, streamEndianness);
}
2020-07-28 13:04:58 +02:00
static ReturnValue_t serialize(const ArrayList<T, count_t>* list,
uint8_t** buffer, size_t* size, size_t maxSize,
Endianness streamEndianness) {
ReturnValue_t result = SerializeAdapter::serialize(&list->size,
buffer, size, maxSize, streamEndianness);
count_t i = 0;
while ((result == HasReturnvaluesIF::RETURN_OK) && (i < list->size)) {
2020-07-28 13:04:58 +02:00
result = SerializeAdapter::serialize(&list->entries[i], buffer,
size, maxSize, streamEndianness);
++i;
}
return result;
}
virtual size_t getSerializedSize() const {
return getSerializedSize(adaptee);
}
static uint32_t getSerializedSize(const ArrayList<T, count_t>* list) {
uint32_t printSize = sizeof(count_t);
count_t i = 0;
for (i = 0; i < list->size; ++i) {
printSize += SerializeAdapter::getSerializedSize(&list->entries[i]);
}
return printSize;
}
virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
Endianness streamEndianness) {
return deSerialize(adaptee, buffer, size, streamEndianness);
}
2020-07-28 13:04:58 +02:00
static ReturnValue_t deSerialize(ArrayList<T, count_t>* list,
const uint8_t** buffer, size_t* size,
Endianness streamEndianness) {
count_t tempSize = 0;
ReturnValue_t result = SerializeAdapter::deSerialize(&tempSize,
buffer, size, streamEndianness);
2020-07-28 13:02:43 +02:00
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
if (tempSize > list->maxSize()) {
return SerializeIF::TOO_MANY_ELEMENTS;
}
list->size = tempSize;
count_t i = 0;
while ((result == HasReturnvaluesIF::RETURN_OK) && (i < list->size)) {
result = SerializeAdapter::deSerialize(
&list->front()[i], buffer, size,
streamEndianness);
++i;
}
return result;
}
private:
ArrayList<T, count_t> *adaptee;
};
2020-07-28 13:04:58 +02:00
#endif /* FRAMEWORK_SERIALIZE_SERIALARRAYLISTADAPTER_H_ */