fsfw/src/fsfw/storagemanager/ConstStorageAccessor.cpp

91 lines
2.7 KiB
C++
Raw Normal View History

2021-07-13 20:22:54 +02:00
#include "fsfw/storagemanager/ConstStorageAccessor.h"
2020-09-29 14:35:05 +02:00
2021-01-03 14:33:17 +01:00
#include <algorithm>
2022-02-02 10:29:30 +01:00
#include "fsfw/globalfunctions/arrayprinter.h"
#include "fsfw/serviceinterface/ServiceInterfaceStream.h"
#include "fsfw/storagemanager/StorageManagerIF.h"
ConstStorageAccessor::ConstStorageAccessor(store_address_t storeId) : storeId(storeId) {}
2020-09-29 14:35:05 +02:00
2022-02-02 10:29:30 +01:00
ConstStorageAccessor::ConstStorageAccessor(store_address_t storeId, StorageManagerIF* store)
: storeId(storeId), store(store) {
internalState = AccessState::ASSIGNED;
2020-09-29 14:35:05 +02:00
}
ConstStorageAccessor::~ConstStorageAccessor() {
2022-02-02 10:29:30 +01:00
if (deleteData and store != nullptr) {
store->deleteData(storeId);
}
2020-09-29 14:35:05 +02:00
}
2022-09-15 10:30:22 +02:00
ConstStorageAccessor::ConstStorageAccessor(ConstStorageAccessor&& other) noexcept
2022-02-02 10:29:30 +01:00
: constDataPointer(other.constDataPointer),
storeId(other.storeId),
size_(other.size_),
store(other.store),
deleteData(other.deleteData),
internalState(other.internalState) {
// This prevent premature deletion
other.store = nullptr;
2020-09-29 14:35:05 +02:00
}
2022-09-15 10:30:22 +02:00
ConstStorageAccessor& ConstStorageAccessor::operator=(ConstStorageAccessor&& other) noexcept {
2022-02-02 10:29:30 +01:00
constDataPointer = other.constDataPointer;
storeId = other.storeId;
store = other.store;
size_ = other.size_;
deleteData = other.deleteData;
this->store = other.store;
// This prevents premature deletion
other.store = nullptr;
return *this;
2020-09-29 14:35:05 +02:00
}
2022-02-02 10:29:30 +01:00
const uint8_t* ConstStorageAccessor::data() const { return constDataPointer; }
2020-09-29 14:35:05 +02:00
size_t ConstStorageAccessor::size() const {
2022-02-02 10:29:30 +01:00
if (internalState == AccessState::UNINIT) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "StorageAccessor: Not initialized!" << std::endl;
#endif
2022-02-02 10:29:30 +01:00
}
return size_;
2020-09-29 14:35:05 +02:00
}
2022-02-02 10:29:30 +01:00
ReturnValue_t ConstStorageAccessor::getDataCopy(uint8_t* pointer, size_t maxSize) {
if (internalState == AccessState::UNINIT) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "StorageAccessor: Not initialized!" << std::endl;
#endif
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
if (size_ > maxSize) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "StorageAccessor: Supplied buffer not large enough" << std::endl;
#endif
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
std::copy(constDataPointer, constDataPointer + size_, pointer);
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2020-09-29 14:35:05 +02:00
}
2022-02-02 10:29:30 +01:00
void ConstStorageAccessor::release() { deleteData = false; }
2020-09-29 14:35:05 +02:00
2022-02-02 10:29:30 +01:00
store_address_t ConstStorageAccessor::getId() const { return storeId; }
2020-09-29 14:35:05 +02:00
void ConstStorageAccessor::print() const {
2022-02-02 10:29:30 +01:00
if (internalState == AccessState::UNINIT or constDataPointer == nullptr) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "StorageAccessor: Not initialized!" << std::endl;
#endif
2022-02-02 10:29:30 +01:00
return;
}
arrayprinter::print(constDataPointer, size_);
2020-09-29 14:35:05 +02:00
}
2022-09-15 10:30:22 +02:00
void ConstStorageAccessor::assignStore(StorageManagerIF* store_) {
2022-02-02 10:29:30 +01:00
internalState = AccessState::ASSIGNED;
2022-09-15 10:30:22 +02:00
store = store_;
2020-09-29 14:35:05 +02:00
}