Array List Entry swapper function

And respective SerialAdapter functions to use it
This commit is contained in:
Robin Müller 2020-01-22 14:24:48 +01:00
parent 3d2bdae14d
commit 1977942c4b
5 changed files with 36 additions and 15 deletions

View File

@ -223,6 +223,25 @@ public:
count_t remaining() { count_t remaining() {
return (maxSize_ - size); return (maxSize_ - size);
} }
/**
* Swap the endianness of the Array list (not the length field !)
* Useful if the case the buffer type is larger than uint8_t
* @param list
*/
void swapArrayListEndianness() {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
count_t i = 0;
// uint8_t buffer does not require swapping of entries.
if(sizeof(T) == 1) {
return;
}
while ((result == HasReturnvaluesIF::RETURN_OK) && (i < size)) {
T newEntry = EndianSwapper::swap(entries[i]);
entries[i] = newEntry;
++i;
}
}
private: private:
/** /**
* This is the copy constructor * This is the copy constructor

View File

@ -2,8 +2,10 @@
#define FIXEDARRAYLIST_H_ #define FIXEDARRAYLIST_H_
#include <framework/container/ArrayList.h> #include <framework/container/ArrayList.h>
/** /**
* \ingroup container * @brief Array List with a fixed maximum size
* @ingroup container
*/ */
template<typename T, uint32_t MAX_SIZE, typename count_t = uint8_t> template<typename T, uint32_t MAX_SIZE, typename count_t = uint8_t>
class FixedArrayList: public ArrayList<T, count_t> { class FixedArrayList: public ArrayList<T, count_t> {
@ -17,10 +19,13 @@ public:
//We could create a constructor to initialize the fixed array list with data and the known size field //We could create a constructor to initialize the fixed array list with data and the known size field
//so it can be used for serialization too (with SerialFixedArrrayListAdapter) //so it can be used for serialization too (with SerialFixedArrrayListAdapter)
//is this feasible? //is this feasible?
FixedArrayList(T * data_, count_t count): FixedArrayList(T * data_, count_t count, bool swapArrayListEndianess = false):
ArrayList<T, count_t>(data, MAX_SIZE) { ArrayList<T, count_t>(data, MAX_SIZE) {
memcpy(this->data, data_, count); memcpy(this->data, data_, count);
this->size = count; this->size = count;
if(swapArrayListEndianess) {
ArrayList<T, count_t>::swapArrayListEndianness();
}
} }
FixedArrayList(const FixedArrayList& other) : FixedArrayList(const FixedArrayList& other) :

View File

@ -79,20 +79,9 @@ public:
return result; return result;
} }
/**
* Swap the endianness of the Array list (not the length field !)
* Useful if the case the buffer type is large than uint8_t and the endianness
* is inconsistent with other SerializeElements.
* @param list
*/
static void swapArrayListEndianness(ArrayList<T, count_t>* list) { static void swapArrayListEndianness(ArrayList<T, count_t>* list) {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; list->swapArrayListEndianness();
count_t i = 0;
while ((result == HasReturnvaluesIF::RETURN_OK) && (i < list->size)) {
T newEntry = EndianSwapper::swap(list->entries[i]);
list->entries[i] = newEntry;
++i;
}
} }
private: private:
ArrayList<T, count_t> *adaptee; ArrayList<T, count_t> *adaptee;

View File

@ -24,6 +24,10 @@
template<typename BUFFER_TYPE, uint32_t MAX_SIZE, typename count_t = uint8_t> template<typename BUFFER_TYPE, uint32_t MAX_SIZE, typename count_t = uint8_t>
class SerialFixedArrayListAdapter : public FixedArrayList<BUFFER_TYPE, MAX_SIZE, count_t>, public SerializeIF { class SerialFixedArrayListAdapter : public FixedArrayList<BUFFER_TYPE, MAX_SIZE, count_t>, public SerializeIF {
public: public:
/**
* Constructor Arguments are forwarded to FixedArrayList constructor
* @param args
*/
template<typename... Args> template<typename... Args>
SerialFixedArrayListAdapter(Args... args) : FixedArrayList<BUFFER_TYPE, MAX_SIZE, count_t>(std::forward<Args>(args)...) { SerialFixedArrayListAdapter(Args... args) : FixedArrayList<BUFFER_TYPE, MAX_SIZE, count_t>(std::forward<Args>(args)...) {
} }

View File

@ -19,6 +19,10 @@
template<typename T> template<typename T>
class SerializeElement : public SerializeIF, public LinkedElement<SerializeIF> { class SerializeElement : public SerializeIF, public LinkedElement<SerializeIF> {
public: public:
/**
* Arguments are forwarded to the element datatype constructor
* @param args
*/
template<typename... Args> template<typename... Args>
SerializeElement(Args... args) : LinkedElement<SerializeIF>(this), entry(std::forward<Args>(args)...) { SerializeElement(Args... args) : LinkedElement<SerializeIF>(this), entry(std::forward<Args>(args)...) {