From 0913798faf73ae0c9776dd12e5ef9edfcfcf98c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Thu, 27 Oct 2016 16:52:01 +0200 Subject: [PATCH] [apps] Replace magic numbers by constants Change-Id: If9cb40bdc9b1e88a941a6f230fb42164b1e2d5f0 --- apps/Makefile | 1 + apps/calculation/history_controller.cpp | 5 +++-- apps/calculation/history_view_cell.cpp | 5 +++-- apps/constant.cpp | 5 +++++ apps/constant.h | 12 ++++++++++++ apps/graph/values/values_controller.cpp | 13 +++++++------ apps/graph/values/values_parameter_controller.cpp | 11 ++++++----- escher/include/escher/buffer_text_view.h | 3 ++- 8 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 apps/constant.cpp create mode 100644 apps/constant.h diff --git a/apps/Makefile b/apps/Makefile index 9923f3e55..2a34edc13 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -6,6 +6,7 @@ include apps/calculation/Makefile app_objs += $(addprefix apps/,\ apps_container.o\ + constant.o\ main.o\ ) diff --git a/apps/calculation/history_controller.cpp b/apps/calculation/history_controller.cpp index ea98754cc..a0086ede8 100644 --- a/apps/calculation/history_controller.cpp +++ b/apps/calculation/history_controller.cpp @@ -1,5 +1,6 @@ #include "history_controller.h" #include "app.h" +#include "../constant.h" #include namespace Calculation { @@ -46,8 +47,8 @@ bool HistoryController::handleEvent(Ion::Events::Event event) { if (selectedSubview == HistoryViewCell::SelectedView::PrettyPrint) { editController->setTextBody(calculation->text()); } else { - char buffer[7]; - Float(calculation->evaluation()).convertFloatToText(buffer, 14, 7); + char buffer[Constant::FloatBufferSizeInScientificMode]; + Float(calculation->evaluation()).convertFloatToText(buffer, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode); editController->setTextBody(buffer); } m_selectableTableView.deselectTable(); diff --git a/apps/calculation/history_view_cell.cpp b/apps/calculation/history_view_cell.cpp index c6eba820d..462aa96b1 100644 --- a/apps/calculation/history_view_cell.cpp +++ b/apps/calculation/history_view_cell.cpp @@ -1,4 +1,5 @@ #include "history_view_cell.h" +#include "../constant.h" #include #include @@ -50,8 +51,8 @@ void HistoryViewCell::layoutSubviews() { void HistoryViewCell::setCalculation(Calculation * calculation) { m_prettyPrint.setExpression(calculation->layout()); - char buffer[7]; - Float(calculation->evaluation()).convertFloatToText(buffer, 14, 7); + char buffer[Constant::FloatBufferSizeInScientificMode]; + Float(calculation->evaluation()).convertFloatToText(buffer, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode); m_result.setText(buffer); } diff --git a/apps/constant.cpp b/apps/constant.cpp new file mode 100644 index 000000000..13635080c --- /dev/null +++ b/apps/constant.cpp @@ -0,0 +1,5 @@ +#include "constant.h" + +constexpr int Constant::FloatBufferSizeInScientificMode; +constexpr int Constant::NumberOfDigitsInMantissaInScientificMode; +constexpr int Constant::NumberOfDigitsInMantissaForDerivativeNumberInScientificMode; diff --git a/apps/constant.h b/apps/constant.h new file mode 100644 index 000000000..cd0ee182e --- /dev/null +++ b/apps/constant.h @@ -0,0 +1,12 @@ +#ifndef APPS_CONSTANT_H +#define APPS_CONSTANT_H + +class Constant { +public: + constexpr static int FloatBufferSizeInScientificMode = 14; + constexpr static int NumberOfDigitsInMantissaInScientificMode = 7; + constexpr static int NumberOfDigitsInMantissaForDerivativeNumberInScientificMode = 3; + +}; + +#endif diff --git a/apps/graph/values/values_controller.cpp b/apps/graph/values/values_controller.cpp index 6bec82d85..a4d803447 100644 --- a/apps/graph/values/values_controller.cpp +++ b/apps/graph/values/values_controller.cpp @@ -1,4 +1,5 @@ #include "values_controller.h" +#include "../../constant.h" #include "../app.h" #include @@ -264,7 +265,7 @@ void ValuesController::editValue(bool overwrite, char initialDigit) { /* This code assumes that the active cell remains the one which is edited * until the invocation is performed. This could lead to concurrency issue in * other cases. */ - char initialTextContent[16]; + char initialTextContent[Constant::FloatBufferSizeInScientificMode]; if (overwrite) { initialTextContent[0] = initialDigit; initialTextContent[1] = 0; @@ -272,7 +273,7 @@ void ValuesController::editValue(bool overwrite, char initialDigit) { if (activeRow() > m_interval.numberOfElements()) { initialTextContent[0] = 0; } else { - Float(m_interval.element(activeRow()-1)).convertFloatToText(initialTextContent, 14, 7); + Float(m_interval.element(activeRow()-1)).convertFloatToText(initialTextContent, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode); } } App * myApp = (App *)app(); @@ -363,7 +364,7 @@ void ValuesController::willDisplayCellAtLocation(TableViewCell * cell, int i, in } // The cell is a value cell: ValueCell * myValueCell = (ValueCell *)cell; - char buffer[14]; + char buffer[Constant::FloatBufferSizeInScientificMode]; // Special case 1: last row if (j == numberOfRows() - 1) { /* Display an empty line only if there is enough space for a new element in @@ -377,16 +378,16 @@ void ValuesController::willDisplayCellAtLocation(TableViewCell * cell, int i, in } // Special case 2: first column if (i == 0){ - Float(m_interval.element(j-1)).convertFloatToText(buffer, 14, 7); + Float(m_interval.element(j-1)).convertFloatToText(buffer, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode); myValueCell->setText(buffer); return; } Function * function = functionAtColumn(i); float x = m_interval.element(j-1); if (isDerivativeColumn(i)) { - Float(function->approximateDerivative(x, m_evaluateContext)).convertFloatToText(buffer, 14, 3); + Float(function->approximateDerivative(x, m_evaluateContext)).convertFloatToText(buffer, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaForDerivativeNumberInScientificMode); } else { - Float(function->evaluateAtAbscissa(x, m_evaluateContext)).convertFloatToText(buffer, 14, 7); + Float(function->evaluateAtAbscissa(x, m_evaluateContext)).convertFloatToText(buffer, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode); } myValueCell->setText(buffer); } diff --git a/apps/graph/values/values_parameter_controller.cpp b/apps/graph/values/values_parameter_controller.cpp index 156516fa9..818444877 100644 --- a/apps/graph/values/values_parameter_controller.cpp +++ b/apps/graph/values/values_parameter_controller.cpp @@ -1,5 +1,6 @@ #include "values_parameter_controller.h" #include "../app.h" +#include "../../constant.h" #include namespace Graph { @@ -38,18 +39,18 @@ int ValuesParameterController::activeCell() { void ValuesParameterController::willDisplayCellForIndex(TableViewCell * cell, int index) { TextMenuListCell * myCell = (TextMenuListCell *) cell; - char buffer[14]; + char buffer[Constant::FloatBufferSizeInScientificMode]; switch (index) { case 0: - Float(m_interval->start()).convertFloatToText(buffer, 14, 7); + Float(m_interval->start()).convertFloatToText(buffer, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode); myCell->setText(buffer); break; case 1: - Float(m_interval->end()).convertFloatToText(buffer, 14, 7); + Float(m_interval->end()).convertFloatToText(buffer, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode); myCell->setText(buffer); break; case 2: - Float(m_interval->step()).convertFloatToText(buffer, 14, 7); + Float(m_interval->step()).convertFloatToText(buffer, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode); myCell->setText(buffer); break; default: @@ -92,7 +93,7 @@ void ValuesParameterController::editInterval(bool overwrite, char initialDigit) /* This code assumes that the active cell remains the one which is edited * until the invocation is performed. This could lead to concurrency issue in * other cases. */ - char initialTextContent[16]; + char initialTextContent[Constant::FloatBufferSizeInScientificMode]; if (overwrite) { initialTextContent[0] = initialDigit; initialTextContent[1] = 0; diff --git a/escher/include/escher/buffer_text_view.h b/escher/include/escher/buffer_text_view.h index a72e300b3..85c9db3df 100644 --- a/escher/include/escher/buffer_text_view.h +++ b/escher/include/escher/buffer_text_view.h @@ -10,7 +10,8 @@ public: void setText(const char * text); const char * text() const override; private: - char m_buffer[16]; + static constexpr int k_maxNumberOfChar = 256; + char m_buffer[k_maxNumberOfChar]; }; #endif