From 17e63a5b07ecf8fccd7c0558cd0bbfc6f981aba2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Fri, 10 Aug 2018 13:17:07 +0200 Subject: [PATCH] [shared] Fix references to Poincare --- apps/shared/expression_field_delegate_app.cpp | 7 +-- apps/shared/expression_model.cpp | 43 +++++++------------ apps/shared/expression_model.h | 9 +--- apps/shared/function.cpp | 2 +- apps/shared/poincare_helpers.h | 4 +- apps/shared/store_context.cpp | 2 +- apps/shared/store_context.h | 4 +- apps/shared/store_controller.cpp | 9 ++-- apps/shared/store_controller.h | 4 +- apps/shared/text_field_delegate_app.cpp | 7 +-- apps/variable_box_controller.cpp | 6 +-- 11 files changed, 37 insertions(+), 60 deletions(-) diff --git a/apps/shared/expression_field_delegate_app.cpp b/apps/shared/expression_field_delegate_app.cpp index 9b268fc5f..46d6b96ec 100644 --- a/apps/shared/expression_field_delegate_app.cpp +++ b/apps/shared/expression_field_delegate_app.cpp @@ -34,17 +34,14 @@ bool ExpressionFieldDelegateApp::layoutFieldDidReceiveEvent(LayoutField * layout char buffer[TextField::maxBufferSize()]; int bufferSize = TextField::maxBufferSize(); int length = layoutField->serialize(buffer, bufferSize); - Expression * exp = Expression::parse(buffer); - if (exp != nullptr) { - delete exp; - } + Expression exp = Expression::parse(buffer); if (length >= bufferSize-1) { /* If the buffer is totally full, it is VERY likely that writeTextInBuffer * escaped before printing utterly the expression. */ displayWarning(I18n::Message::SyntaxError); return true; } - if (exp == nullptr) { + if (!exp.isDefined()) { layoutField->app()->displayWarning(I18n::Message::SyntaxError); return true; } diff --git a/apps/shared/expression_model.cpp b/apps/shared/expression_model.cpp index f28976010..6d27a6797 100644 --- a/apps/shared/expression_model.cpp +++ b/apps/shared/expression_model.cpp @@ -10,35 +10,17 @@ namespace Shared { ExpressionModel::ExpressionModel() : m_text{0}, - m_expression(nullptr), - m_layoutRef(nullptr) + m_expression(), + m_layoutRef() { } -ExpressionModel::~ExpressionModel() { - /* We cannot call tidy here because tidy is a virtual function and does not - * do the same thing for all children class. */ - if (m_layoutRef.isDefined()) { - m_layoutRef = LayoutRef(nullptr); - } - if (m_expression != nullptr) { - delete m_expression; - m_expression = nullptr; - } -} - -ExpressionModel& ExpressionModel::operator=(const ExpressionModel& other) { - // Self-assignment is benign - setContent(other.m_text); - return *this; -} - const char * ExpressionModel::text() const { return m_text; } -Poincare::Expression * ExpressionModel::expression(Poincare::Context * context) const { - if (m_expression == nullptr) { +Poincare::Expression ExpressionModel::expression(Poincare::Context * context) const { + if (!m_expression.isDefined()) { m_expression = PoincareHelpers::ParseAndSimplify(m_text, *context); } return m_expression; @@ -46,11 +28,8 @@ Poincare::Expression * ExpressionModel::expression(Poincare::Context * context) LayoutRef ExpressionModel::layoutRef() { if (!m_layoutRef.isDefined()) { - Expression * nonSimplifiedExpression = Expression::parse(m_text); - if (nonSimplifiedExpression != nullptr) { - m_layoutRef = PoincareHelpers::CreateLayout(nonSimplifiedExpression); - delete nonSimplifiedExpression; - } + Expression nonSimplifiedExpression = Expression::parse(m_text); + m_layoutRef = PoincareHelpers::CreateLayout(nonSimplifiedExpression); } return m_layoutRef; } @@ -68,6 +47,8 @@ void ExpressionModel::setContent(const char * c) { /* We cannot call tidy here because tidy is a virtual function and does not * do the same thing for all children class. And here we want to delete only * the m_layout and m_expression. */ +// TODO: the previous expression and layout are going to be destroyed as soon as we call expression(). Should we optimize this? +#if 0 if (m_layoutRef.isDefined()) { m_layoutRef = LayoutRef(nullptr); } @@ -75,9 +56,16 @@ void ExpressionModel::setContent(const char * c) { delete m_expression; m_expression = nullptr; } +#endif } void ExpressionModel::tidy() { +// TODO: what do we want to do? Delete layout and ref? +#if 0 + m_layoutRef = LayoutRef(); ? + m_expression = Expression(); ? + + if (m_layoutRef.isDefined()) { m_layoutRef = LayoutRef(nullptr); } @@ -85,6 +73,7 @@ void ExpressionModel::tidy() { delete m_expression; m_expression = nullptr; } +#endif } } diff --git a/apps/shared/expression_model.h b/apps/shared/expression_model.h index 4ad4e9c0c..ca66424bd 100644 --- a/apps/shared/expression_model.h +++ b/apps/shared/expression_model.h @@ -10,13 +10,8 @@ namespace Shared { class ExpressionModel { public: ExpressionModel(); - virtual ~ExpressionModel(); // Delete expression and layout, if needed - ExpressionModel& operator=(const ExpressionModel& other); - ExpressionModel& operator=(ExpressionModel&& other) = delete; - ExpressionModel(const ExpressionModel& other) = delete; - ExpressionModel(ExpressionModel&& other) = delete; const char * text() const; - Poincare::Expression * expression(Poincare::Context * context) const; + Poincare::Expression expression(Poincare::Context * context) const; Poincare::LayoutRef layoutRef(); /* Here, isDefined is the exact contrary of isEmpty. However, for Sequence * inheriting from ExpressionModel, isEmpty and isDefined have not exactly @@ -34,7 +29,7 @@ private: constexpr static size_t k_dataLengthInBytes = (TextField::maxBufferSize())*sizeof(char); static_assert((k_dataLengthInBytes & 0x3) == 0, "The expression model data size is not a multiple of 4 bytes (cannot compute crc)"); // Assert that dataLengthInBytes is a multiple of 4 char m_text[k_expressionBufferSize]; - mutable Poincare::Expression * m_expression; + mutable Poincare::Expression m_expression; mutable Poincare::LayoutRef m_layoutRef; }; diff --git a/apps/shared/function.cpp b/apps/shared/function.cpp index aaf14c388..efa4cc6e5 100644 --- a/apps/shared/function.cpp +++ b/apps/shared/function.cpp @@ -41,7 +41,7 @@ void Function::setActive(bool active) { template T Function::templatedApproximateAtAbscissa(T x, Poincare::Context * context) const { - return expression(context)->approximateWithValueForSymbol(symbol(), x, *context, Preferences::sharedPreferences()->angleUnit()); + return expression(context).approximateWithValueForSymbol(symbol(), x, *context, Preferences::sharedPreferences()->angleUnit()); } } diff --git a/apps/shared/poincare_helpers.h b/apps/shared/poincare_helpers.h index e3b1bc4c9..17d85793e 100644 --- a/apps/shared/poincare_helpers.h +++ b/apps/shared/poincare_helpers.h @@ -39,8 +39,8 @@ inline Poincare::Expression ParseAndSimplify(const char * text, Poincare::Contex return Poincare::Expression::ParseAndSimplify(text, context, Poincare::Preferences::sharedPreferences()->angleUnit()); } -inline void Simplify(Poincare::Expression* expressionAddress, Poincare::Context & context) { - return Poincare::Expression::Simplify(expressionAddress, context, Poincare::Preferences::sharedPreferences()->angleUnit()); +inline Poincare::Expression Simplify(Poincare::Expression e, Poincare::Context & context) { + return e.simplify(context, Poincare::Preferences::sharedPreferences()->angleUnit()); } } diff --git a/apps/shared/store_context.cpp b/apps/shared/store_context.cpp index db4dd28bf..97ffe4127 100644 --- a/apps/shared/store_context.cpp +++ b/apps/shared/store_context.cpp @@ -7,7 +7,7 @@ using namespace Poincare; namespace Shared { -void StoreContext::setExpressionForSymbolName(const Expression * expression, const Symbol * symbol, Context & context) { +void StoreContext::setExpressionForSymbolName(const Expression expression, const Symbol symbol, Context & context) { m_parentContext->setExpressionForSymbolName(expression, symbol, context); } diff --git a/apps/shared/store_context.h b/apps/shared/store_context.h index f6221ec23..1c6b97b42 100644 --- a/apps/shared/store_context.h +++ b/apps/shared/store_context.h @@ -18,12 +18,12 @@ public: {} void setParentContext(Poincare::Context * parentContext) { m_parentContext = parentContext; } void setSeriesPairIndex(int j) { m_seriesPairIndex = j; } - void setExpressionForSymbolName(const Poincare::Expression * expression, const Poincare::Symbol * symbol, Poincare::Context & context) override; + void setExpressionForSymbolName(const Poincare::Expression expression, const Poincare::Symbol symbol, Poincare::Context & context) override; protected: Shared::DoublePairStore * m_store; int m_seriesPairIndex; Poincare::Context * m_parentContext; - Poincare::Approximation m_value; + Poincare::Float m_value; }; } diff --git a/apps/shared/store_controller.cpp b/apps/shared/store_controller.cpp index 8439d87b0..d7094ed5a 100644 --- a/apps/shared/store_controller.cpp +++ b/apps/shared/store_controller.cpp @@ -72,8 +72,8 @@ bool StoreController::textFieldShouldFinishEditing(TextField * textField, Ion::E bool StoreController::textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) { if (textField == contentView()->formulaInputView()->textField()) { // Handle formula input - Expression * expression = Expression::parse(textField->text()); - if (expression == nullptr) { + Expression expression = Expression::parse(textField->text()); + if (!expression.isDefined()) { app()->displayWarning(I18n::Message::SyntaxError); return false; } @@ -81,7 +81,6 @@ bool StoreController::textFieldDidFinishEditing(TextField * textField, const cha if (fillColumnWithFormula(expression)) { app()->setFirstResponder(contentView()); } - delete expression; return true; } AppsContainer * appsContainer = ((TextFieldDelegateApp *)app())->container(); @@ -269,12 +268,12 @@ void StoreController::unloadView(View * view) { delete view; } -bool StoreController::privateFillColumnWithFormula(Expression * formula, Expression::isVariableTest isVariable) { +bool StoreController::privateFillColumnWithFormula(Expression formula, ExpressionNode::isVariableTest isVariable) { int currentColumn = selectedColumn(); // Fetch the series used in the formula to compute the size of the filled in series char variables[Expression::k_maxNumberOfVariables]; variables[0] = 0; - formula->getVariables(isVariable, variables); + formula.getVariables(isVariable, variables); int numberOfValuesToCompute = -1; int index = 0; while (variables[index] != 0) { diff --git a/apps/shared/store_controller.h b/apps/shared/store_controller.h index f3e031d8e..9e2c551dd 100644 --- a/apps/shared/store_controller.h +++ b/apps/shared/store_controller.h @@ -19,7 +19,7 @@ public: virtual StoreContext * storeContext() = 0; void displayFormulaInput(); virtual void setFormulaLabel() = 0; - virtual bool fillColumnWithFormula(Poincare::Expression * formula) = 0; + virtual bool fillColumnWithFormula(Poincare::Expression formula) = 0; // TextFieldDelegate bool textFieldShouldFinishEditing(TextField * textField, Ion::Events::Event event) override; @@ -85,7 +85,7 @@ protected: virtual HighlightCell * titleCells(int index) = 0; char m_draftTextBuffer[TextField::maxBufferSize()]; int seriesAtColumn(int column) const { return column / DoublePairStore::k_numberOfColumnsPerSeries; } - bool privateFillColumnWithFormula(Poincare::Expression * formula, Poincare::Expression::isVariableTest isVariable); + bool privateFillColumnWithFormula(Poincare::Expression formula, Poincare::ExpressionNode::isVariableTest isVariable); virtual StoreParameterController * storeParameterController() = 0; StoreCell * m_editableCells[k_maxNumberOfEditableCells]; DoublePairStore * m_store; diff --git a/apps/shared/text_field_delegate_app.cpp b/apps/shared/text_field_delegate_app.cpp index d8a26093a..0fef4e5d3 100644 --- a/apps/shared/text_field_delegate_app.cpp +++ b/apps/shared/text_field_delegate_app.cpp @@ -78,11 +78,8 @@ bool TextFieldDelegateApp::textFieldShouldFinishEditing(TextField * textField, I bool TextFieldDelegateApp::textFieldDidReceiveEvent(TextField * textField, Ion::Events::Event event) { if (textField->isEditing() && textField->textFieldShouldFinishEditing(event)) { - Expression * exp = Expression::parse(textField->text()); - if (exp != nullptr) { - delete exp; - } - if (exp == nullptr) { + Expression exp = Expression::parse(textField->text()); + if (!exp.isDefined()) { textField->app()->displayWarning(I18n::Message::SyntaxError); return true; } diff --git a/apps/variable_box_controller.cpp b/apps/variable_box_controller.cpp index 6686ed4cd..9f5497ff1 100644 --- a/apps/variable_box_controller.cpp +++ b/apps/variable_box_controller.cpp @@ -79,11 +79,11 @@ bool VariableBoxController::ContentViewController::handleEvent(Ion::Events::Even if (event == Ion::Events::Backspace && m_currentPage != Page::RootMenu) { if (m_currentPage == Page::Scalar) { const Symbol symbol('A'+selectedRow()); - m_context->setExpressionForSymbolName(nullptr, &symbol, *m_context); + m_context->setExpressionForSymbolName(Expression(), symbol, *m_context); } if (m_currentPage == Page::Matrix) { const Symbol symbol = Symbol::matrixSymbol('0'+(char)selectedRow()); - m_context->setExpressionForSymbolName(nullptr, &symbol, *m_context); + m_context->setExpressionForSymbolName(Expression(), symbol, *m_context); } m_selectableTableView.reloadData(); return true; @@ -138,7 +138,7 @@ void VariableBoxController::ContentViewController::willDisplayCellForIndex(Highl char label[3]; putLabelAtIndexInBuffer(index, label); myCell->setLabel(label); - const Expression * evaluation = expressionForIndex(index); + const Expression evaluation = expressionForIndex(index); if (m_currentPage == Page::Scalar) { myCell->displayExpression(false); char buffer[PrintFloat::k_maxComplexBufferLength];