fsfw/datapoollocal/LocalPoolVector.tpp

179 lines
6.1 KiB
C++
Raw Normal View History

2020-12-03 13:00:04 +01:00
#ifndef FSFW_DATAPOOLLOCAL_LOCALPOOLVECTOR_TPP_
#define FSFW_DATAPOOLLOCAL_LOCALPOOLVECTOR_TPP_
2020-10-01 12:05:24 +02:00
2020-12-03 13:00:04 +01:00
#ifndef FSFW_DATAPOOLLOCAL_LOCALPOOLVECTOR_H_
2020-10-01 12:05:24 +02:00
#error Include LocalPoolVector.h before LocalPoolVector.tpp!
#endif
template<typename T, uint16_t vectorSize>
2020-12-03 13:00:04 +01:00
inline LocalPoolVector<T, vectorSize>::LocalPoolVector(
HasLocalDataPoolIF* hkOwner, lp_id_t poolId, DataSetIF* dataSet,
2020-10-01 12:05:24 +02:00
pool_rwm_t setReadWriteMode):
2020-12-03 13:00:04 +01:00
LocalPoolObjectBase(poolId, hkOwner, dataSet, setReadWriteMode) {}
2020-10-01 12:05:24 +02:00
template<typename T, uint16_t vectorSize>
2020-12-03 13:00:04 +01:00
inline LocalPoolVector<T, vectorSize>::LocalPoolVector(object_id_t poolOwner,
lp_id_t poolId, DataSetIF *dataSet, pool_rwm_t setReadWriteMode):
LocalPoolObjectBase(poolOwner, poolId, dataSet, setReadWriteMode) {}
template<typename T, uint16_t vectorSize>
inline LocalPoolVector<T, vectorSize>::LocalPoolVector(gp_id_t globalPoolId,
DataSetIF *dataSet, pool_rwm_t setReadWriteMode):
LocalPoolObjectBase(globalPoolId.objectId, globalPoolId.localPoolId,
dataSet, setReadWriteMode) {}
2020-10-01 12:05:24 +02:00
template<typename T, uint16_t vectorSize>
inline ReturnValue_t LocalPoolVector<T, vectorSize>::read(
MutexIF::TimeoutType timeoutType, uint32_t timeoutMs) {
MutexHelper(hkManager->getMutexHandle(), timeoutType, timeoutMs);
2020-10-01 12:05:24 +02:00
return readWithoutLock();
}
template<typename T, uint16_t vectorSize>
inline ReturnValue_t LocalPoolVector<T, vectorSize>::readWithoutLock() {
if(readWriteMode == pool_rwm_t::VAR_WRITE) {
2021-01-07 20:28:03 +01:00
object_id_t targetObjectId = hkManager->getOwner()->getObjectId();
reportReadCommitError("LocalPoolVector",
PoolVariableIF::INVALID_READ_WRITE_MODE, true, targetObjectId,
localPoolId);
2020-10-01 12:05:24 +02:00
return PoolVariableIF::INVALID_READ_WRITE_MODE;
}
PoolEntry<T>* poolEntry = nullptr;
ReturnValue_t result = hkManager->fetchPoolEntry(localPoolId, &poolEntry);
memset(this->value, 0, vectorSize * sizeof(T));
if(result != RETURN_OK) {
2021-01-07 20:23:23 +01:00
object_id_t targetObjectId = hkManager->getOwner()->getObjectId();
reportReadCommitError("LocalPoolVector", result, true, targetObjectId,
localPoolId);
2020-10-01 12:05:24 +02:00
return result;
}
2021-01-08 13:40:13 +01:00
std::memcpy(this->value, poolEntry->getDataPtr(), poolEntry->getByteSize());
this->valid = poolEntry->getValid();
2020-10-01 12:05:24 +02:00
return RETURN_OK;
}
2021-01-08 00:10:10 +01:00
template<typename T, uint16_t vectorSize>
inline ReturnValue_t LocalPoolVector<T, vectorSize>::commit(bool valid,
MutexIF::TimeoutType timeoutType, uint32_t timeoutMs) {
this->setValid(valid);
return commit(timeoutType, timeoutMs);
}
2020-10-01 12:05:24 +02:00
template<typename T, uint16_t vectorSize>
inline ReturnValue_t LocalPoolVector<T, vectorSize>::commit(
MutexIF::TimeoutType timeoutType, uint32_t timeoutMs) {
MutexHelper(hkManager->getMutexHandle(), timeoutType, timeoutMs);
2020-10-01 12:05:24 +02:00
return commitWithoutLock();
}
template<typename T, uint16_t vectorSize>
inline ReturnValue_t LocalPoolVector<T, vectorSize>::commitWithoutLock() {
if(readWriteMode == pool_rwm_t::VAR_READ) {
2021-01-07 20:28:03 +01:00
object_id_t targetObjectId = hkManager->getOwner()->getObjectId();
reportReadCommitError("LocalPoolVector",
PoolVariableIF::INVALID_READ_WRITE_MODE, false, targetObjectId,
localPoolId);
2020-10-01 12:05:24 +02:00
return PoolVariableIF::INVALID_READ_WRITE_MODE;
}
PoolEntry<T>* poolEntry = nullptr;
ReturnValue_t result = hkManager->fetchPoolEntry(localPoolId, &poolEntry);
if(result != RETURN_OK) {
2021-01-07 20:23:23 +01:00
object_id_t targetObjectId = hkManager->getOwner()->getObjectId();
2021-01-07 20:28:03 +01:00
reportReadCommitError("LocalPoolVector", result, false, targetObjectId,
2021-01-07 20:23:23 +01:00
localPoolId);
2020-10-01 12:05:24 +02:00
return result;
}
2021-01-08 13:40:13 +01:00
std::memcpy(poolEntry->getDataPtr(), this->value, poolEntry->getByteSize());
poolEntry->setValid(this->valid);
2020-10-01 12:05:24 +02:00
return RETURN_OK;
}
template<typename T, uint16_t vectorSize>
2021-01-08 01:49:00 +01:00
inline T& LocalPoolVector<T, vectorSize>::operator [](size_t i) {
if(i < vectorSize) {
2020-10-01 12:05:24 +02:00
return value[i];
}
// If this happens, I have to set some value. I consider this
// a configuration error, but I wont exit here.
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "LocalPoolVector: Invalid index. Setting or returning"
2020-10-01 12:05:24 +02:00
" last value!" << std::endl;
2021-01-08 01:49:00 +01:00
#else
sif::printWarning("LocalPoolVector: Invalid index. Setting or returning"
" last value!\n");
#endif
return value[vectorSize - 1];
2020-10-01 12:05:24 +02:00
}
template<typename T, uint16_t vectorSize>
2021-01-08 01:49:00 +01:00
inline const T& LocalPoolVector<T, vectorSize>::operator [](size_t i) const {
if(i < vectorSize) {
2020-10-01 12:05:24 +02:00
return value[i];
}
// If this happens, I have to set some value. I consider this
// a configuration error, but I wont exit here.
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "LocalPoolVector: Invalid index. Setting or returning"
2020-10-01 12:05:24 +02:00
" last value!" << std::endl;
2021-01-08 01:49:00 +01:00
#else
sif::printWarning("LocalPoolVector: Invalid index. Setting or returning"
" last value!\n");
#endif
return value[vectorSize - 1];
2020-10-01 12:05:24 +02:00
}
template<typename T, uint16_t vectorSize>
inline ReturnValue_t LocalPoolVector<T, vectorSize>::serialize(uint8_t** buffer,
size_t* size, size_t maxSize,
SerializeIF::Endianness streamEndianness) const {
ReturnValue_t result = HasReturnvaluesIF::RETURN_FAILED;
for (uint16_t i = 0; i < vectorSize; i++) {
result = SerializeAdapter::serialize(&(value[i]), buffer, size,
maxSize, streamEndianness);
if (result != HasReturnvaluesIF::RETURN_OK) {
break;
}
}
return result;
}
template<typename T, uint16_t vectorSize>
inline size_t LocalPoolVector<T, vectorSize>::getSerializedSize() const {
return vectorSize * SerializeAdapter::getSerializedSize(value);
}
template<typename T, uint16_t vectorSize>
inline ReturnValue_t LocalPoolVector<T, vectorSize>::deSerialize(
const uint8_t** buffer, size_t* size,
SerializeIF::Endianness streamEndianness) {
ReturnValue_t result = HasReturnvaluesIF::RETURN_FAILED;
for (uint16_t i = 0; i < vectorSize; i++) {
result = SerializeAdapter::deSerialize(&(value[i]), buffer, size,
streamEndianness);
if (result != HasReturnvaluesIF::RETURN_OK) {
break;
}
}
return result;
}
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-10-01 12:05:24 +02:00
template<typename T, uint16_t vectorSize>
inline std::ostream& operator<< (std::ostream &out,
const LocalPoolVector<T, vectorSize> &var) {
out << "Vector: [";
for(int i = 0;i < vectorSize; i++) {
out << var.value[i];
if(i < vectorSize - 1) {
out << ", ";
}
}
out << "]";
return out;
}
#endif
2020-10-01 12:05:24 +02:00
2020-12-03 13:00:04 +01:00
#endif /* FSFW_DATAPOOLLOCAL_LOCALPOOLVECTOR_TPP_ */