diff --git a/apps/calculation/app.cpp b/apps/calculation/app.cpp index 51b521e66..2d9e56937 100644 --- a/apps/calculation/app.cpp +++ b/apps/calculation/app.cpp @@ -92,7 +92,7 @@ bool App::textInputIsCorrect(const char * text) { * less than k_printedExpressionLength characters. Otherwise, we prevent the * user from adding this expression to the calculation store. */ Expression exp = Expression::parse(text); - if (!exp.isDefined()) { + if (exp.isUninitialized()) { return false; } exp = exp.replaceSymbolWithExpression(Symbol::SpecialSymbols::Ans, static_cast(snapshot())->calculationStore()->ansExpression(localContext())); diff --git a/apps/calculation/calculation.cpp b/apps/calculation/calculation.cpp index 2b9cf58ed..ecf6bc65d 100644 --- a/apps/calculation/calculation.cpp +++ b/apps/calculation/calculation.cpp @@ -100,7 +100,7 @@ const char * Calculation::approximateOutputText() { } Expression Calculation::input() { - if (!m_input.isDefined()) { + if (m_input.isUninitialized()) { m_input = Expression::parse(m_inputText); } return m_input; @@ -143,13 +143,13 @@ void Calculation::tidy() { } Expression Calculation::exactOutput(Context * context) { - if (!m_exactOutput.isDefined()) { + if (m_exactOutput.isUninitialized()) { /* Because the angle unit might have changed, we do not simplify again. We * thereby avoid turning cos(Pi/4) into sqrt(2)/2 and displaying * 'sqrt(2)/2 = 0.999906' (which is totally wrong) instead of * 'cos(pi/4) = 0.999906' (which is true in degree). */ m_exactOutput = Expression::parse(m_exactOutputText); - if (!m_exactOutput.isDefined()) { + if (m_exactOutput.isUninitialized()) { m_exactOutput = Undefined(); } } @@ -161,7 +161,7 @@ LayoutRef Calculation::createExactOutputLayout(Context * context) { } Expression Calculation::approximateOutput(Context * context) { - if (!m_approximateOutput.isDefined()) { + if (m_approximateOutput.isUninitialized()) { /* To ensure that the expression 'm_output' is a matrix or a complex, we * call 'evaluate'. */ Expression exp = Expression::parse(m_approximateOutputText); diff --git a/apps/calculation/edit_expression_controller.cpp b/apps/calculation/edit_expression_controller.cpp index 04b8ca42f..49ffeff8d 100644 --- a/apps/calculation/edit_expression_controller.cpp +++ b/apps/calculation/edit_expression_controller.cpp @@ -154,11 +154,10 @@ bool EditExpressionController::inputViewDidReceiveEvent(Ion::Events::Event event bool EditExpressionController::inputViewDidFinishEditing(const char * text, LayoutRef layoutR) { App * calculationApp = (App *)app(); - if (!layoutR.isDefined()) { + if (layoutR.isUninitialized()) { assert(text); strlcpy(m_cacheBuffer, text, Calculation::k_printedExpressionSize); } else { - assert(layoutR.isDefined()); layoutR.serialize(m_cacheBuffer, Calculation::k_printedExpressionSize); } m_calculationStore->push(m_cacheBuffer, calculationApp->localContext()); diff --git a/apps/calculation/history_view_cell.cpp b/apps/calculation/history_view_cell.cpp index 21bd7f2f8..c1a0cd5df 100644 --- a/apps/calculation/history_view_cell.cpp +++ b/apps/calculation/history_view_cell.cpp @@ -100,7 +100,7 @@ void HistoryViewCell::setCalculation(Calculation * calculation) { /* Both output expressions have to be updated at the same time. Otherwise, * when updating one layout, if the second one still points to a deleted * layout, calling to layoutSubviews() would fail. */ - if (m_exactOutputLayout.isDefined()) { + if (!m_exactOutputLayout.isUninitialized()) { m_exactOutputLayout = Poincare::LayoutRef(); } if (!calculation->shouldOnlyDisplayApproximateOutput(calculationApp->localContext())) { diff --git a/apps/shared/expression_field_delegate_app.cpp b/apps/shared/expression_field_delegate_app.cpp index 46d6b96ec..6e4b377e1 100644 --- a/apps/shared/expression_field_delegate_app.cpp +++ b/apps/shared/expression_field_delegate_app.cpp @@ -41,7 +41,7 @@ bool ExpressionFieldDelegateApp::layoutFieldDidReceiveEvent(LayoutField * layout displayWarning(I18n::Message::SyntaxError); return true; } - if (!exp.isDefined()) { + if (exp.isUninitialized()) { layoutField->app()->displayWarning(I18n::Message::SyntaxError); return true; } diff --git a/apps/shared/expression_model.cpp b/apps/shared/expression_model.cpp index 984ce6e85..b59693407 100644 --- a/apps/shared/expression_model.cpp +++ b/apps/shared/expression_model.cpp @@ -20,14 +20,14 @@ const char * ExpressionModel::text() const { } Poincare::Expression ExpressionModel::expression(Poincare::Context * context) const { - if (!m_expression.isDefined()) { + if (m_expression.isUninitialized()) { m_expression = PoincareHelpers::ParseAndSimplify(m_text, *context); } return m_expression; } LayoutRef ExpressionModel::layoutRef() { - if (!m_layoutRef.isDefined()) { + if (m_layoutRef.isUninitialized()) { Expression nonSimplifiedExpression = Expression::parse(m_text); m_layoutRef = PoincareHelpers::CreateLayout(nonSimplifiedExpression); } @@ -49,7 +49,7 @@ void ExpressionModel::setContent(const char * c) { * 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()) { + if (!m_layoutRef.isUninitialized()) { m_layoutRef = LayoutRef(); } if (m_expression != nullptr) { @@ -66,7 +66,7 @@ void ExpressionModel::tidy() { m_expression = Expression(); ? - if (m_layoutRef.isDefined()) { + if (!m_layoutRef.isUninitialized()) { m_layoutRef = LayoutRef(); } if (m_expression != nullptr) { diff --git a/apps/shared/expression_model_list_controller.cpp b/apps/shared/expression_model_list_controller.cpp index 0cda0150a..aa8a2a4e9 100644 --- a/apps/shared/expression_model_list_controller.cpp +++ b/apps/shared/expression_model_list_controller.cpp @@ -23,7 +23,7 @@ KDCoordinate ExpressionModelListController::expressionRowHeight(int j) { return Metric::StoreRowHeight; } ExpressionModel * m = modelStore()->modelAtIndex(j); - if (!m->layoutRef().isDefined()) { + if (m->layoutRef().isUninitialized()) { return Metric::StoreRowHeight; } KDCoordinate modelSize = m->layoutRef().layoutSize().height(); diff --git a/apps/shared/expression_model_store.cpp b/apps/shared/expression_model_store.cpp index 02e98fba6..970a287b1 100644 --- a/apps/shared/expression_model_store.cpp +++ b/apps/shared/expression_model_store.cpp @@ -39,7 +39,7 @@ ExpressionModel * ExpressionModelStore::definedModelAtIndex(int i) { assert(i>=0 && iisDefined()) { + if (!modelAtIndex(k)->isUninitialized()) { if (i == index) { return modelAtIndex(k); } @@ -53,7 +53,7 @@ ExpressionModel * ExpressionModelStore::definedModelAtIndex(int i) { int ExpressionModelStore::numberOfDefinedModels() { int result = 0; for (int i = 0; i < m_numberOfModels; i++) { - if (modelAtIndex(i)->isDefined()) { + if (!modelAtIndex(i)->isUninitialized()) { result++; } } diff --git a/apps/shared/scrollable_exact_approximate_expressions_view.cpp b/apps/shared/scrollable_exact_approximate_expressions_view.cpp index 8276592b3..1cb08e3d9 100644 --- a/apps/shared/scrollable_exact_approximate_expressions_view.cpp +++ b/apps/shared/scrollable_exact_approximate_expressions_view.cpp @@ -70,10 +70,10 @@ Poincare::LayoutRef ScrollableExactApproximateExpressionsView::ContentCell::layo } int ScrollableExactApproximateExpressionsView::ContentCell::numberOfSubviews() const { - if (m_exactExpressionView.layoutRef().isDefined()) { - return 3; + if (m_exactExpressionView.layoutRef().isUninitialized()) { + return 1; } - return 1; + return 3; } View * ScrollableExactApproximateExpressionsView::ContentCell::subviewAtIndex(int index) { @@ -115,7 +115,7 @@ void ScrollableExactApproximateExpressionsView::setEqualMessage(I18n::Message eq } void ScrollableExactApproximateExpressionsView::didBecomeFirstResponder() { - if (!m_contentCell.exactExpressionView()->layoutRef().isDefined()) { + if (m_contentCell.exactExpressionView()->layoutRef().isUninitialized()) { setSelectedSubviewType(SubviewType::ApproximativeOutput); } else { setSelectedSubviewType(SubviewType::ExactOutput); diff --git a/apps/shared/store_controller.cpp b/apps/shared/store_controller.cpp index d7094ed5a..f8074d99c 100644 --- a/apps/shared/store_controller.cpp +++ b/apps/shared/store_controller.cpp @@ -73,7 +73,7 @@ bool StoreController::textFieldDidFinishEditing(TextField * textField, const cha if (textField == contentView()->formulaInputView()->textField()) { // Handle formula input Expression expression = Expression::parse(textField->text()); - if (!expression.isDefined()) { + if (expression.isUninitialized()) { app()->displayWarning(I18n::Message::SyntaxError); return false; } diff --git a/apps/shared/text_field_delegate_app.cpp b/apps/shared/text_field_delegate_app.cpp index 0fef4e5d3..2a9d3896f 100644 --- a/apps/shared/text_field_delegate_app.cpp +++ b/apps/shared/text_field_delegate_app.cpp @@ -79,7 +79,7 @@ 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.isDefined()) { + if (exp.isUninitialized()) { textField->app()->displayWarning(I18n::Message::SyntaxError); return true; } diff --git a/apps/variable_box_controller.cpp b/apps/variable_box_controller.cpp index 1218a07bc..942c5e1c9 100644 --- a/apps/variable_box_controller.cpp +++ b/apps/variable_box_controller.cpp @@ -147,7 +147,7 @@ void VariableBoxController::ContentViewController::willDisplayCellForIndex(Highl return; } myCell->displayExpression(true); - if (evaluation.isDefined()) { + if (!evaluation.isUninitialized()) { /* TODO: implement list contexts */ // TODO: handle matrix and scalar! LayoutRef layoutR = layoutRefForIndex(index); @@ -169,7 +169,7 @@ KDCoordinate VariableBoxController::ContentViewController::rowHeight(int index) return Metric::ToolboxRowHeight; } LayoutRef layoutR = layoutRefForIndex(index); - if (layoutR.isDefined()) { + if (!layoutR.isUninitialized()) { return layoutR.layoutSize().height()+k_leafMargin; } return Metric::ToolboxRowHeight; diff --git a/escher/src/expression_view.cpp b/escher/src/expression_view.cpp index e716cebcf..e55fa4582 100644 --- a/escher/src/expression_view.cpp +++ b/escher/src/expression_view.cpp @@ -42,7 +42,7 @@ int ExpressionView::numberOfLayouts() const { } KDSize ExpressionView::minimalSizeForOptimalDisplay() const { - if (!m_layoutRef.isDefined()) { + if (m_layoutRef.isUninitialized()) { return KDSizeZero; } KDSize expressionSize = m_layoutRef.layoutSize(); @@ -60,7 +60,7 @@ KDPoint ExpressionView::absoluteDrawingOrigin() const { void ExpressionView::drawRect(KDContext * ctx, KDRect rect) const { ctx->fillRect(rect, m_backgroundColor); - if (m_layoutRef.isDefined()) { + if (!m_layoutRef.isUninitialized()) { m_layoutRef.draw(ctx, drawingOrigin(), m_textColor, m_backgroundColor); } } diff --git a/escher/src/layout_field.cpp b/escher/src/layout_field.cpp index 8e580d75a..1f5d80dd2 100644 --- a/escher/src/layout_field.cpp +++ b/escher/src/layout_field.cpp @@ -79,7 +79,7 @@ bool LayoutField::handleEventWithText(const char * text, bool indentation, bool m_contentView.cursor()->addEmptyMatrixLayout(); } else { Expression resultExpression = Expression::parse(text); - if (!resultExpression.isDefined()) { + if (resultExpression.isUninitialized()) { m_contentView.cursor()->insertText(text); } else { LayoutRef resultLayoutRef = resultExpression.createLayout(Poincare::Preferences::sharedPreferences()->displayMode(), Poincare::Preferences::sharedPreferences()->numberOfSignificantDigits()); @@ -248,7 +248,7 @@ void LayoutField::scrollToBaselinedRect(KDRect rect, KDCoordinate baseline) { } void LayoutField::insertLayoutAtCursor(LayoutRef layoutR, LayoutRef pointedLayoutR, bool forceCursorRightOfLayout) { - if (!layoutR.isDefined() || layoutRef().isAllocationFailure()) { + if (layoutR.isUninitialized() || layoutRef().isAllocationFailure()) { return; } @@ -265,7 +265,7 @@ void LayoutField::insertLayoutAtCursor(LayoutRef layoutR, LayoutRef pointedLayou if (layoutRef().isAllocationFailure()) { m_contentView.cursor()->setLayoutReference(layoutRef()); } else if(!forceCursorRightOfLayout) { - if (pointedLayoutR.isDefined() && (!layoutWillBeMerged || pointedLayoutR != layoutR)) { + if (!pointedLayoutR.isUninitialized() && (!layoutWillBeMerged || pointedLayoutR != layoutR)) { // Make sure the layout was inserted (its parent is not uninitialized) m_contentView.cursor()->setLayoutReference(pointedLayoutR); m_contentView.cursor()->setPosition(LayoutCursor::Position::Right); @@ -292,7 +292,7 @@ void LayoutField::insertLayoutAtCursor(LayoutRef layoutR, LayoutRef pointedLayou if (!layoutWillBeMerged) { scrollRightOfLayout(layoutR); } else { - assert(lastMergedLayoutChild.isDefined()); + assert(!lastMergedLayoutChild.isUninitialized()); scrollRightOfLayout(lastMergedLayoutChild); } scrollToCursor(); diff --git a/escher/src/selectable_table_view.cpp b/escher/src/selectable_table_view.cpp index 54f486318..e5525220d 100644 --- a/escher/src/selectable_table_view.cpp +++ b/escher/src/selectable_table_view.cpp @@ -129,7 +129,7 @@ bool SelectableTableView::handleEvent(Ion::Events::Event event) { return true; } Poincare::LayoutRef layoutR = cell->layoutRef(); - if (layoutR.isDefined() && !layoutR.isAllocationFailure()) { + if (!layoutR.isUninitialized() && !layoutR.isAllocationFailure()) { Clipboard::sharedClipboard()->store(layoutR); return true; } diff --git a/poincare/include/poincare/expression.h b/poincare/include/poincare/expression.h index 0e31c36c1..e7b200245 100644 --- a/poincare/include/poincare/expression.h +++ b/poincare/include/poincare/expression.h @@ -108,7 +108,7 @@ public: bool isEqualToItsApproximationLayout(Expression approximation, int bufferSize, Preferences::AngleUnit angleUnit, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits, Context & context); /* Layout Helper */ - LayoutRef createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const { return isDefined() ? node()->createLayout(floatDisplayMode, numberOfSignificantDigits) : LayoutRef(); } + LayoutRef createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const { return node()->createLayout(floatDisplayMode, numberOfSignificantDigits); } int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode = Preferences::PrintFloatMode::Decimal, int numberOfSignificantDigits = PrintFloat::k_numberOfStoredSignificantDigits) const { return this->node()->serialize(buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits); } /* Simplification */ diff --git a/poincare/include/poincare/layout_cursor.h b/poincare/include/poincare/layout_cursor.h index d82a32739..67989e0df 100644 --- a/poincare/include/poincare/layout_cursor.h +++ b/poincare/include/poincare/layout_cursor.h @@ -48,7 +48,7 @@ public: } // Definition - bool isDefined() const { return m_layoutRef.isDefined(); } + bool isDefined() const { return !m_layoutRef.isUninitialized(); } // Getters and setters LayoutRef layoutReference() { return m_layoutRef; } diff --git a/poincare/include/poincare/layout_reference.h b/poincare/include/poincare/layout_reference.h index 554e8302b..ad528ad2f 100644 --- a/poincare/include/poincare/layout_reference.h +++ b/poincare/include/poincare/layout_reference.h @@ -92,7 +92,7 @@ public: void replaceChildWithEmpty(LayoutReference oldChild, LayoutCursor * cursor = nullptr); void replaceWith(LayoutReference newChild, LayoutCursor * cursor) { LayoutReference p = parent(); - assert(p.isDefined()); + assert(!p.isUninitialized()); p.replaceChild(*this, newChild, cursor); } void replaceWithJuxtapositionOf(LayoutReference leftChild, LayoutReference rightChild, LayoutCursor * cursor, bool putCursorInTheMiddle = false); diff --git a/poincare/include/poincare/tree_by_reference.h b/poincare/include/poincare/tree_by_reference.h index 704225733..5e7586351 100644 --- a/poincare/include/poincare/tree_by_reference.h +++ b/poincare/include/poincare/tree_by_reference.h @@ -38,53 +38,54 @@ public: int identifier() const { return m_identifier; } virtual TreeNode * node() const { return TreePool::sharedPool()->node(m_identifier); } - bool isDefined() const { return m_identifier != TreePool::NoNodeIdentifier && node() != nullptr; } - bool isAllocationFailure() const { return isDefined() && node()->isAllocationFailure(); } + bool isUninitialized() const { return node()->isUninitialized(); } + bool isAllocationFailure() const { return node()->isAllocationFailure(); } + bool isStatic() const { return node()->isStatic(); } int nodeRetainCount() const { - assert(isDefined()); + assert(!isUninitialized()); return node()->retainCount(); } void incrementNumberOfChildren(int increment = 1) { - assert(isDefined()); + assert(!isUninitialized()); node()->incrementNumberOfChildren(increment); } void decrementNumberOfChildren(int decrement = 1) { - assert(isDefined()); + assert(!isUninitialized()); node()->decrementNumberOfChildren(decrement); } int numberOfDescendants(bool includeSelf) const { - assert(isDefined()); + assert(!isUninitialized()); return node()->numberOfDescendants(includeSelf); } /* Hierarchy */ bool hasChild(TreeByReference t) const { - assert(isDefined()); + assert(!isUninitialized()); return node()->hasChild(t.node()); } bool hasSibling(TreeByReference t) const { - assert(isDefined()); + assert(!isUninitialized()); return node()->hasSibling(t.node()); } bool hasAncestor(TreeByReference t, bool includeSelf) const { - assert(isDefined()); + assert(!isUninitialized()); return node()->hasAncestor(t.node(), includeSelf); } int numberOfChildren() const { - assert(isDefined()); + assert(!isUninitialized()); return node()->numberOfChildren(); } TreeByReference parent() const { - assert(isDefined()); + assert(!isUninitialized()); return TreeByReference(node()->parent()); } TreeByReference childAtIndex(int i) const { - assert(isDefined()); + assert(!isUninitialized()); return TreeByReference(node()->childAtIndex(i)); } int indexOfChild(TreeByReference t) const { - assert(isDefined()); + assert(!isUninitialized()); return node()->indexOfChild(t.node()); } @@ -118,9 +119,7 @@ protected: TreeByReference() : m_identifier(-1) {} void setIdentifierAndRetain(int newId) { m_identifier = newId; - if (isDefined()) { - node()->retain(); - } + node()->retain(); } void setTo(const TreeByReference & tr); /* Hierarchy operations */ diff --git a/poincare/include/poincare/tree_by_value.h b/poincare/include/poincare/tree_by_value.h index 2cebd70fe..bde69351c 100644 --- a/poincare/include/poincare/tree_by_value.h +++ b/poincare/include/poincare/tree_by_value.h @@ -19,7 +19,7 @@ public: virtual ~TreeByValue() = default; - bool isDefined() const { return TreeByReference::isDefined(); } + bool isUninitialized() const { return TreeByReference::isUninitialized(); } bool isAllocationFailure() const { return TreeByReference::isAllocationFailure(); } TreeNode * node() const override { return TreeByReference::node(); } diff --git a/poincare/src/addition.cpp b/poincare/src/addition.cpp index 75e34af7d..a27e9332f 100644 --- a/poincare/src/addition.cpp +++ b/poincare/src/addition.cpp @@ -75,7 +75,7 @@ Expression AdditionNode::factorizeOnCommonDenominator(Context & context, Prefere Multiplication commonDenominator = Multiplication(); for (int i = 0; i < numberOfChildren(); i++) { Expression denominator = childAtIndex(i)->denominator(context, angleUnit); - if (denominator.isDefined()) { + if (!denominator.isUninitialized()) { // Make commonDenominator = LeastCommonMultiple(commonDenominator, denominator); commonDenominator.addMissingFactors(denominator, context, angleUnit); } diff --git a/poincare/src/complex.cpp b/poincare/src/complex.cpp index 0c3ff2b7b..1bee2f4d2 100644 --- a/poincare/src/complex.cpp +++ b/poincare/src/complex.cpp @@ -72,9 +72,9 @@ Expression ComplexNode::complexToExpression(Preferences::ComplexFormat comple imag = Multiplication(Number::DecimalNumber(-this->imag()), Symbol(Ion::Charset::IComplex)); } } - if (!imag.isDefined()) { + if (imag.isUninitialized()) { return real; - } else if (!real.isDefined()) { + } else if (real.isUninitialized()) { if (this->imag() > 0) { return imag; } else { @@ -110,9 +110,9 @@ Expression ComplexNode::complexToExpression(Preferences::ComplexFormat comple } exp = Power(Symbol(Ion::Charset::Exponential), arg); } - if (!exp.isDefined()) { + if (exp.isUninitialized()) { return norm; - } else if (!norm.isDefined()) { + } else if (norm.isUninitialized()) { return exp; } else { return Multiplication(norm, exp); diff --git a/poincare/src/expression.cpp b/poincare/src/expression.cpp index 8db9e7f95..abd0beb21 100644 --- a/poincare/src/expression.cpp +++ b/poincare/src/expression.cpp @@ -201,11 +201,13 @@ bool Expression::isEqualToItsApproximationLayout(Expression approximation, int b Expression Expression::ParseAndSimplify(const char * text, Context & context, Preferences::AngleUnit angleUnit) { Expression exp = parse(text); - if (!exp.isDefined()) { + if (exp.isUninitialized()) { return Undefined(); } Expression reduced = exp.simplify(context, angleUnit); - if (!reduced.isDefined()) { + /* simplify might have been interrupted, in which case the resulting + * expression is uninitialized, so we need to check that. */ + if (reduced.isUninitialized()) { return exp; } return reduced; @@ -250,11 +252,8 @@ Expression Expression::deepBeautify(Context & context, Preferences::AngleUnit an template Expression Expression::approximate(Context& context, Preferences::AngleUnit angleUnit, Preferences::Preferences::ComplexFormat complexFormat) const { - if (isDefined()) { - Evaluation e = node()->approximate(U(), context, angleUnit); - return e.complexToExpression(complexFormat); - } - return Undefined(); + Evaluation e = node()->approximate(U(), context, angleUnit); + return e.complexToExpression(complexFormat); } template @@ -487,17 +486,17 @@ double Expression::nextIntersectionWithExpression(char symbol, double start, dou double extremumMax = std::isnan(result) ? max : result; Coordinate2D resultExtremum[2] = { nextMinimumOfExpression(symbol, start, step, extremumMax, [](char symbol, double x, Context & context, Preferences::AngleUnit angleUnit, const Expression expression0, const Expression expression1) { - if (expression1.isDefined()) { - return expression0.approximateWithValueForSymbol(symbol, x, context, angleUnit)-expression1.approximateWithValueForSymbol(symbol, x, context, angleUnit); - } else { + if (expression1.isUninitialized()) { return expression0.approximateWithValueForSymbol(symbol, x, context, angleUnit); + } else { + return expression0.approximateWithValueForSymbol(symbol, x, context, angleUnit)-expression1.approximateWithValueForSymbol(symbol, x, context, angleUnit); } }, context, angleUnit, expression, true), nextMinimumOfExpression(symbol, start, step, extremumMax, [](char symbol, double x, Context & context, Preferences::AngleUnit angleUnit, const Expression expression0, const Expression expression1) { - if (expression1.isDefined()) { - return expression1.approximateWithValueForSymbol(symbol, x, context, angleUnit)-expression0.approximateWithValueForSymbol(symbol, x, context, angleUnit); - } else { + if (expression1.isUninitialized()) { return -expression0.approximateWithValueForSymbol(symbol, x, context, angleUnit); + } else { + return expression1.approximateWithValueForSymbol(symbol, x, context, angleUnit)-expression0.approximateWithValueForSymbol(symbol, x, context, angleUnit); } }, context, angleUnit, expression, true)}; for (int i = 0; i < 2; i++) { diff --git a/poincare/src/global_context.cpp b/poincare/src/global_context.cpp index 9ce4707b4..75a1bde34 100644 --- a/poincare/src/global_context.cpp +++ b/poincare/src/global_context.cpp @@ -43,7 +43,7 @@ const Expression GlobalContext::expressionForSymbol(const Symbol symbol) { if (index < 0 || index >= k_maxNumberOfScalarExpressions) { return Expression(); } - if (m_expressions[index].isDefined()) { + if (!m_expressions[index].isUninitialized()) { return m_expressions[index]; } return defaultExpression(); @@ -52,7 +52,7 @@ const Expression GlobalContext::expressionForSymbol(const Symbol symbol) { LayoutRef GlobalContext::layoutForSymbol(const Symbol symbol, int numberOfSignificantDigits) { if (Symbol::isMatrixSymbol(symbol.name())) { int index = symbolIndex(symbol); - if (!m_matrixLayouts[index].isDefined()) { + if (m_matrixLayouts[index].isUninitialized()) { m_matrixLayouts[index] = m_matrixExpressions[index].createLayout(Preferences::PrintFloatMode::Decimal, numberOfSignificantDigits); } return m_matrixLayouts[index]; @@ -65,8 +65,8 @@ void GlobalContext::setExpressionForSymbolName(const Expression expression, cons if (Symbol::isMatrixSymbol(symbol.name())) { int indexMatrix = symbol.name() - (char)Symbol::SpecialSymbols::M0; assert(indexMatrix >= 0 && indexMatrix < k_maxNumberOfMatrixExpressions); - Expression evaluation = expression.isDefined() ? expression.approximate(context, Preferences::sharedPreferences()->angleUnit(), Preferences::sharedPreferences()->complexFormat()) : Expression(); // evaluate before deleting anything (to be able to evaluate M1+2->M1) - if (evaluation.isDefined()) { + Expression evaluation = expression.isUninitialized() ? Expression() : expression.approximate(context, Preferences::sharedPreferences()->angleUnit(), Preferences::sharedPreferences()->complexFormat()); // evaluate before deleting anything (to be able to evaluate M1+2->M1) + if (!evaluation.isUninitialized()) { if (evaluation.type() != ExpressionNode::Type::Matrix) { m_matrixExpressions[indexMatrix] = Matrix(evaluation); } else { @@ -78,8 +78,8 @@ void GlobalContext::setExpressionForSymbolName(const Expression expression, cons if (index < 0 || index >= k_maxNumberOfScalarExpressions) { return; } - Expression evaluation = expression.isDefined() ? expression.approximate(context, Preferences::sharedPreferences()->angleUnit(), Preferences::sharedPreferences()->complexFormat()) : Expression(); // evaluate before deleting anything (to be able to evaluate A+2->A) - if (!evaluation.isDefined()) { + Expression evaluation = expression.isUninitialized() ? Expression() : expression.approximate(context, Preferences::sharedPreferences()->angleUnit(), Preferences::sharedPreferences()->complexFormat()); // evaluate before deleting anything (to be able to evaluate A+2->A) + if (evaluation.isUninitialized()) { return; } if (evaluation.type() == ExpressionNode::Type::Matrix) { diff --git a/poincare/src/layout_cursor.cpp b/poincare/src/layout_cursor.cpp index 43c614514..9bddce8e7 100644 --- a/poincare/src/layout_cursor.cpp +++ b/poincare/src/layout_cursor.cpp @@ -173,7 +173,7 @@ void LayoutCursor::insertText(const char * text) { newChild = CharLayoutRef(Ion::Charset::MiddleDot); } else if (text[i] == '(') { newChild = CharLayoutRef('('); //TODO - if (!pointedChild.isDefined()) { + if (pointedChild.isUninitialized()) { pointedChild = newChild; } } else if (text[i] == ')') { @@ -205,7 +205,7 @@ void LayoutCursor::insertText(const char * text) { } m_layoutRef.addSibling(this, newChild, true); } - if (pointedChild.isDefined() && pointedChild.parent().isDefined()) { + if (!pointedChild.isUninitialized() && !pointedChild.parent().isUninitialized()) { m_layoutRef = pointedChild; } } @@ -276,7 +276,7 @@ bool LayoutCursor::baseForNewPowerLayout() { return true; } LayoutCursor equivalentLayoutCursor = m_layoutRef.equivalentCursor(this); - if (!equivalentLayoutCursor.layoutReference().isDefined() + if (equivalentLayoutCursor.layoutReference().isUninitialized() || (equivalentLayoutCursor.layoutReference().isHorizontal() && equivalentLayoutCursor.position() == Position::Left)) { @@ -300,12 +300,12 @@ bool LayoutCursor::privateShowHideEmptyLayoutIfNeeded(bool show) { } else { // Check the equivalent cursor position LayoutRef equivalentPointedLayout = m_layoutRef.equivalentCursor(this).layoutReference(); - if (equivalentPointedLayout.isDefined() && equivalentPointedLayout.isEmpty()) { + if (!equivalentPointedLayout.isUninitialized() && equivalentPointedLayout.isEmpty()) { adjacentEmptyLayout = equivalentPointedLayout; } } - if (!adjacentEmptyLayout.isDefined()) { + if (adjacentEmptyLayout.isUninitialized()) { return false; } /* Change the visibility of the neighbouring empty layout: it might be either diff --git a/poincare/src/layout_helper.cpp b/poincare/src/layout_helper.cpp index 55d1437d4..6fbf9b057 100644 --- a/poincare/src/layout_helper.cpp +++ b/poincare/src/layout_helper.cpp @@ -46,7 +46,7 @@ LayoutRef LayoutHelper::Prefix(const Expression expression, Preferences::PrintFl LayoutRef LayoutHelper::Parentheses(LayoutRef layoutRef, bool cloneLayout) { HorizontalLayoutRef result; result.addChildAtIndex(LeftParenthesisLayoutRef(), 0, 0, nullptr); - if (layoutRef.isDefined()) { + if (!layoutRef.isUninitialized()) { result.addOrMergeChildAtIndex(cloneLayout ? layoutRef.clone() : layoutRef, 1, true); } result.addChildAtIndex(RightParenthesisLayoutRef(), result.numberOfChildren(), result.numberOfChildren(), nullptr); diff --git a/poincare/src/layout_node.cpp b/poincare/src/layout_node.cpp index 8c204a54b..0b1b0c6d9 100644 --- a/poincare/src/layout_node.cpp +++ b/poincare/src/layout_node.cpp @@ -237,7 +237,7 @@ void LayoutNode::scoreCursorInDescendantsVertically ( bool LayoutNode::changeGreySquaresOfAllMatrixAncestors(bool add) { bool changedSquares = false; LayoutRef currentAncestor = LayoutRef(parent()); - while (currentAncestor.isDefined()) { + while (!currentAncestor.isUninitialized()) { if (currentAncestor.isMatrix()) { if (add) { MatrixLayoutRef(static_cast(currentAncestor.node())).addGreySquares(); diff --git a/poincare/src/layout_reference.cpp b/poincare/src/layout_reference.cpp index b0685ec26..393853cfe 100644 --- a/poincare/src/layout_reference.cpp +++ b/poincare/src/layout_reference.cpp @@ -50,7 +50,7 @@ void LayoutReference::replaceWithJuxtapositionOf(LayoutRef leftChild, LayoutRef return; } LayoutReference p = parent(); - assert(p.isDefined()); + assert(!p.isUninitialized()); if (!p.isHorizontal()) { /* One of the children to juxtapose might be "this", so we cannot just call * replaceWith. */ @@ -130,7 +130,7 @@ void LayoutReference::addSibling(LayoutCursor * cursor, LayoutReference sibling, * root layout. */ LayoutRef rootLayout = root(); LayoutRef p = parent(); - assert(p.isDefined()); + assert(!p.isUninitialized()); if (p.isHorizontal()) { int indexInParent = p.indexOfChild(*this); int siblingIndex = cursor->position() == LayoutCursor::Position::Left ? indexInParent : indexInParent + 1; @@ -145,7 +145,7 @@ void LayoutReference::addSibling(LayoutCursor * cursor, LayoutReference sibling, } else if (cursor->position() == LayoutCursor::Position::Right && indexInParent < p.numberOfChildren() - 1) { neighbour = p.childAtIndex(indexInParent + 1); } - if (neighbour.isDefined() && neighbour.isVerticalOffset()) { + if (!neighbour.isUninitialized() && neighbour.isVerticalOffset()) { if (moveCursor) { cursor->setLayoutReference(neighbour); cursor->setPosition(cursor->position() == LayoutCursor::Position::Left ? LayoutCursor::Position::Right : LayoutCursor::Position::Left); @@ -198,7 +198,7 @@ void LayoutReference::removeChild(LayoutRef l, LayoutCursor * cursor, bool force void LayoutReference::collapseOnDirection(HorizontalDirection direction, int absorbingChildIndex) { LayoutRef p = parent(); - if (!p.isDefined() || !p.isHorizontal()) { + if (p.isUninitialized() || !p.isHorizontal()) { return; } int idxInParent = p.indexOfChild(*this); @@ -206,7 +206,7 @@ void LayoutReference::collapseOnDirection(HorizontalDirection direction, int abs int numberOfOpenParenthesis = 0; bool canCollapse = true; LayoutRef absorbingChild = childAtIndex(absorbingChildIndex); - if (!absorbingChild.isDefined() || !absorbingChild.isHorizontal()) { + if (absorbingChild.isUninitialized() || !absorbingChild.isHorizontal()) { return; } HorizontalLayoutRef horizontalAbsorbingChild = HorizontalLayoutRef(static_cast(absorbingChild.node())); diff --git a/poincare/src/matrix.cpp b/poincare/src/matrix.cpp index 6210f8535..7b01d6baa 100644 --- a/poincare/src/matrix.cpp +++ b/poincare/src/matrix.cpp @@ -183,7 +183,7 @@ Matrix Matrix::rowCanonize(Context & context, Preferences::AngleUnit angleUnit, // No non-null coefficient in this column, skip k++; // Update determinant: det *= 0 - if (determinant.isDefined()) { determinant.addChildAtIndexInPlace(Rational(0), 0, determinant.numberOfChildren()); } + if (!determinant.isUninitialized()) { determinant.addChildAtIndexInPlace(Rational(0), 0, determinant.numberOfChildren()); } } else { // Swap row h and iPivot if (iPivot != h) { @@ -191,12 +191,12 @@ Matrix Matrix::rowCanonize(Context & context, Preferences::AngleUnit angleUnit, matrix.swapChildrenInPlace(iPivot*n+col, h*n+col); } // Update determinant: det *= -1 - if (determinant.isDefined()) { determinant.addChildAtIndexInPlace(Rational(-1), 0, determinant.numberOfChildren()); } + if (!determinant.isUninitialized()) { determinant.addChildAtIndexInPlace(Rational(-1), 0, determinant.numberOfChildren()); } } /* Set to 1 M[h][k] by linear combination */ Expression divisor = matrixChild(h, k); // Update determinant: det *= divisor - if (determinant.isDefined()) { determinant.addChildAtIndexInPlace(divisor, 0, determinant.numberOfChildren()); } + if (!determinant.isUninitialized()) { determinant.addChildAtIndexInPlace(divisor, 0, determinant.numberOfChildren()); } for (int j = k+1; j < n; j++) { Expression opHJ = matrixChild(h, j); Expression newOpHJ = Division(opHJ, divisor); diff --git a/poincare/src/power.cpp b/poincare/src/power.cpp index e91083185..585d0affe 100644 --- a/poincare/src/power.cpp +++ b/poincare/src/power.cpp @@ -517,7 +517,7 @@ template MatrixComplex PowerNode::computeOnMatrixAndComplex(const } if (power < 0) { MatrixComplex inverse = m.inverse(); - if (!inverse.isDefined()) { + if (inverse.isUninitialized()) { return MatrixComplex::Undefined(); } Complex minusC = Complex(-d); diff --git a/poincare/src/symbol.cpp b/poincare/src/symbol.cpp index 811411d12..64dbc1a3a 100644 --- a/poincare/src/symbol.cpp +++ b/poincare/src/symbol.cpp @@ -156,10 +156,10 @@ Evaluation SymbolNode::templatedApproximate(Context& context, Preferences::An return Complex(0.0, 1.0); } const Expression e = context.expressionForSymbol(Symbol(m_name)); - if (e.isDefined()) { - return e.node()->approximate(T(), context, angleUnit); + if (e.isUninitialized()) { + return Complex::Undefined(); } - return Complex::Undefined(); + return e.node()->approximate(T(), context, angleUnit); } const char * Symbol::textForSpecialSymbols(char name) { @@ -302,12 +302,12 @@ Expression Symbol::shallowReduce(Context& context, Preferences::AngleUnit angleU #if 0 // Do not replace symbols in expression of type: 3->A Expression p = parent(); - if (p.isDefined() && p.type() == ExpressionNode::Type::Store && p.childAtIndex(1) == *this) { + if (!p.isUninitialized() && p.type() == ExpressionNode::Type::Store && p.childAtIndex(1) == *this) { return *this; } #endif const Expression e = context.expressionForSymbol(*this); - if (e.isDefined() && node()->hasAnExactRepresentation(context)) { + if (!e.isUninitialized() && node()->hasAnExactRepresentation(context)) { // TODO: later AZ should be replaced. /* The stored expression had been beautified which forces to call deepReduce. */ return e.deepReduce(context, angleUnit); @@ -319,7 +319,7 @@ Expression Symbol::replaceSymbolWithExpression(char symbol, Expression expressio if (name() == symbol) { Expression value = expression; Expression p = parent(); - if (p.isDefined() && value.node()->needsParenthesesWithParent(p.node())) { + if (!p.isUninitialized() && value.node()->needsParenthesesWithParent(p.node())) { value = Parenthesis(value); } return value; diff --git a/poincare/src/tree_by_reference.cpp b/poincare/src/tree_by_reference.cpp index ef72ee470..d329f642f 100644 --- a/poincare/src/tree_by_reference.cpp +++ b/poincare/src/tree_by_reference.cpp @@ -9,16 +9,14 @@ namespace Poincare { /* Constructors */ TreeByReference::~TreeByReference() { - if (isDefined()) { - assert(node()->identifier() == m_identifier); - node()->release(numberOfChildren()); //TODO No malformed nodes ? - } + assert(node()->identifier() == m_identifier); + node()->release(numberOfChildren()); //TODO No malformed nodes ? } /* Clone */ TreeByReference TreeByReference::clone() const { - if (!isDefined()){ + if (isUninitialized()){ return TreeByReference(); } TreeNode * myNode = node(); @@ -33,12 +31,12 @@ TreeByReference TreeByReference::clone() const { /* Hierarchy operations */ void TreeByReference::replaceWithInPlace(TreeByReference t) { - assert(isDefined()); + assert(!isUninitialized()); if (isAllocationFailure()) { return; } TreeByReference p = parent(); - if (p.isDefined()) { + if (!p.isUninitialized()) { p.replaceChildInPlace(*this, t); } } @@ -48,7 +46,7 @@ void TreeByReference::replaceChildInPlace(TreeByReference oldChild, TreeByRefere return; } - assert(isDefined()); + assert(!isUninitialized()); if (isAllocationFailure()) { return; } @@ -58,7 +56,7 @@ void TreeByReference::replaceChildInPlace(TreeByReference oldChild, TreeByRefere } // Move the new child - assert(!newChild.parent().isDefined()); + assert(newChild.parent().isUninitialized()); TreePool::sharedPool()->move(oldChild.node(), newChild.node(), newChild.numberOfChildren()); /* We could have moved the new node to oldChild.node()->nextSibling(), but * nextSibling is not computed correctly if we inserted an @@ -74,9 +72,9 @@ void TreeByReference::replaceWithAllocationFailureInPlace(int currentNumberOfChi if (isAllocationFailure()) { return; } - assert(isDefined()); + assert(!isUninitialized()); TreeByReference p = parent(); - bool hasParent = p.isDefined(); + bool hasParent = !p.isUninitialized(); int indexInParentNode = hasParent ? node()->indexInParent() : -1; int currentRetainCount = node()->retainCount(); TreeNode * staticAllocFailNode = node()->failedAllocationStaticNode(); @@ -100,7 +98,7 @@ void TreeByReference::replaceWithAllocationFailureInPlace(int currentNumberOfChi TreeNode * newAllocationFailureNode = TreePool::sharedPool()->deepCopy(staticAllocFailNode); newAllocationFailureNode->rename(m_identifier, true); newAllocationFailureNode->retain(); - if (p.isDefined()) { + if (hasParent) { assert(indexInParentNode >= 0); /* Set the refCount to previousRefCount-1 because the previous parent is * no longer retaining the node. When we add this node to the parent, it @@ -138,7 +136,7 @@ void TreeByReference::mergeChildrenAtIndexInPlace(TreeByReference t, int i) { } void TreeByReference::swapChildrenInPlace(int i, int j) { - assert(isDefined()); + assert(!isStatic()); assert(i >= 0 && i < numberOfChildren()); assert(j >= 0 && j < numberOfChildren()); if (i == j) { @@ -163,7 +161,7 @@ void TreeByReference::log() const { // Add void TreeByReference::addChildAtIndexInPlace(TreeByReference t, int index, int currentNumberOfChildren) { - assert(isDefined()); + assert(!isUninitialized()); if (node()->isAllocationFailure()) { return; } @@ -173,7 +171,7 @@ void TreeByReference::addChildAtIndexInPlace(TreeByReference t, int index, int c } assert(index >= 0 && index <= currentNumberOfChildren); - assert(!t.parent().isDefined()); + assert(t.parent().isUninitialized()); // Move t TreeNode * newChildPosition = node()->next(); for (int i = 0; i < index; i++) { @@ -189,21 +187,21 @@ void TreeByReference::addChildAtIndexInPlace(TreeByReference t, int index, int c // Remove void TreeByReference::removeChildAtIndexInPlace(int i) { - assert(isDefined()); + assert(!isUninitialized()); assert(i >= 0 && i < numberOfChildren()); TreeByReference t = childAtIndex(i); removeChildInPlace(t, t.numberOfChildren()); } void TreeByReference::removeChildInPlace(TreeByReference t, int childNumberOfChildren) { - assert(isDefined()); + assert(!isUninitialized()); TreePool::sharedPool()->move(TreePool::sharedPool()->last(), t.node(), childNumberOfChildren); t.node()->release(childNumberOfChildren); node()->decrementNumberOfChildren(); } void TreeByReference::removeChildrenInPlace(int currentNumberOfChildren) { - assert(isDefined()); + assert(!isUninitialized()); for (int i = 0; i < currentNumberOfChildren; i++) { TreeByReference childRef = childAtIndex(0); TreePool::sharedPool()->move(TreePool::sharedPool()->last(), childRef.node(), childRef.numberOfChildren()); diff --git a/poincare/src/variable_context.cpp b/poincare/src/variable_context.cpp index f3f202d8f..e128bed15 100644 --- a/poincare/src/variable_context.cpp +++ b/poincare/src/variable_context.cpp @@ -22,7 +22,7 @@ void VariableContext::setApproximationForVariable(T value) { template void VariableContext::setExpressionForSymbolName(const Expression expression, const Symbol symbol, Context & context) { if (symbol.name() == m_name) { - if (!expression.isDefined()) { + if (expression.isUninitialized()) { return; } m_value = Float(expression.approximateToScalar(context, Preferences::sharedPreferences()->angleUnit())); diff --git a/poincare/src/vertical_offset_layout_node.cpp b/poincare/src/vertical_offset_layout_node.cpp index 85d43bae2..5d2e9cb08 100644 --- a/poincare/src/vertical_offset_layout_node.cpp +++ b/poincare/src/vertical_offset_layout_node.cpp @@ -271,7 +271,7 @@ bool VerticalOffsetLayoutNode::willAddSibling(LayoutCursor * cursor, LayoutNode assert(cursor->position() == LayoutCursor::Position::Left); parentRef.addChildAtIndex(rightParenthesis, idxInParent, parentRef.numberOfChildren(), nullptr); } - if (rightParenthesis.parent().isDefined()) { + if (!rightParenthesis.parent().isUninitialized()) { cursor->setLayoutReference(rightParenthesis); } if (rootLayout.isAllocationFailure()) { diff --git a/poincare/test/helper.cpp b/poincare/test/helper.cpp index 1f0cbf92a..e8c949add 100644 --- a/poincare/test/helper.cpp +++ b/poincare/test/helper.cpp @@ -49,7 +49,7 @@ Expression parse_expression(const char * expression) { strlcpy(buffer, expression, sizeof(buffer)); translate_in_special_chars(buffer); Expression result = Expression::parse(buffer); - assert(result.isDefined()); + assert(!result.isUninitialized()); return result; }