From 8ef686cb74eec4528c8ce51a4699d43721178742 Mon Sep 17 00:00:00 2001
From: Grigoris Pavlakis <grigpavl@ece.auth.gr>
Date: Fri, 30 Aug 2019 12:57:29 +0300
Subject: [PATCH] Convert Parameter class to template

---
 inc/Services/Parameter.hpp | 11 +++++------
 src/Services/Parameter.cpp | 20 +++++++++++++-------
 2 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/inc/Services/Parameter.hpp b/inc/Services/Parameter.hpp
index 54a40bb1..12f8f1bb 100644
--- a/inc/Services/Parameter.hpp
+++ b/inc/Services/Parameter.hpp
@@ -23,8 +23,7 @@
  * @typedef Flags: container for the binary flags
  */
 typedef uint16_t ParamId;
-typedef uint32_t ValueType;
-typedef void(*UpdatePtr)(ValueType*);
+//typedef etl::variant<bool, uint8_t, int32_t, float> ValueType;
 typedef etl::bitset<NUM_OF_FLAGS> Flags;
 
 /**
@@ -55,17 +54,17 @@ typedef etl::bitset<NUM_OF_FLAGS> Flags;
  * @public getCurrentValue(): Gets the current value of the parameter
  * @public getPTC(), getPFC(): Returns the PFC and PTC of the parameter
  */
-class Parameter {
+template <typename ValueType> class Parameter {
 	uint8_t ptc;
 	uint8_t pfc;
-	UpdatePtr ptr;
+	void(*ptr)(ValueType*);
 	Flags flags;
 	ValueType currentValue = 0;
 
 	public:
-		Parameter(uint8_t newPtc, uint8_t newPfc, uint32_t initialValue = 0, UpdatePtr newPtr = nullptr);
+		Parameter<ValueType>(uint8_t newPtc, uint8_t newPfc, const ValueType& initialValue, void(*newPtr)(ValueType*));
 
-		void setCurrentValue(ValueType newVal);
+		void setCurrentValue(const ValueType& newVal);
 		void setFlags(const char* flags);
 
 		ValueType getCurrentValue();
diff --git a/src/Services/Parameter.cpp b/src/Services/Parameter.cpp
index 2ab92271..9a96fe4e 100644
--- a/src/Services/Parameter.cpp
+++ b/src/Services/Parameter.cpp
@@ -1,6 +1,7 @@
 #include "Services/Parameter.hpp"
 
-Parameter::Parameter(uint8_t newPtc, uint8_t newPfc, ValueType initialValue, UpdatePtr newPtr) {
+template <typename ValueType>
+Parameter<ValueType>::Parameter(uint8_t newPtc, uint8_t newPfc, const ValueType& initialValue, void(*newPtr)(ValueType*)) {
 	ptc = newPtc;
 	pfc = newPfc;
 	ptr = newPtr;
@@ -15,25 +16,30 @@ Parameter::Parameter(uint8_t newPtc, uint8_t newPfc, ValueType initialValue, Upd
 	}
 }
 
-void Parameter::setCurrentValue(ValueType newVal) {
+template <typename ValueType>
+void Parameter<ValueType>::setCurrentValue(const ValueType& newVal) {
 	// set the value only if the parameter can be updated manually
 	if (flags[1]) {
 		currentValue = newVal;
 	}
 }
 
-ValueType Parameter::getCurrentValue() {
+template <typename ValueType>
+ValueType Parameter<ValueType>::getCurrentValue() {
 	return currentValue;
 }
-
-uint8_t Parameter::getPTC() {
+template <typename ValueType>
+uint8_t Parameter<ValueType>::getPTC() {
 	return ptc;
 }
 
-uint8_t Parameter::getPFC() {
+template <typename ValueType>
+uint8_t Parameter<ValueType>::getPFC() {
 	return pfc;
 }
 
-void Parameter::setFlags(const char* flags) {
+template <typename ValueType>
+void Parameter<ValueType>::setFlags(const char* flags) {
 	this->flags = Flags(flags);
 }
+
-- 
GitLab