Merge remote-tracking branch 'upstream/master' into mueller/serialize-convergence

This commit is contained in:
Robin Müller 2020-09-10 15:23:22 +02:00
commit 0da6456bad
7 changed files with 128 additions and 67 deletions

View File

@ -27,14 +27,27 @@ public:
/** /**
* @brief Custom copy constructor which prevents setting the * @brief Custom copy constructor which prevents setting the
* underlying pointer wrong. * underlying pointer wrong. This function allocates memory!
* @details This is a very heavy operation so try to avoid this!
*
*/ */
DynamicFIFO(const DynamicFIFO& other): FIFOBase<T>(other), DynamicFIFO(const DynamicFIFO& other): FIFOBase<T>(other),
fifoVector(other.maxCapacity) { fifoVector(other.maxCapacity) {
this->fifoVector = other.fifoVector;
this->setContainer(fifoVector.data()); this->setContainer(fifoVector.data());
} }
/**
* @brief Custom assignment operator
* @details This is a very heavy operation so try to avoid this!
* @param other DyamicFIFO to copy from
*/
DynamicFIFO& operator=(const DynamicFIFO& other){
FIFOBase<T>::operator=(other);
this->fifoVector = other.fifoVector;
this->setContainer(fifoVector.data());
return *this;
}
private: private:
std::vector<T> fifoVector; std::vector<T> fifoVector;
}; };

View File

@ -25,9 +25,21 @@ public:
* @param other * @param other
*/ */
FIFO(const FIFO& other): FIFOBase<T>(other) { FIFO(const FIFO& other): FIFOBase<T>(other) {
this->fifoArray = other.fifoArray;
this->setContainer(fifoArray.data()); this->setContainer(fifoArray.data());
} }
/**
* @brief Custom assignment operator
* @param other
*/
FIFO& operator=(const FIFO& other){
FIFOBase<T>::operator=(other);
this->fifoArray = other.fifoArray;
this->setContainer(fifoArray.data());
return *this;
}
private: private:
std::array<T, capacity> fifoArray; std::array<T, capacity> fifoArray;
}; };

View File

@ -4,12 +4,15 @@
#include "ArrayList.h" #include "ArrayList.h"
#include "../returnvalues/HasReturnvaluesIF.h" #include "../returnvalues/HasReturnvaluesIF.h"
#include <utility> #include <utility>
#include <type_traits>
/** /**
* \ingroup container * \ingroup container
*/ */
template<typename key_t, typename T> template<typename key_t, typename T>
class FixedMap: public SerializeIF { class FixedMap: public SerializeIF {
static_assert (std::is_trivially_copyable<T>::value or std::is_base_of<SerializeIF, T>::value,
"Types used in FixedMap must either be trivial copy-able or a derived Class from SerializeIF to be serialize-able");
public: public:
static const uint8_t INTERFACE_ID = CLASS_ID::FIXED_MAP; static const uint8_t INTERFACE_ID = CLASS_ID::FIXED_MAP;
static const ReturnValue_t KEY_ALREADY_EXISTS = MAKE_RETURN_CODE(0x01); static const ReturnValue_t KEY_ALREADY_EXISTS = MAKE_RETURN_CODE(0x01);

View File

@ -1,41 +0,0 @@
#ifndef ISDERIVEDFROM_H_
#define ISDERIVEDFROM_H_
template<typename D, typename B>
class IsDerivedFrom {
class No {
};
class Yes {
No no[3];
};
static Yes Test(B*); // declared, but not defined
static No Test(... ); // declared, but not defined
public:
enum {
Is = sizeof(Test(static_cast<D*>(0))) == sizeof(Yes)
};
};
template<typename, typename>
struct is_same {
static bool const value = false;
};
template<typename A>
struct is_same<A, A> {
static bool const value = true;
};
template<bool C, typename T = void>
struct enable_if {
typedef T type;
};
template<typename T>
struct enable_if<false, T> { };
#endif /* ISDERIVEDFROM_H_ */

View File

@ -1,12 +1,11 @@
#ifndef FSFW_SERIALIZE_SERIALIZEADAPTER_H_ #ifndef _FSFW_SERIALIZE_SERIALIZEADAPTER_H_
#define FSFW_SERIALIZE_SERIALIZEADAPTER_H_ #define _FSFW_SERIALIZE_SERIALIZEADAPTER_H_
#include "../returnvalues/HasReturnvaluesIF.h"
#include "EndianConverter.h" #include "EndianConverter.h"
#include "SerializeIF.h" #include "SerializeIF.h"
#include <cstddef>
#include "../container/IsDerivedFrom.h" #include <type_traits>
#include "../returnvalues/HasReturnvaluesIF.h"
#include <cstring>
/** /**
* @brief These adapters provides an interface to use the SerializeIF functions * @brief These adapters provides an interface to use the SerializeIF functions
@ -21,27 +20,83 @@
*/ */
class SerializeAdapter { class SerializeAdapter {
public: public:
/***
* This function can be used to serialize a trivial copy-able type or a
* child of SerializeIF.
* The right template to be called is determined in the function itself.
* For objects of non trivial copy-able type this function is almost never
* called by the user directly. Instead helpers for specific types like
* SerialArrayListAdapter or SerialLinkedListAdapter is the right choice here.
*
* @param[in] object Object to serialize, the used type is deduced from this pointer
* @param[in/out] buffer Buffer to serialize into. Will be moved by the function.
* @param[in/out] size Size of current written buffer. Will be incremented by the function.
* @param[in] maxSize Max size of Buffer
* @param[in] streamEndianness Endianness of serialized element as in according to SerializeIF::Endianness
* @return
* - @c BUFFER_TOO_SHORT The given buffer in is too short
* - @c RETURN_FAILED Generic Error
* - @c RETURN_OK Successful serialization
*/
template<typename T> template<typename T>
static ReturnValue_t serialize(const T *object, uint8_t **buffer, static ReturnValue_t serialize(const T *object, uint8_t **buffer,
size_t *size, size_t maxSize, SerializeIF::Endianness streamEndianness) { size_t *size, size_t maxSize,
InternalSerializeAdapter<T, IsDerivedFrom<T, SerializeIF>::Is> adapter; SerializeIF::Endianness streamEndianness) {
InternalSerializeAdapter<T, std::is_base_of<SerializeIF, T>::value> adapter;
return adapter.serialize(object, buffer, size, maxSize, return adapter.serialize(object, buffer, size, maxSize,
streamEndianness); streamEndianness);
} }
/**
* Function to return the serialized size of the object in the pointer.
* May be a trivially copy-able object or a Child of SerializeIF
*
* @param object Pointer to Object
* @return Serialized size of object
*/
template<typename T> template<typename T>
static uint32_t getSerializedSize(const T *object) { static size_t getSerializedSize(const T *object){
InternalSerializeAdapter<T, IsDerivedFrom<T, SerializeIF>::Is> adapter; InternalSerializeAdapter<T, std::is_base_of<SerializeIF, T>::value> adapter;
return adapter.getSerializedSize(object); return adapter.getSerializedSize(object);
} }
/**
* @brief
* Deserializes a object from a given buffer of given size.
* Object Must be trivially copy-able or a child of SerializeIF.
*
* @details
* Buffer will be moved to the current read location. Size will be decreased by the function.
*
* @param[in/out] buffer Buffer to deSerialize from. Will be moved by the function.
* @param[in/out] size Remaining size of the buffer to read from. Will be decreased by function.
* @param[in] streamEndianness Endianness as in according to SerializeIF::Endianness
* @return
* - @c STREAM_TOO_SHORT The input stream is too short to deSerialize the object
* - @c TOO_MANY_ELEMENTS The buffer has more inputs than expected
* - @c RETURN_FAILED Generic Error
* - @c RETURN_OK Successful deserialization
*/
template<typename T> template<typename T>
static ReturnValue_t deSerialize(T *object, const uint8_t **buffer, static ReturnValue_t deSerialize(T *object, const uint8_t **buffer,
size_t *size, SerializeIF::Endianness streamEndianness) { size_t *size, SerializeIF::Endianness streamEndianness) {
InternalSerializeAdapter<T, IsDerivedFrom<T, SerializeIF>::Is> adapter; InternalSerializeAdapter<T, std::is_base_of<SerializeIF, T>::value> adapter;
return adapter.deSerialize(object, buffer, size, streamEndianness); return adapter.deSerialize(object, buffer, size, streamEndianness);
} }
private: private:
template<typename T, int> /**
class InternalSerializeAdapter { * Internal template to deduce the right function calls at compile time
*/
template<typename T, bool> class InternalSerializeAdapter;
/**
* Template to be used if T is not a child of SerializeIF
*
* @tparam T T must be trivially_copyable
*/
template<typename T>
class InternalSerializeAdapter<T, false> {
static_assert (std::is_trivially_copyable<T>::value,
"If a type needs to be serialized it must be a child of "
"SerializeIF or trivially copy-able");
public: public:
static ReturnValue_t serialize(const T *object, uint8_t **buffer, static ReturnValue_t serialize(const T *object, uint8_t **buffer,
size_t *size, size_t max_size, size_t *size, size_t max_size,
@ -50,8 +105,10 @@ private:
if (size == nullptr) { if (size == nullptr) {
size = &ignoredSize; size = &ignoredSize;
} }
//TODO check integer overflow of *size // Check remaining size is large enough and check integer
if (sizeof(T) + *size <= max_size) { // overflow of *size
size_t newSize = sizeof(T) + *size;
if ((newSize <= max_size) and (newSize > *size)) {
T tmp; T tmp;
switch (streamEndianness) { switch (streamEndianness) {
case SerializeIF::Endianness::BIG: case SerializeIF::Endianness::BIG:
@ -103,14 +160,18 @@ private:
uint32_t getSerializedSize(const T *object) { uint32_t getSerializedSize(const T *object) {
return sizeof(T); return sizeof(T);
} }
}; };
/**
* Template for objects that inherit from SerializeIF
*
* @tparam T A child of SerializeIF
*/
template<typename T> template<typename T>
class InternalSerializeAdapter<T, 1> { class InternalSerializeAdapter<T, true> {
public: public:
ReturnValue_t serialize(const T *object, uint8_t **buffer, ReturnValue_t serialize(const T *object, uint8_t **buffer, size_t *size,
size_t *size, size_t max_size, size_t max_size,
SerializeIF::Endianness streamEndianness) const { SerializeIF::Endianness streamEndianness) const {
size_t ignoredSize = 0; size_t ignoredSize = 0;
if (size == nullptr) { if (size == nullptr) {
@ -118,7 +179,7 @@ private:
} }
return object->serialize(buffer, size, max_size, streamEndianness); return object->serialize(buffer, size, max_size, streamEndianness);
} }
uint32_t getSerializedSize(const T *object) const { size_t getSerializedSize(const T *object) const {
return object->getSerializedSize(); return object->getSerializedSize();
} }
@ -129,4 +190,4 @@ private:
}; };
}; };
#endif /* SERIALIZEADAPTER_H_ */ #endif /* _FSFW_SERIALIZE_SERIALIZEADAPTER_H_ */

View File

@ -46,7 +46,7 @@ public:
* @param[in] maxSize The size of the buffer that is allowed to be used for serialize. * @param[in] maxSize The size of the buffer that is allowed to be used for serialize.
* @param[in] streamEndianness Endianness of the serialized data according to SerializeIF::Endianness * @param[in] streamEndianness Endianness of the serialized data according to SerializeIF::Endianness
* @return * @return
* - @¢ BUFFER_TOO_SHORT The given buffer in is too short * - @c BUFFER_TOO_SHORT The given buffer in is too short
* - @c RETURN_FAILED Generic error * - @c RETURN_FAILED Generic error
* - @c RETURN_OK Successful serialization * - @c RETURN_OK Successful serialization
*/ */

View File

@ -211,8 +211,7 @@ protected:
virtual void doPeriodicOperation(); virtual void doPeriodicOperation();
struct CommandInfo: public SerializeIF{
struct CommandInfo {
struct tcInfo { struct tcInfo {
uint8_t ackFlags; uint8_t ackFlags;
uint16_t tcPacketId; uint16_t tcPacketId;
@ -225,6 +224,20 @@ protected:
Command_t command; Command_t command;
object_id_t objectId; object_id_t objectId;
FIFO<store_address_t, 3> fifo; FIFO<store_address_t, 3> fifo;
virtual ReturnValue_t serialize(uint8_t **buffer, size_t *size,
size_t maxSize, Endianness streamEndianness) const override{
return HasReturnvaluesIF::RETURN_FAILED;
};
virtual size_t getSerializedSize() const override {
return 0;
};
virtual ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size,
Endianness streamEndianness) override{
return HasReturnvaluesIF::RETURN_FAILED;
};
}; };
using CommandMapIter = FixedMap<MessageQueueId_t, using CommandMapIter = FixedMap<MessageQueueId_t,