From 6326bd5575b10eb0dd8bb86d9590131906645952 Mon Sep 17 00:00:00 2001 From: forntoh Date: Wed, 23 Oct 2024 16:59:52 +0200 Subject: [PATCH] Refactor widget range creation functions to use templates Updated the creation functions for WidgetRange instances to utilize templates for flexibility and code reusability. Removed redundant specific type functions. --- src/widget/WidgetRange.h | 70 +++++++--------------------------------- 1 file changed, 12 insertions(+), 58 deletions(-) diff --git a/src/widget/WidgetRange.h b/src/widget/WidgetRange.h index c7ba9cd9..9e8117fd 100644 --- a/src/widget/WidgetRange.h +++ b/src/widget/WidgetRange.h @@ -97,75 +97,29 @@ class WidgetRange : public BaseWidgetValue { }; /** - * @brief Function to create a new WidgetRange instance. + * @brief Function to create a new WidgetRange instance. + * @tparam T The type of the value. * * @param value The initial value of the widget. * @param step The step value for incrementing/decrementing. * @param min The minimum value of the range. * @param max The maximum value of the range. - * @param format The format string for displaying the value (default is "%2.1f"). + * @param format The format string for displaying the value. * @param cursorOffset The offset for the cursor (default is 0). * @param cycle Whether the value should cycle when out of range (default is false). * @param callback The callback function to call when the value changes (default is nullptr). */ -inline BaseWidgetValue* WIDGET_FLOAT_RANGE( - float value, - float step, - float min, - float max, - const char* format = "%2.1f", - uint8_t cursorOffset = 0, - bool cycle = false, - void (*callback)(const float&) = nullptr) { - return new WidgetRange(value, step, min, max, format, cursorOffset, cycle, callback); -} - -/** - * @brief Function to create a new WidgetRange instance. - * - * @param value The initial value of the widget. - * @param step The step value for incrementing/decrementing. - * @param min The minimum value of the range. - * @param max The maximum value of the range. - * @param format The format string for displaying the value (default is "%d"). - * @param cursorOffset The offset for the cursor (default is 0). - * @param cycle Whether the value should cycle when out of range (default is false). - * @param callback The callback function to call when the value changes (default is nullptr). - */ -inline BaseWidgetValue* WIDGET_INT_RANGE( - int value, - int step, - int min, - int max, - const char* format = "%d", - uint8_t cursorOffset = 0, - bool cycle = false, - void (*callback)(const int&) = nullptr) { - return new WidgetRange(value, step, min, max, format, cursorOffset, cycle, callback); -} - -/** - * @brief Function to create a new WidgetRange instance. - * - * @param value The initial value of the widget. - * @param step The step value for incrementing/decrementing. - * @param min The minimum value of the range. - * @param max The maximum value of the range. - * @param format The format string for displaying the value (default is "%d"). - * @param cursorOffset The offset for the cursor (default is 0). - * @param cycle Whether the value should cycle when out of range (default is false). - * @param callback The callback function to call when the value changes (default is nullptr). - */ -inline BaseWidgetValue* WIDGET_UINT8_RANGE( - uint8_t value, - uint8_t step, - uint8_t min, - uint8_t max, - const char* format = "%d", +template +inline BaseWidgetValue* WIDGET_RANGE( + T value, + T step, + T min, + T max, + const char* format, uint8_t cursorOffset = 0, bool cycle = false, - void (*callback)(const uint8_t&) = nullptr) { - return new WidgetRange(value, step, min, max, format, cursorOffset, cycle, callback); + void (*callback)(const T&) = nullptr) { + return new WidgetRange(value, step, min, max, format, cursorOffset, cycle, callback); } #endif \ No newline at end of file