fsfw/container/FIFOBase.tpp

94 lines
2.0 KiB
C++
Raw Normal View History

2020-08-27 20:19:27 +02:00
#ifndef FSFW_CONTAINER_FIFOBASE_TPP_
#define FSFW_CONTAINER_FIFOBASE_TPP_
2020-07-06 13:26:58 +02:00
2020-08-27 20:19:27 +02:00
#ifndef FSFW_CONTAINER_FIFOBASE_H_
2020-07-06 13:26:58 +02:00
#error Include FIFOBase.h before FIFOBase.tpp!
#endif
template<typename T>
inline FIFOBase<T>::FIFOBase(T* values, const size_t maxCapacity):
2021-01-28 11:44:49 +01:00
maxCapacity(maxCapacity), values(values){};
2020-07-06 13:26:58 +02:00
template<typename T>
inline ReturnValue_t FIFOBase<T>::insert(T value) {
2021-01-28 11:44:49 +01:00
if (full()) {
return FULL;
} else {
values[writeIndex] = value;
writeIndex = next(writeIndex);
++currentSize;
return HasReturnvaluesIF::RETURN_OK;
}
2020-07-06 13:26:58 +02:00
};
template<typename T>
inline ReturnValue_t FIFOBase<T>::retrieve(T* value) {
2021-01-28 11:44:49 +01:00
if (empty()) {
return EMPTY;
} else {
if (value == nullptr){
return HasReturnvaluesIF::RETURN_FAILED;
}
*value = values[readIndex];
readIndex = next(readIndex);
--currentSize;
return HasReturnvaluesIF::RETURN_OK;
}
2020-07-06 13:26:58 +02:00
};
template<typename T>
inline ReturnValue_t FIFOBase<T>::peek(T* value) {
2021-01-28 11:44:49 +01:00
if(empty()) {
return EMPTY;
} else {
if (value == nullptr){
return HasReturnvaluesIF::RETURN_FAILED;
}
*value = values[readIndex];
return HasReturnvaluesIF::RETURN_OK;
}
2020-07-06 13:26:58 +02:00
};
template<typename T>
inline ReturnValue_t FIFOBase<T>::pop() {
2021-01-28 11:44:49 +01:00
T value;
return this->retrieve(&value);
2020-07-06 13:26:58 +02:00
};
template<typename T>
inline bool FIFOBase<T>::empty() {
2021-01-28 11:44:49 +01:00
return (currentSize == 0);
2020-07-06 13:26:58 +02:00
};
template<typename T>
inline bool FIFOBase<T>::full() {
2021-01-28 11:44:49 +01:00
return (currentSize == maxCapacity);
2020-07-06 13:26:58 +02:00
}
template<typename T>
inline size_t FIFOBase<T>::size() {
2021-01-28 11:44:49 +01:00
return currentSize;
2020-07-06 13:26:58 +02:00
}
template<typename T>
inline size_t FIFOBase<T>::next(size_t current) {
2021-01-28 11:44:49 +01:00
++current;
if (current == maxCapacity) {
current = 0;
}
return current;
2020-07-06 13:26:58 +02:00
}
2020-07-06 23:08:31 +02:00
template<typename T>
inline size_t FIFOBase<T>::getMaxCapacity() const {
2021-01-28 11:44:49 +01:00
return maxCapacity;
2020-07-06 23:08:31 +02:00
}
2020-07-28 13:13:40 +02:00
template<typename T>
2020-09-01 12:53:53 +02:00
inline void FIFOBase<T>::setContainer(T *data) {
2021-01-28 11:44:49 +01:00
this->values = data;
2020-07-28 13:13:40 +02:00
}
2020-07-06 13:26:58 +02:00
#endif