fsfw/src/fsfw/datapool/DatasetEntry.h

87 lines
2.0 KiB
C++

#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
{
allocated = owner->registerEntry(this);
if (!allocated) {
return;
}
storedValue = new T();
storedValid = new bool;
}
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
~DatasetEntry() {
if (allocated) {
delete storedValue;
delete storedValid;
}
}
virtual void setValid(bool isValid) { valid = isValid; }
virtual bool getValid() { return valid; }
//T value; //TODO can this be private?
protected:
virtual void commit() {
*storedValue = Parameter<T>::value;
*storedValid = valid;
}
virtual void read() {
Parameter<T>::value = *storedValue;
valid = *storedValid;
}
virtual void connect(DatasetEntryIF *entry) {
DatasetEntry<T> *theOther = dynamic_cast<DatasetEntry<T> *>(entry);
if (theOther == nullptr) {
// Configuration error
return;
}
this->storedValue = theOther->storedValue;
this->storedValid = theOther->storedValid;
}
virtual bool changed() { return ((Parameter<T>::value != *storedValue) || (valid != *storedValid)); }
private:
T *storedValue;
bool *storedValid;
bool valid;
bool allocated;
};
#ifdef FSFW_INTROSPECTION
#define createDatasetEntry(p1, p2) createDatasetEntry(p1, p2)
#else
#define createDatasetEntry(p1, p2) createDatasetEntry(p1)
#endif