[apps] Replace magic numbers by constants

Change-Id: If9cb40bdc9b1e88a941a6f230fb42164b1e2d5f0
This commit is contained in:
Émilie Feral
2016-10-27 16:52:01 +02:00
parent d8179abd77
commit 0913798faf
8 changed files with 39 additions and 16 deletions

View File

@@ -6,6 +6,7 @@ include apps/calculation/Makefile
app_objs += $(addprefix apps/,\
apps_container.o\
constant.o\
main.o\
)

View File

@@ -1,5 +1,6 @@
#include "history_controller.h"
#include "app.h"
#include "../constant.h"
#include <assert.h>
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();

View File

@@ -1,4 +1,5 @@
#include "history_view_cell.h"
#include "../constant.h"
#include <assert.h>
#include <string.h>
@@ -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);
}

5
apps/constant.cpp Normal file
View File

@@ -0,0 +1,5 @@
#include "constant.h"
constexpr int Constant::FloatBufferSizeInScientificMode;
constexpr int Constant::NumberOfDigitsInMantissaInScientificMode;
constexpr int Constant::NumberOfDigitsInMantissaForDerivativeNumberInScientificMode;

12
apps/constant.h Normal file
View File

@@ -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

View File

@@ -1,4 +1,5 @@
#include "values_controller.h"
#include "../../constant.h"
#include "../app.h"
#include <assert.h>
@@ -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);
}

View File

@@ -1,5 +1,6 @@
#include "values_parameter_controller.h"
#include "../app.h"
#include "../../constant.h"
#include <assert.h>
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;

View File

@@ -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