fsfw/datapool/PoolEntry.cpp

102 lines
2.5 KiB
C++
Raw Permalink Normal View History

2020-08-13 20:53:35 +02:00
#include "PoolEntry.h"
2020-09-06 15:23:38 +02:00
2021-01-13 12:02:23 +01:00
#include "../serviceinterface/ServiceInterface.h"
2020-08-13 20:53:35 +02:00
#include "../globalfunctions/arrayprinter.h"
2020-06-05 13:43:06 +02:00
#include <cstring>
2021-01-03 14:08:40 +01:00
#include <algorithm>
template <typename T>
2021-01-13 12:02:23 +01:00
PoolEntry<T>::PoolEntry(std::initializer_list<T> initValue, bool setValid ):
2021-03-07 01:35:55 +01:00
length(static_cast<uint8_t>(initValue.size())), valid(setValid) {
this->address = new T[this->length];
2020-05-08 14:38:10 +02:00
if(initValue.size() == 0) {
2020-06-05 13:43:06 +02:00
std::memset(this->address, 0, this->getByteSize());
}
2020-05-08 14:38:10 +02:00
else {
2020-06-05 13:43:06 +02:00
std::copy(initValue.begin(), initValue.end(), this->address);
2020-05-08 14:38:10 +02:00
}
}
template <typename T>
2021-01-13 12:02:23 +01:00
PoolEntry<T>::PoolEntry(T* initValue, uint8_t setLength, bool setValid):
2020-06-05 13:43:06 +02:00
length(setLength), valid(setValid) {
2020-05-08 14:38:10 +02:00
this->address = new T[this->length];
if (initValue != nullptr) {
2020-06-05 13:43:06 +02:00
std::memcpy(this->address, initValue, this->getByteSize() );
} else {
2020-06-05 13:43:06 +02:00
std::memset(this->address, 0, this->getByteSize() );
}
}
//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() {
delete[] this->address;
}
template <typename T>
uint16_t PoolEntry<T>::getByteSize() {
return ( sizeof(T) * this->length );
}
template <typename T>
uint8_t PoolEntry<T>::getSize() {
return this->length;
}
template <typename T>
void* PoolEntry<T>::getRawData() {
return this->address;
}
template <typename T>
2020-06-05 13:43:06 +02:00
void PoolEntry<T>::setValid(bool isValid) {
this->valid = isValid;
}
template <typename T>
2020-06-05 13:43:06 +02:00
bool PoolEntry<T>::getValid() {
return valid;
}
template <typename T>
void PoolEntry<T>::print() {
2021-01-13 12:02:23 +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
2021-01-13 12:02:23 +01:00
sif::info << "PoolEntry information." << std::endl;
sif::info << "PoolEntry validity: " << validString << std::endl;
#else
sif::printInfo("PoolEntry information.\n");
sif::printInfo("PoolEntry validity: %s\n", validString);
#endif
2021-01-13 12:02:23 +01:00
arrayprinter::print(reinterpret_cast<uint8_t*>(address), getByteSize());
}
template<typename T>
inline T* PoolEntry<T>::getDataPtr() {
return this->address;
}
template<typename T>
Type PoolEntry<T>::getType() {
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>;