FIFO hotfix

This commit is contained in:
Robin Müller 2020-07-11 11:52:01 +02:00
parent 9716bcdd74
commit 69946d5276
4 changed files with 41 additions and 8 deletions

View File

@ -17,11 +17,26 @@
template<typename T> template<typename T>
class DynamicFIFO: public FIFOBase<T> { class DynamicFIFO: public FIFOBase<T> {
public: public:
DynamicFIFO(size_t maxCapacity): FIFOBase<T>(values.data(), maxCapacity), DynamicFIFO(size_t maxCapacity): FIFOBase<T>(nullptr, maxCapacity),
values(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: private:
std::vector<T> values; std::vector<T> fifoVector;
}; };
#endif /* FRAMEWORK_CONTAINER_DYNAMICFIFO_H_ */ #endif /* FRAMEWORK_CONTAINER_DYNAMICFIFO_H_ */

View File

@ -17,10 +17,18 @@
template<typename T, size_t capacity> template<typename T, size_t capacity>
class FIFO: public FIFOBase<T> { class FIFO: public FIFOBase<T> {
public: 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: private:
std::array<T, capacity> values; std::array<T, capacity> fifoArray;
}; };
#endif /* FRAMEWORK_CONTAINERS_STATICFIFO_H_ */ #endif /* FRAMEWORK_CONTAINERS_STATICFIFO_H_ */

View File

@ -3,6 +3,7 @@
#include <framework/returnvalues/HasReturnvaluesIF.h> #include <framework/returnvalues/HasReturnvaluesIF.h>
#include <cstddef> #include <cstddef>
#include <cstring>
template <typename T> template <typename T>
class FIFOBase { class FIFOBase {
@ -43,12 +44,15 @@ public:
bool full(); bool full();
size_t size(); size_t size();
size_t getMaxCapacity() const; size_t getMaxCapacity() const;
private: protected:
T* values; void setData(T* data);
size_t maxCapacity = 0; size_t maxCapacity = 0;
T* values;
size_t readIndex = 0; size_t readIndex = 0;
size_t writeIndex = 0; size_t writeIndex = 0;
size_t currentSize = 0; size_t currentSize = 0;

View File

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