fsfw/src/fsfw/datapool/DatasetEntry.h

87 lines
2.0 KiB
C
Raw Normal View History

2023-07-18 13:17:34 +02:00
#pragma once
#include <fsfw/introspection/Parameter.h>
#include <fsfw/introspection/Types.h>
#include <fsfw/introspection/TypesHelper.h>
#include <stdio.h>
#include "Dataset.h"
#include "DatasetEntryIF.h"
// TODO: ifdef introspection stuff
template <typename T>
class DatasetEntry : public Parameter<T>, public DatasetEntryIF {
protected:
#ifdef FSFW_INTROSPECTION
DatasetEntry(Dataset *owner, const char *name)
: Parameter<T>(owner, name)
#else
DatasetEntry(Dataset *owner)
: Parameter<T>(owner)
#endif
{
2023-07-18 15:07:21 +02:00
allocated = owner->registerEntry(this);
if (!allocated) {
return;
}
storedValue = new T();
2023-07-21 14:23:55 +02:00
storedValid = new bool;
2023-07-18 13:17:34 +02:00
}
public:
#ifdef FSFW_INTROSPECTION
static DatasetEntry createDatasetEntry(Dataset *owner, const char *name) {
return DatasetEntry(owner, name);
}
#else
static DatasetEntry createDatasetEntry(Dataset *owner) { return DatasetEntry(owner); }
#endif
2023-07-18 15:07:21 +02:00
~DatasetEntry() {
if (allocated) {
delete storedValue;
delete storedValid;
}
}
2023-07-18 13:17:34 +02:00
virtual void setValid(bool isValid) { valid = isValid; }
virtual bool getValid() { return valid; }
2023-07-26 22:28:18 +02:00
//T value; //TODO can this be private?
2023-07-21 14:23:55 +02:00
2023-07-18 13:17:34 +02:00
protected:
2023-07-18 15:07:21 +02:00
virtual void commit() {
2023-07-26 22:28:18 +02:00
*storedValue = Parameter<T>::value;
2023-07-18 15:07:21 +02:00
*storedValid = valid;
}
virtual void read() {
2023-07-26 22:28:18 +02:00
Parameter<T>::value = *storedValue;
2023-07-18 15:07:21 +02:00
valid = *storedValid;
}
virtual void connect(DatasetEntryIF *entry) {
2023-07-21 14:23:55 +02:00
DatasetEntry<T> *theOther = dynamic_cast<DatasetEntry<T> *>(entry);
2023-07-18 15:07:21 +02:00
if (theOther == nullptr) {
// Configuration error
return;
}
this->storedValue = theOther->storedValue;
this->storedValid = theOther->storedValid;
}
2023-07-18 13:17:34 +02:00
2023-07-26 22:28:18 +02:00
virtual bool changed() { return ((Parameter<T>::value != *storedValue) || (valid != *storedValid)); }
2023-07-18 13:17:34 +02:00
private:
2023-07-21 14:23:55 +02:00
2023-07-18 15:07:21 +02:00
T *storedValue;
bool *storedValid;
2023-07-18 13:17:34 +02:00
bool valid;
2023-07-18 15:07:21 +02:00
bool allocated;
2023-07-18 13:17:34 +02:00
};
#ifdef FSFW_INTROSPECTION
#define createDatasetEntry(p1, p2) createDatasetEntry(p1, p2)
#else
#define createDatasetEntry(p1, p2) createDatasetEntry(p1)
#endif