[shared] Fix references to Poincare

This commit is contained in:
Émilie Feral
2018-08-10 13:17:07 +02:00
parent b5f66171fc
commit 17e63a5b07
11 changed files with 37 additions and 60 deletions

View File

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

View File

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

View File

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

View File

@@ -41,7 +41,7 @@ void Function::setActive(bool active) {
template<typename T>
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());
}
}

View File

@@ -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());
}
}

View File

@@ -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);
}

View File

@@ -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<double> m_value;
Poincare::Float<double> m_value;
};
}

View File

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

View File

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

View File

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

View File

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