Refactoring. Use QScopedPointer for class Calculator.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2016-05-24 15:25:58 +03:00
parent 83243e5af3
commit 9c0f5f2fa4
9 changed files with 12 additions and 27 deletions

View File

@ -2421,9 +2421,8 @@ bool TMainWindow::EvalFormula(const QString &formula, bool fromUser, VContainer
f = formula; f = formula;
} }
f.replace("\n", " "); f.replace("\n", " ");
Calculator *cal = new Calculator(); QScopedPointer<Calculator> cal(new Calculator());
qreal result = cal->EvalFormula(data->PlainVariables(), f); qreal result = cal->EvalFormula(data->PlainVariables(), f);
delete cal;
if (qIsInf(result) || qIsNaN(result)) if (qIsInf(result) || qIsNaN(result))
{ {

View File

@ -310,9 +310,8 @@ bool DialogIncrements::EvalIncrementFormula(const QString &formula, bool fromUse
f = formula; f = formula;
} }
f.replace("\n", " "); f.replace("\n", " ");
Calculator *cal = new Calculator(); QScopedPointer<Calculator> cal(new Calculator());
const qreal result = cal->EvalFormula(data->PlainVariables(), f); const qreal result = cal->EvalFormula(data->PlainVariables(), f);
delete cal;
if (qIsInf(result) || qIsNaN(result)) if (qIsInf(result) || qIsNaN(result))
{ {

View File

@ -2455,9 +2455,8 @@ qreal VPattern::EvalFormula(VContainer *data, const QString &formula, bool *ok)
// Replace line return character with spaces for calc if exist // Replace line return character with spaces for calc if exist
QString f = formula; QString f = formula;
f.replace("\n", " "); f.replace("\n", " ");
Calculator *cal = new Calculator(); QScopedPointer<Calculator> cal(new Calculator());
const qreal result = cal->EvalFormula(data->PlainVariables(), f); const qreal result = cal->EvalFormula(data->PlainVariables(), f);
delete cal;
(qIsInf(result) || qIsNaN(result)) ? *ok = false : *ok = true; (qIsInf(result) || qIsNaN(result)) ? *ok = false : *ok = true;
return result; return result;

View File

@ -879,9 +879,8 @@ qreal VMeasurements::EvalFormula(VContainer *data, const QString &formula, bool
// Replace line return character with spaces for calc if exist // Replace line return character with spaces for calc if exist
QString f = formula; QString f = formula;
f.replace("\n", " "); f.replace("\n", " ");
Calculator *cal = new Calculator(); QScopedPointer<Calculator> cal(new Calculator());
const qreal result = cal->EvalFormula(data->PlainVariables(), f); const qreal result = cal->EvalFormula(data->PlainVariables(), f);
delete cal;
(qIsInf(result) || qIsNaN(result)) ? *ok = false : *ok = true; (qIsInf(result) || qIsNaN(result)) ? *ok = false : *ok = true;
return result; return result;

View File

@ -48,9 +48,8 @@ class VContainer;
* //Need delete dialog here because parser in dialog don't allow use correct separator for parsing here. * //Need delete dialog here because parser in dialog don't allow use correct separator for parsing here.
* //Don't know why. * //Don't know why.
* delete dialog; * delete dialog;
* Calculator *cal = new Calculator(); * QScopedPointer<Calculator> cal(new Calculator());
* result = cal->EvalFormula(data->PlainVariables(), formula); * result = cal->EvalFormula(data->PlainVariables(), formula);
* delete cal;//Here can be memory leak, but dialog already check this formula and probability very low.
* } * }
*/ */
class Calculator:public qmu::QmuFormulaBase class Calculator:public qmu::QmuFormulaBase

View File

@ -234,10 +234,9 @@ void VFormula::Eval()
{ {
try try
{ {
Calculator *cal = new Calculator(); QScopedPointer<Calculator> cal(new Calculator());
QString expression = qApp->TrVars()->FormulaFromUser(formula, qApp->Settings()->GetOsSeparator()); QString expression = qApp->TrVars()->FormulaFromUser(formula, qApp->Settings()->GetOsSeparator());
const qreal result = cal->EvalFormula(data->PlainVariables(), expression); const qreal result = cal->EvalFormula(data->PlainVariables(), expression);
delete cal;
if (qIsInf(result) || qIsNaN(result)) if (qIsInf(result) || qIsNaN(result))
{ {

View File

@ -449,9 +449,8 @@ qreal DialogTool::Eval(const QString &text, bool &flag, QLabel *label, const QSt
formula.replace("\n", " "); formula.replace("\n", " ");
// Translate to internal look. // Translate to internal look.
formula = qApp->TrVars()->FormulaFromUser(formula, qApp->Settings()->GetOsSeparator()); formula = qApp->TrVars()->FormulaFromUser(formula, qApp->Settings()->GetOsSeparator());
Calculator *cal = new Calculator(); QScopedPointer<Calculator> cal(new Calculator());
result = cal->EvalFormula(data->PlainVariables(), formula); result = cal->EvalFormula(data->PlainVariables(), formula);
delete cal;
if (qIsInf(result) || qIsNaN(result)) if (qIsInf(result) || qIsNaN(result))
{ {

View File

@ -294,9 +294,8 @@ qreal VDrawTool::CheckFormula(const quint32 &toolId, QString &formula, VContaine
Calculator *cal = nullptr; Calculator *cal = nullptr;
try try
{ {
cal = new Calculator(); QScopedPointer<Calculator> cal(new Calculator());
result = cal->EvalFormula(data->PlainVariables(), formula); result = cal->EvalFormula(data->PlainVariables(), formula);
delete cal;
if (qIsInf(result) || qIsNaN(result)) if (qIsInf(result) || qIsNaN(result))
{ {
@ -311,11 +310,10 @@ qreal VDrawTool::CheckFormula(const quint32 &toolId, QString &formula, VContaine
<< "Message: " << e.GetMsg() << "\n" << "Message: " << e.GetMsg() << "\n"
<< "Expression: " << e.GetExpr() << "\n" << "Expression: " << e.GetExpr() << "\n"
<< "--------------------------------------"; << "--------------------------------------";
delete cal;
if (qApp->IsAppInGUIMode()) if (qApp->IsAppInGUIMode())
{ {
DialogUndo *dialogUndo = new DialogUndo(qApp->getMainWindow()); QScopedPointer<DialogUndo> dialogUndo(new DialogUndo(qApp->getMainWindow()));
forever forever
{ {
if (dialogUndo->exec() == QDialog::Accepted) if (dialogUndo->exec() == QDialog::Accepted)
@ -323,8 +321,7 @@ qreal VDrawTool::CheckFormula(const quint32 &toolId, QString &formula, VContaine
const UndoButton resultUndo = dialogUndo->Result(); const UndoButton resultUndo = dialogUndo->Result();
if (resultUndo == UndoButton::Fix) if (resultUndo == UndoButton::Fix)
{ {
DialogEditWrongFormula *dialog = new DialogEditWrongFormula(data, toolId, auto *dialog = new DialogEditWrongFormula(data, toolId, qApp->getMainWindow());
qApp->getMainWindow());
dialog->setWindowTitle(tr("Edit wrong formula")); dialog->setWindowTitle(tr("Edit wrong formula"));
dialog->SetFormula(formula); dialog->SetFormula(formula);
if (dialog->exec() == QDialog::Accepted) if (dialog->exec() == QDialog::Accepted)
@ -333,10 +330,8 @@ qreal VDrawTool::CheckFormula(const quint32 &toolId, QString &formula, VContaine
/* Need delete dialog here because parser in dialog don't allow use correct separator for /* Need delete dialog here because parser in dialog don't allow use correct separator for
* parsing here. */ * parsing here. */
delete dialog; delete dialog;
Calculator *cal1 = new Calculator(); QScopedPointer<Calculator> cal1(new Calculator());
result = cal1->EvalFormula(data->PlainVariables(), formula); result = cal1->EvalFormula(data->PlainVariables(), formula);
delete cal1; /* Here can be memory leak, but dialog already checked this formula and
probability very low. */
if (qIsInf(result) || qIsNaN(result)) if (qIsInf(result) || qIsNaN(result))
{ {
@ -358,11 +353,9 @@ qreal VDrawTool::CheckFormula(const quint32 &toolId, QString &formula, VContaine
} }
else else
{ {
delete dialogUndo;
throw; throw;
} }
} }
delete dialogUndo;
} }
else else
{ {

View File

@ -158,9 +158,8 @@ qreal Visualization::FindVal(const QString &expression, const QHash<QString, qre
QString formula = expression; QString formula = expression;
formula.replace("\n", " "); formula.replace("\n", " ");
formula = qApp->TrVars()->FormulaFromUser(formula, qApp->Settings()->GetOsSeparator()); formula = qApp->TrVars()->FormulaFromUser(formula, qApp->Settings()->GetOsSeparator());
Calculator *cal = new Calculator(); QScopedPointer<Calculator> cal(new Calculator());
val = cal->EvalFormula(vars, formula); val = cal->EvalFormula(vars, formula);
delete cal;
if (qIsInf(val) || qIsNaN(val)) if (qIsInf(val) || qIsNaN(val))
{ {