fsfw/src/fsfw/datapool/PoolEntry.cpp

103 lines
2.6 KiB
C++
Raw Normal View History

2021-07-13 20:22:54 +02:00
#include "fsfw/datapool/PoolEntry.h"
2021-01-03 14:08:40 +01:00
#include <algorithm>
2022-02-02 10:29:30 +01:00
#include <cstring>
#include "fsfw/globalfunctions/arrayprinter.h"
#include "fsfw/serviceinterface/ServiceInterface.h"
template <typename T>
PoolEntry<T>::PoolEntry(uint8_t len, bool setValid) : length(len), valid(setValid) {
this->address = new T[this->length]();
std::memset(this->address, 0, this->getByteSize());
}
template <typename T>
PoolEntry<T>::PoolEntry(std::initializer_list<T> initValues, bool setValid)
: length(static_cast<uint8_t>(initValues.size())), valid(setValid) {
this->address = new T[this->length]();
if (initValues.size() > 0) {
std::copy(initValues.begin(), initValues.end(), this->address);
2022-02-02 10:29:30 +01:00
}
2020-05-08 14:38:10 +02:00
}
template <typename T>
PoolEntry<T>::PoolEntry(const T* initValue, uint8_t setLength, bool setValid)
2022-02-02 10:29:30 +01:00
: length(setLength), valid(setValid) {
this->address = new T[this->length]();
2022-02-02 10:29:30 +01:00
if (initValue != nullptr) {
std::memcpy(this->address, initValue, this->getByteSize());
}
}
2022-02-02 10:29:30 +01:00
// As the data pool is global, this dtor is only be called on program exit.
// Warning! Never copy pool entries!
template <typename T>
PoolEntry<T>::~PoolEntry() {
2022-02-02 10:29:30 +01:00
delete[] this->address;
}
template <typename T>
uint16_t PoolEntry<T>::getByteSize() {
2022-02-02 10:29:30 +01:00
return (sizeof(T) * this->length);
}
template <typename T>
uint8_t PoolEntry<T>::getSize() {
2022-02-02 10:29:30 +01:00
return this->length;
}
template <typename T>
void* PoolEntry<T>::getRawData() {
2022-02-02 10:29:30 +01:00
return this->address;
}
template <typename T>
2020-06-05 13:43:06 +02:00
void PoolEntry<T>::setValid(bool isValid) {
2022-02-02 10:29:30 +01:00
this->valid = isValid;
}
template <typename T>
2020-06-05 13:43:06 +02:00
bool PoolEntry<T>::getValid() {
2022-02-02 10:29:30 +01:00
return valid;
}
template <typename T>
void PoolEntry<T>::print() {
2022-02-02 10:29:30 +01:00
const char* validString = nullptr;
if (valid) {
validString = "Valid";
} else {
validString = "Invalid";
}
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::info << "PoolEntry information." << std::endl;
sif::info << "PoolEntry validity: " << validString << std::endl;
2021-01-13 12:02:23 +01:00
#else
2022-02-02 10:29:30 +01:00
sif::printInfo("PoolEntry information.\n");
sif::printInfo("PoolEntry validity: %s\n", validString);
#endif
2022-02-02 10:29:30 +01:00
arrayprinter::print(reinterpret_cast<uint8_t*>(address), getByteSize());
2021-01-13 12:02:23 +01:00
}
2022-02-02 10:29:30 +01:00
template <typename T>
2021-01-13 12:02:23 +01:00
inline T* PoolEntry<T>::getDataPtr() {
2022-02-02 10:29:30 +01:00
return this->address;
}
2022-02-02 10:29:30 +01:00
template <typename T>
Type PoolEntry<T>::getType() {
2022-02-02 10:29:30 +01:00
return PodTypeConversion<T>::type;
}
template class PoolEntry<uint8_t>;
template class PoolEntry<uint16_t>;
template class PoolEntry<uint32_t>;
2021-01-13 12:02:23 +01:00
template class PoolEntry<uint64_t>;
template class PoolEntry<int8_t>;
template class PoolEntry<int16_t>;
template class PoolEntry<int32_t>;
2021-01-13 12:02:23 +01:00
template class PoolEntry<int64_t>;
template class PoolEntry<float>;
template class PoolEntry<double>;