2020-09-04 16:07:11 +02:00
|
|
|
#ifndef FSFW_SERIALIZE_SERIALARRAYLISTADAPTER_H_
|
|
|
|
#define FSFW_SERIALIZE_SERIALARRAYLISTADAPTER_H_
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-08-13 20:53:35 +02:00
|
|
|
#include "SerializeIF.h"
|
2020-09-04 16:07:11 +02:00
|
|
|
#include "../container/ArrayList.h"
|
2016-06-15 23:48:41 +02:00
|
|
|
#include <utility>
|
|
|
|
|
2019-08-28 14:50:24 +02:00
|
|
|
/**
|
2020-09-04 16:07:11 +02:00
|
|
|
* Also serializes length field !
|
|
|
|
* @author baetz
|
2020-07-28 13:04:58 +02:00
|
|
|
* @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 SerialArrayListAdapter : public SerializeIF {
|
|
|
|
public:
|
|
|
|
SerialArrayListAdapter(ArrayList<T, count_t> *adaptee) : adaptee(adaptee) {
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size,
|
|
|
|
size_t maxSize, Endianness streamEndianness) const {
|
|
|
|
return serialize(adaptee, buffer, size, maxSize, streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
2020-07-28 13:04:58 +02:00
|
|
|
static ReturnValue_t serialize(const ArrayList<T, count_t>* list,
|
2020-09-04 16:07:11 +02:00
|
|
|
uint8_t** buffer, size_t* size, size_t maxSize,
|
|
|
|
Endianness streamEndianness) {
|
2020-04-21 21:34:03 +02:00
|
|
|
ReturnValue_t result = SerializeAdapter::serialize(&list->size,
|
2020-04-21 22:28:43 +02:00
|
|
|
buffer, size, maxSize, streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
count_t i = 0;
|
|
|
|
while ((result == HasReturnvaluesIF::RETURN_OK) && (i < list->size)) {
|
2020-09-04 16:07:11 +02:00
|
|
|
result = SerializeAdapter::serialize(&list->entries[i], buffer, size,
|
|
|
|
maxSize, streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
virtual size_t getSerializedSize() const {
|
2016-06-15 23:48:41 +02:00
|
|
|
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) {
|
2020-04-21 21:34:03 +02:00
|
|
|
printSize += SerializeAdapter::getSerializedSize(&list->entries[i]);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return printSize;
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
|
|
|
|
Endianness streamEndianness) {
|
|
|
|
return deSerialize(adaptee, buffer, size, streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
2020-07-28 13:04:58 +02:00
|
|
|
static ReturnValue_t deSerialize(ArrayList<T, count_t>* list,
|
2020-09-04 16:07:11 +02:00
|
|
|
const uint8_t** buffer, size_t* size,
|
2020-04-21 22:28:43 +02:00
|
|
|
Endianness streamEndianness) {
|
2018-07-12 16:29:32 +02:00
|
|
|
count_t tempSize = 0;
|
2020-04-21 21:34:03 +02:00
|
|
|
ReturnValue_t result = SerializeAdapter::deSerialize(&tempSize,
|
2020-04-21 22:28:43 +02:00
|
|
|
buffer, size, streamEndianness);
|
2020-07-28 13:02:43 +02:00
|
|
|
if(result != HasReturnvaluesIF::RETURN_OK) {
|
2020-09-04 16:07:11 +02:00
|
|
|
return result;
|
2020-07-28 13:02:43 +02:00
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
if (tempSize > list->maxSize()) {
|
2016-06-15 23:48:41 +02:00
|
|
|
return SerializeIF::TOO_MANY_ELEMENTS;
|
|
|
|
}
|
2020-09-04 16:07:11 +02:00
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
list->size = tempSize;
|
2016-06-15 23:48:41 +02:00
|
|
|
count_t i = 0;
|
|
|
|
while ((result == HasReturnvaluesIF::RETURN_OK) && (i < list->size)) {
|
2020-04-21 21:34:03 +02:00
|
|
|
result = SerializeAdapter::deSerialize(
|
2016-06-15 23:48:41 +02:00
|
|
|
&list->front()[i], buffer, size,
|
2020-04-21 22:28:43 +02:00
|
|
|
streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2020-09-04 16:07:11 +02:00
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
private:
|
|
|
|
ArrayList<T, count_t> *adaptee;
|
|
|
|
};
|
|
|
|
|
2020-09-04 16:07:11 +02:00
|
|
|
#endif /* FSFW_SERIALIZE_SERIALARRAYLISTADAPTER_H_ */
|