From daf4247d42be8ed752f3e318fb24901459bb95da Mon Sep 17 00:00:00 2001 From: Grigoris Pavlakis <grigpavl@ece.auth.gr> Date: Sun, 15 Mar 2020 21:29:34 +0200 Subject: [PATCH] Convert ParameterBase to common virtual interface ParameterBase merely provides a virtual setter and getter, and also acts as a common type which will be used to store pointers to Parameter objects in an array, so as to enable ID-based parameter fetching and updating. Rewrite of documentation and of the rest of the service is underway. --- inc/Services/Parameter.hpp | 78 ++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 24 deletions(-) diff --git a/inc/Services/Parameter.hpp b/inc/Services/Parameter.hpp index 20b8e8c5..bc101b52 100644 --- a/inc/Services/Parameter.hpp +++ b/inc/Services/Parameter.hpp @@ -3,7 +3,6 @@ #include "etl/String.hpp" #include "ECSS_Definitions.hpp" -#include <iostream> /** * Implementation of a Parameter field, as specified in ECSS-E-ST-70-41C. * Fully compliant with the standard's requirements. @@ -51,44 +50,75 @@ typedef uint16_t ParamId; */ class ParameterBase { -protected: - uint8_t sizeInBytes; - void* valuePtr; public: - virtual String<ECSS_ST_20_MAX_STRING_LENGTH> getValueAsString() = 0; - - template <typename ValueType> - void setCurrentValue(ValueType newVal) { - *reinterpret_cast<ValueType*>(valuePtr) = newVal; - } + virtual void setValueFromMessage(Message message) = 0; }; -template <typename ValueType> +template <typename DataType> class Parameter : public ParameterBase { - void (* ptr)(ValueType*); - ValueType currentValue; + DataType currentValue; + void (* updateFunction)(DataType*); public: - Parameter(ValueType initialValue = 0, void(* newPtr)(ValueType*) = nullptr) { - ptr = newPtr; - sizeInBytes = sizeof(initialValue); - // previously null valuePtr now points to the currentValue field - valuePtr = static_cast<void*>(¤tValue); - - if (ptr != nullptr) { - (*ptr)(¤tValue); // call the update function for the initial value - } else { + Parameter(DataType initialValue, void (* updateFunction)(DataType*) = nullptr) { + this.updateFunction = updateFunction; + + if (this.updateFunction != nullptr) { + (*updateFunction)(¤tValue); + } + else { currentValue = initialValue; } } + void setValueFromMessage(Message message) override { + // TODO: implement setValueForMessage + } + String<ECSS_ST_20_MAX_STRING_LENGTH> getValueAsString() override { - String<ECSS_ST_20_MAX_STRING_LENGTH> contents(reinterpret_cast<uint8_t*>(¤tValue), sizeInBytes); - return contents; + // TODO: implement getValueAsString() + return String<ECSS_ST_20_MAX_STRING_LENGTH>("DUMMY STRING"); } }; +// class ParameterBase { +// public: + +// virtual String<ECSS_ST_20_MAX_STRING_LENGTH> getValueAsString() = 0; +// virtual void setValueFromMessage(Message message) = 0; + +// template <typename ValueType> +// void setCurrentValue(ValueType newVal) { +// *reinterpret_cast<ValueType*>(valuePtr) = newVal; +// } +// }; + +// template <typename ValueType> +// class Parameter : public ParameterBase { +// void (* ptr)(ValueType*); +// ValueType currentValue; + +// public: +// Parameter(ValueType initialValue = 0, void(* newPtr)(ValueType*) = nullptr) { +// ptr = newPtr; +// sizeInBytes = sizeof(initialValue); +// // previously null valuePtr now points to the currentValue field +// valuePtr = static_cast<void*>(¤tValue); + +// if (ptr != nullptr) { +// (*ptr)(¤tValue); // call the update function for the initial value +// } else { +// currentValue = initialValue; +// } +// } + +// String<ECSS_ST_20_MAX_STRING_LENGTH> getValueAsString() override { +// String<ECSS_ST_20_MAX_STRING_LENGTH> contents(reinterpret_cast<uint8_t*>(¤tValue), sizeInBytes); +// return contents; +// } +// }; + #endif //ECSS_SERVICES_PARAMETER_HPP -- GitLab