[apps, poincare] Optimize the preferences singletons' usage by removing superfluous checks in the setters, and performing manual CSE in the callers.

Signed-off-by: Lionel Debroux <lionel_debroux@yahoo.fr>
This commit is contained in:
Lionel Debroux
2018-10-19 23:01:16 +02:00
committed by EmilieNumworks
parent ddebc06fa5
commit 70a8d06cfe
10 changed files with 50 additions and 49 deletions

View File

@@ -114,8 +114,9 @@ VariableBoxController * AppsContainer::variableBoxController() {
void AppsContainer::suspend(bool checkIfPowerKeyReleased) {
resetShiftAlphaStatus();
GlobalPreferences * globalPreferences = GlobalPreferences::sharedGlobalPreferences();
#ifdef EPSILON_BOOT_PROMPT
if (activeApp()->snapshot()!= onBoardingAppSnapshot() && activeApp()->snapshot() != hardwareTestAppSnapshot() && GlobalPreferences::sharedGlobalPreferences()->showPopUp()) {
if (activeApp()->snapshot()!= onBoardingAppSnapshot() && activeApp()->snapshot() != hardwareTestAppSnapshot() && globalPreferences->showPopUp()) {
activeApp()->displayModalViewController(&m_promptController, 0.f, 0.f);
}
#endif
@@ -123,7 +124,7 @@ void AppsContainer::suspend(bool checkIfPowerKeyReleased) {
/* Ion::Power::suspend() completely shuts down the LCD controller. Therefore
* the frame memory is lost. That's why we need to force a window redraw
* upon wakeup, otherwise the screen is filled with noise. */
Ion::Backlight::setBrightness(GlobalPreferences::sharedGlobalPreferences()->brightnessLevel());
Ion::Backlight::setBrightness(globalPreferences->brightnessLevel());
m_backlightDimmingTimer.reset();
window()->redraw(true);
}

View File

@@ -144,7 +144,8 @@ Calculation::EqualSign Calculation::exactAndApproximateDisplayedOutputsAreEqual(
return m_equalSign;
}
char buffer[k_printedExpressionSize];
m_equalSign = exactOutput(context).isEqualToItsApproximationLayout(approximateOutput(context), buffer, k_printedExpressionSize, Preferences::sharedPreferences()->angleUnit(), Preferences::sharedPreferences()->displayMode(), Preferences::sharedPreferences()->numberOfSignificantDigits(), *context) ? EqualSign::Equal : EqualSign::Approximation;
Preferences * preferences = Preferences::sharedPreferences();
m_equalSign = exactOutput(context).isEqualToItsApproximationLayout(approximateOutput(context), buffer, k_printedExpressionSize, preferences->angleUnit(), preferences->displayMode(), preferences->numberOfSignificantDigits(), *context) ? EqualSign::Equal : EqualSign::Approximation;
return m_equalSign;
}

View File

@@ -1,15 +1,5 @@
#include "global_preferences.h"
GlobalPreferences::GlobalPreferences() :
m_language(I18n::Language::EN),
m_examMode(ExamMode::Desactivate),
#ifdef EPSILON_BOOT_PROMPT
m_showPopUp(true),
#endif
m_brightnessLevel(Ion::Backlight::MaxBrightness)
{
}
GlobalPreferences * GlobalPreferences::sharedGlobalPreferences() {
static GlobalPreferences globalPreferences;
return &globalPreferences;
@@ -20,9 +10,7 @@ I18n::Language GlobalPreferences::language() const {
}
void GlobalPreferences::setLanguage(I18n::Language language) {
if (language != m_language) {
m_language = language;
}
m_language = language;
}
GlobalPreferences::ExamMode GlobalPreferences::examMode() const {
@@ -30,17 +18,13 @@ GlobalPreferences::ExamMode GlobalPreferences::examMode() const {
}
void GlobalPreferences::setExamMode(ExamMode examMode) {
if (examMode != m_examMode) {
m_examMode = examMode;
}
m_examMode = examMode;
}
#ifdef EPSILON_BOOT_PROMPT
void GlobalPreferences::setShowPopUp(bool showPopUp) {
if (showPopUp != m_showPopUp) {
m_showPopUp = showPopUp;
}
m_showPopUp = showPopUp;
}
#endif

View File

@@ -9,7 +9,6 @@ public:
Activate,
Desactivate
};
GlobalPreferences();
static GlobalPreferences * sharedGlobalPreferences();
I18n::Language language() const;
void setLanguage(I18n::Language language);
@@ -23,6 +22,13 @@ public:
void setBrightnessLevel(int brightnessLevel);
constexpr static int NumberOfBrightnessStates = 5;
private:
GlobalPreferences() :
m_language(I18n::Language::EN),
m_examMode(ExamMode::Desactivate),
#ifdef EPSILON_BOOT_PROMPT
m_showPopUp(true),
#endif
m_brightnessLevel(Ion::Backlight::MaxBrightness) {}
I18n::Language m_language;
ExamMode m_examMode;
#ifdef EPSILON_BOOT_PROMPT

View File

@@ -261,12 +261,13 @@ T Sequence::approximateToNextRank(int n, SequenceContext * sqctx) const {
Poincare::Symbol vn1Symbol(Symbol::SpecialSymbols::vn1);
Poincare::Symbol unSymbol(Symbol::SpecialSymbols::un);
Poincare::Symbol un1Symbol(Symbol::SpecialSymbols::un1);
Preferences * preferences = Poincare::Preferences::sharedPreferences();
switch (m_type) {
case Type::Explicit:
{
ctx.setValueForSymbol(un, unSymbol);
ctx.setValueForSymbol(vn, vnSymbol);
return expression(sqctx).approximateWithValueForSymbol(symbol(), (T)n, ctx, Poincare::Preferences::sharedPreferences()->angleUnit());
return expression(sqctx).approximateWithValueForSymbol(symbol(), (T)n, ctx, preferences->angleUnit());
}
case Type::SingleRecurrence:
{
@@ -277,7 +278,7 @@ T Sequence::approximateToNextRank(int n, SequenceContext * sqctx) const {
ctx.setValueForSymbol(unm1, unSymbol);
ctx.setValueForSymbol(vn, vn1Symbol);
ctx.setValueForSymbol(vnm1, vnSymbol);
return expression(sqctx).approximateWithValueForSymbol(symbol(), (T)(n-1), ctx, Poincare::Preferences::sharedPreferences()->angleUnit());
return expression(sqctx).approximateWithValueForSymbol(symbol(), (T)(n-1), ctx, preferences->angleUnit());
}
default:
{
@@ -291,7 +292,7 @@ T Sequence::approximateToNextRank(int n, SequenceContext * sqctx) const {
ctx.setValueForSymbol(unm2, unSymbol);
ctx.setValueForSymbol(vnm1, vn1Symbol);
ctx.setValueForSymbol(vnm2, vnSymbol);
return expression(sqctx).approximateWithValueForSymbol(symbol(), (T)(n-2), ctx, Poincare::Preferences::sharedPreferences()->angleUnit());
return expression(sqctx).approximateWithValueForSymbol(symbol(), (T)(n-2), ctx, preferences->angleUnit());
}
}
}

View File

@@ -69,6 +69,7 @@ void MainController::didBecomeFirstResponder() {
}
bool MainController::handleEvent(Ion::Events::Event event) {
GlobalPreferences * globalPreferences = GlobalPreferences::sharedGlobalPreferences();
if (m_messageTreeModel->children(selectedRow())->numberOfChildren() == 0) {
#if EPSILON_BOOT_PROMPT == EPSILON_BETA_PROMPT
if (m_messageTreeModel->children(selectedRow())->label() == I18n::Message::BetaPopUp) {
@@ -77,7 +78,7 @@ bool MainController::handleEvent(Ion::Events::Event event) {
#endif
#ifdef EPSILON_BOOT_PROMPT
if (event == Ion::Events::OK || event == Ion::Events::EXE) {
GlobalPreferences::sharedGlobalPreferences()->setShowPopUp(!GlobalPreferences::sharedGlobalPreferences()->showPopUp());
globalPreferences->setShowPopUp(!globalPreferences->showPopUp());
m_selectableTableView.reloadCellAtLocation(m_selectableTableView.selectedColumn(), m_selectableTableView.selectedRow());
return true;
}
@@ -88,7 +89,7 @@ bool MainController::handleEvent(Ion::Events::Event event) {
if (event == Ion::Events::Right || event == Ion::Events::Left || event == Ion::Events::Plus || event == Ion::Events::Minus) {
int delta = Ion::Backlight::MaxBrightness/GlobalPreferences::NumberOfBrightnessStates;
int direction = (event == Ion::Events::Right || event == Ion::Events::Plus) ? delta : -delta;
GlobalPreferences::sharedGlobalPreferences()->setBrightnessLevel(GlobalPreferences::sharedGlobalPreferences()->brightnessLevel()+direction);
globalPreferences->setBrightnessLevel(globalPreferences->brightnessLevel()+direction);
m_selectableTableView.reloadCellAtLocation(m_selectableTableView.selectedColumn(), m_selectableTableView.selectedRow());
return true;
}
@@ -184,16 +185,18 @@ int MainController::typeAtLocation(int i, int j) {
}
void MainController::willDisplayCellForIndex(HighlightCell * cell, int index) {
GlobalPreferences * globalPreferences = GlobalPreferences::sharedGlobalPreferences();
Preferences * preferences = Preferences::sharedPreferences();
MessageTableCell * myCell = (MessageTableCell *)cell;
myCell->setMessage(m_messageTreeModel->children(index)->label());
if (index == 4) {
MessageTableCellWithGauge * myGaugeCell = (MessageTableCellWithGauge *)cell;
GaugeView * myGauge = (GaugeView *)myGaugeCell->accessoryView();
myGauge->setLevel((float)GlobalPreferences::sharedGlobalPreferences()->brightnessLevel()/(float)Ion::Backlight::MaxBrightness);
myGauge->setLevel((float)globalPreferences->brightnessLevel()/(float)Ion::Backlight::MaxBrightness);
return;
}
if (index == 5) {
int index = (int)GlobalPreferences::sharedGlobalPreferences()->language()-1;
int index = (int)globalPreferences->language()-1;
static_cast<MessageTableCellWithChevronAndMessage *>(cell)->setSubtitle(I18n::LanguageNames[index]);
return;
}
@@ -201,7 +204,7 @@ void MainController::willDisplayCellForIndex(HighlightCell * cell, int index) {
if (index == 7) {
MessageTableCellWithSwitch * mySwitchCell = (MessageTableCellWithSwitch *)cell;
SwitchView * mySwitch = (SwitchView *)mySwitchCell->accessoryView();
mySwitch->setState(GlobalPreferences::sharedGlobalPreferences()->showPopUp());
mySwitch->setState(globalPreferences->showPopUp());
return;
}
#endif
@@ -209,16 +212,16 @@ void MainController::willDisplayCellForIndex(HighlightCell * cell, int index) {
int childIndex = -1;
switch (index) {
case 0:
childIndex = (int)Preferences::sharedPreferences()->angleUnit();
childIndex = (int)preferences->angleUnit();
break;
case 1:
childIndex = (int)Preferences::sharedPreferences()->displayMode();
childIndex = (int)preferences->displayMode();
break;
case 2:
childIndex = (int)Preferences::sharedPreferences()->editionMode();
childIndex = (int)preferences->editionMode();
break;
case 3:
childIndex = (int)Preferences::sharedPreferences()->complexFormat();
childIndex = (int)preferences->complexFormat();
break;
}
I18n::Message message = childIndex >= 0 ? m_messageTreeModel->children(index)->children(childIndex)->label() : I18n::Message::Default;

View File

@@ -109,32 +109,34 @@ void PreferencesController::willDisplayCellForIndex(HighlightCell * cell, int in
}
void PreferencesController::setPreferenceWithValueIndex(I18n::Message message, int valueIndex) {
Preferences * preferences = Preferences::sharedPreferences();
if (message == I18n::Message::AngleUnit) {
Preferences::sharedPreferences()->setAngleUnit((Preferences::AngleUnit)valueIndex);
preferences->setAngleUnit((Preferences::AngleUnit)valueIndex);
}
if (message == I18n::Message::DisplayMode) {
Preferences::sharedPreferences()->setDisplayMode((Preferences::PrintFloatMode)valueIndex);
preferences->setDisplayMode((Preferences::PrintFloatMode)valueIndex);
}
if (message == I18n::Message::EditionMode) {
Preferences::sharedPreferences()->setEditionMode((Preferences::EditionMode)valueIndex);
preferences->setEditionMode((Preferences::EditionMode)valueIndex);
}
if (message == I18n::Message::ComplexFormat) {
Preferences::sharedPreferences()->setComplexFormat((Preferences::ComplexFormat)valueIndex);
preferences->setComplexFormat((Preferences::ComplexFormat)valueIndex);
}
}
int PreferencesController::valueIndexForPreference(I18n::Message message) {
Preferences * preferences = Preferences::sharedPreferences();
if (message == I18n::Message::AngleUnit) {
return (int)Preferences::sharedPreferences()->angleUnit();
return (int)preferences->angleUnit();
}
if (message == I18n::Message::DisplayMode) {
return (int)Preferences::sharedPreferences()->displayMode();
return (int)preferences->displayMode();
}
if (message == I18n::Message::EditionMode) {
return (int)Preferences::sharedPreferences()->editionMode();
return (int)preferences->editionMode();
}
if (message == I18n::Message::ComplexFormat) {
return (int)Preferences::sharedPreferences()->complexFormat();
return (int)preferences->complexFormat();
}
return 0;
}

View File

@@ -119,8 +119,9 @@ EquationStore::Error EquationStore::exactSolve(Poincare::Context * context) {
Expression coefficients[k_maxNumberOfEquations][Expression::k_maxNumberOfVariables];
Expression constants[k_maxNumberOfEquations];
bool isLinear = true; // Invalid the linear system if one equation is non-linear
Preferences * preferences = Preferences::sharedPreferences();
for (int i = 0; i < numberOfDefinedModels(); i++) {
isLinear = isLinear && definedModelAtIndex(i)->standardForm(context).getLinearCoefficients(m_variables, coefficients[i], &constants[i], *context, Preferences::sharedPreferences()->angleUnit());
isLinear = isLinear && definedModelAtIndex(i)->standardForm(context).getLinearCoefficients(m_variables, coefficients[i], &constants[i], *context, preferences->angleUnit());
if (!isLinear) {
// TODO: should we clean pool allocated memory if the system is not linear
#if 0
@@ -151,7 +152,7 @@ EquationStore::Error EquationStore::exactSolve(Poincare::Context * context) {
assert(numberOfVariables == 1 && numberOfDefinedModels() == 1);
char x = m_variables[0];
Expression polynomialCoefficients[Expression::k_maxNumberOfPolynomialCoefficients];
int degree = definedModelAtIndex(0)->standardForm(context).getPolynomialReducedCoefficients(x, polynomialCoefficients, *context, Preferences::sharedPreferences()->angleUnit());
int degree = definedModelAtIndex(0)->standardForm(context).getPolynomialReducedCoefficients(x, polynomialCoefficients, *context, preferences->angleUnit());
if (degree == 2) {
/* Polynomial degree <= 2*/
m_type = Type::PolynomialMonovariable;
@@ -179,7 +180,7 @@ EquationStore::Error EquationStore::exactSolve(Poincare::Context * context) {
/* Check for equality between exact and approximate layouts */
if (!m_exactSolutionIdentity[i]) {
char buffer[Shared::ExpressionModel::k_expressionBufferSize];
m_exactSolutionEquality[i] = exactSolutions[i].isEqualToItsApproximationLayout(approximate, buffer, Shared::ExpressionModel::k_expressionBufferSize, Preferences::sharedPreferences()->angleUnit(), Preferences::sharedPreferences()->displayMode(), Preferences::sharedPreferences()->numberOfSignificantDigits(), *context);
m_exactSolutionEquality[i] = exactSolutions[i].isEqualToItsApproximationLayout(approximate, buffer, Shared::ExpressionModel::k_expressionBufferSize, preferences->angleUnit(), preferences->displayMode(), preferences->numberOfSignificantDigits(), *context);
}
}
}

View File

@@ -84,10 +84,11 @@ void TitleBarView::refreshPreferences() {
constexpr size_t bufferSize = 13;
char buffer[bufferSize];
int numberOfChar = 0;
if (Preferences::sharedPreferences()->displayMode() == Preferences::PrintFloatMode::Scientific) {
Preferences * preferences = Preferences::sharedPreferences();
if (preferences->displayMode() == Preferences::PrintFloatMode::Scientific) {
numberOfChar += strlcpy(buffer, I18n::translate(I18n::Message::Sci), bufferSize);
}
if (Preferences::sharedPreferences()->angleUnit() == Preferences::AngleUnit::Radian) {
if (preferences->angleUnit() == Preferences::AngleUnit::Radian) {
numberOfChar += strlcpy(buffer+numberOfChar, I18n::translate(I18n::Message::Rad), bufferSize - numberOfChar);
} else {
numberOfChar += strlcpy(buffer+numberOfChar, I18n::translate(I18n::Message::Deg), bufferSize - numberOfChar);

View File

@@ -43,7 +43,8 @@ void GlobalContext::setExpressionForSymbolName(const Expression & expression, co
FileName symbolFileName = fileNameForSymbol(symbol);
// evaluate before deleting anything (to be able to evaluate A+2->A)
Expression evaluation = expression.isUninitialized() ? Expression() : expression.approximate<double>(context, Preferences::sharedPreferences()->angleUnit(), Preferences::sharedPreferences()->complexFormat());
Preferences * preferences = Preferences::sharedPreferences();
Expression evaluation = expression.isUninitialized() ? Expression() : expression.approximate<double>(context, preferences->angleUnit(), preferences->complexFormat());
// Delete any record with same name (as it is going to be override)
Ion::Storage::sharedStorage()->recordNamed(symbolFileName.nameWithExtension).destroy();