dynamic fifo bug fixed

This commit is contained in:
Robin Müller 2020-07-11 02:36:04 +02:00
parent 6a6395313f
commit 35d4267b40
5 changed files with 44 additions and 28 deletions

View File

@ -17,11 +17,26 @@
template<typename T>
class DynamicFIFO: public FIFOBase<T> {
public:
DynamicFIFO(size_t maxCapacity): FIFOBase<T>(values.data(), maxCapacity),
values(maxCapacity) {};
DynamicFIFO(size_t maxCapacity): FIFOBase<T>(nullptr, maxCapacity),
fifoVector(maxCapacity) {
// trying to pass the pointer of the uninitialized vector
// to the FIFOBase constructor directly lead to a super evil bug.
// So we do it like this now.
this->setData(fifoVector.data());
};
/**
* @brief Custom copy constructor which prevents setting the
* underlying pointer wrong.
*/
DynamicFIFO(const DynamicFIFO& other): FIFOBase<T>(other),
fifoVector(other.maxCapacity) {
this->setData(fifoVector.data());
}
private:
std::vector<T> values;
std::vector<T> fifoVector;
};
#endif /* FRAMEWORK_CONTAINER_DYNAMICFIFO_H_ */

View File

@ -17,10 +17,18 @@
template<typename T, size_t capacity>
class FIFO: public FIFOBase<T> {
public:
FIFO(): FIFOBase<T>(values.data(), capacity) {};
FIFO(): FIFOBase<T>(fifoArray.data(), capacity) {};
/**
* @brief Custom copy constructor to set pointer correctly.
* @param other
*/
FIFO(const FIFO& other): FIFOBase<T>(other) {
this->setData(fifoArray.data());
}
private:
std::array<T, capacity> values;
std::array<T, capacity> fifoArray;
};
#endif /* FRAMEWORK_CONTAINERS_STATICFIFO_H_ */

View File

@ -44,29 +44,15 @@ public:
bool full();
size_t size();
FIFOBase(const FIFOBase& other): readIndex(other.readIndex),
writeIndex(other.writeIndex), currentSize(other.currentSize),
maxCapacity(other.maxCapacity) {
std::memcpy(values, other.values, sizeof(T) * currentSize);
}
FIFOBase& operator=(const FIFOBase& other) {
if(&other == this)
return *this;
maxCapacity = other.maxCapacity;
readIndex = other.readIndex;
writeIndex = other.writeIndex;
currentSize = other.currentSize;
std::memcpy(values, other.values, sizeof(T) * currentSize);
return *this;
}
size_t getMaxCapacity() const;
private:
T* values;
protected:
void setData(T* data);
size_t maxCapacity = 0;
T* values;
size_t readIndex = 0;
size_t writeIndex = 0;
size_t currentSize = 0;

View File

@ -7,7 +7,7 @@
template<typename T>
inline FIFOBase<T>::FIFOBase(T* values, const size_t maxCapacity):
values(values), maxCapacity(maxCapacity) {};
maxCapacity(maxCapacity), values(values){};
template<typename T>
inline ReturnValue_t FIFOBase<T>::insert(T value) {
@ -78,4 +78,10 @@ inline size_t FIFOBase<T>::getMaxCapacity() const {
return maxCapacity;
}
template<typename T>
inline void FIFOBase<T>::setData(T *data) {
this->values = data;
}
#endif

View File

@ -63,11 +63,12 @@ public:
* @return
*/
indexSizePair getNextFifoPair();
private:
//! A FIFO is used to store information about multiple PUS packets
//! inside the receive buffer. The maximum number of entries is defined
//! by the first constructor argument.
private:
/** A FIFO is used to store information about multiple PUS packets
* inside the receive buffer. The maximum number of entries is defined
* by the first constructor argument.
*/
DynamicFIFO<indexSizePair> indexSizePairFIFO;
bool storeSplitPackets = false;