Making class Calculator independent from class VContainer.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2015-10-15 13:07:43 +03:00
parent 4fbae45406
commit 5cd9a88761
12 changed files with 46 additions and 73 deletions

View File

@ -2171,8 +2171,8 @@ bool TMainWindow::EvalFormula(const QString &formula, bool fromUser, VContainer
f = formula; f = formula;
} }
f.replace("\n", " "); f.replace("\n", " ");
Calculator *cal = new Calculator(data, mType); Calculator *cal = new Calculator();
const qreal result = UnitConvertor(cal->EvalFormula(f), mUnit, pUnit); const qreal result = UnitConvertor(cal->EvalFormula(data->PlainVariables(), f), mUnit, pUnit);
delete cal; delete cal;
label->setText(qApp->LocaleToString(result) + " " +postfix); label->setText(qApp->LocaleToString(result) + " " +postfix);

View File

@ -323,8 +323,8 @@ bool DialogIncrements::EvalIncrementFormula(const QString &formula, bool fromUse
f = formula; f = formula;
} }
f.replace("\n", " "); f.replace("\n", " ");
Calculator *cal = new Calculator(data, qApp->patternType()); Calculator *cal = new Calculator();
const qreal result = cal->EvalFormula(f); const qreal result = cal->EvalFormula(data->PlainVariables(), f);
delete cal; delete cal;
label->setText(qApp->LocaleToString(result) + " " + postfix); label->setText(qApp->LocaleToString(result) + " " + postfix);

View File

@ -2043,8 +2043,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(data, qApp->patternType()); Calculator *cal = new Calculator();
const qreal result = cal->EvalFormula(f); const qreal result = cal->EvalFormula(data->PlainVariables(), f);
delete cal; delete cal;
*ok = true; *ok = true;

View File

@ -829,8 +829,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(data, type); Calculator *cal = new Calculator();
const qreal result = cal->EvalFormula(f); const qreal result = cal->EvalFormula(data->PlainVariables(), f);
delete cal; delete cal;
*ok = true; *ok = true;

View File

@ -43,15 +43,14 @@ using namespace qmu;
* *
* const QString formula = qApp->FormulaFromUser(edit->text()); * const QString formula = qApp->FormulaFromUser(edit->text());
* Calculator *cal = new Calculator(data, patternType); * Calculator *cal = new Calculator(data, patternType);
* const qreal result = cal->EvalFormula(formula); * const qreal result = cal->EvalFormula(data->PlainVariables(), formula);
* delete cal; * delete cal;
* *
* @param data pointer to a variable container. * @param data pointer to a variable container.
*/ */
Calculator::Calculator(const VContainer *data, MeasurementsType patternType) Calculator::Calculator()
:QmuFormulaBase(), vVarVal(nullptr), data(data), patternType(patternType) :QmuFormulaBase()
{ {
SCASSERT(data != nullptr)
InitCharacterSets(); InitCharacterSets();
setAllowSubexpressions(false);//Only one expression per time setAllowSubexpressions(false);//Only one expression per time
@ -61,7 +60,6 @@ Calculator::Calculator(const VContainer *data, MeasurementsType patternType)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
Calculator::~Calculator() Calculator::~Calculator()
{ {
delete [] vVarVal;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -74,7 +72,7 @@ Calculator::~Calculator()
* @param formula string of formula. * @param formula string of formula.
* @return value of formula. * @return value of formula.
*/ */
qreal Calculator::EvalFormula(const QString &formula) qreal Calculator::EvalFormula(const QHash<QString, qreal *> &vars, const QString &formula)
{ {
// Parser doesn't know any variable on this stage. So, we just use variable factory that for each unknown variable // Parser doesn't know any variable on this stage. So, we just use variable factory that for each unknown variable
// set value to 0. // set value to 0.
@ -106,7 +104,7 @@ qreal Calculator::EvalFormula(const QString &formula)
} }
// Add variables to parser because we have deal with expression with variables. // Add variables to parser because we have deal with expression with variables.
InitVariables(data, tokens, formula); InitVariables(vars, tokens, formula);
return Eval(); return Eval();
} }
@ -116,51 +114,26 @@ qreal Calculator::EvalFormula(const QString &formula)
* *
* For optimization purpose we try don't add variables that we don't need. * For optimization purpose we try don't add variables that we don't need.
* *
* @param data pointer to a variable container. Hold all informations about variables. * @param vars list of variables.
* @param tokens all tokens (measurements names, variables with lengths) that parser have found in expression. * @param tokens all tokens (measurements names, variables with lengths) that parser have found in expression.
* @param formula expression, need for throwing better error message. * @param formula expression, need for throwing better error message.
*/ */
void Calculator::InitVariables(const VContainer *data, const QMap<int, QString> &tokens, const QString &formula) void Calculator::InitVariables(const QHash<QString, qreal *> &vars, const QMap<int, QString> &tokens,
const QString &formula)
{ {
if (patternType == MeasurementsType::Standard)
{
vVarVal = new qreal[2]; //stabdard measurements table have two additional variables
}
SCASSERT(data != nullptr)
const QHash<QString, QSharedPointer<VInternalVariable> > *vars = data->DataVariables();
QMap<int, QString>::const_iterator i = tokens.constBegin(); QMap<int, QString>::const_iterator i = tokens.constBegin();
while (i != tokens.constEnd()) while (i != tokens.constEnd())
{ {
bool found = false; bool found = false;
if (vars->contains(i.value())) if (vars.contains(i.value()))
{ {
QSharedPointer<VInternalVariable> var = vars->value(i.value()); DefineVar(i.value(), vars.value(i.value()));
if (patternType == MeasurementsType::Standard && var->GetType() == VarType::Measurement)
{
QSharedPointer<VVariable> m = data->GetVariable<VVariable>(i.value());
m->SetValue(data->size(), data->height(), *data->GetPatternUnit());
}
DefineVar(i.value(), var->GetValue());
found = true; found = true;
} }
if (patternType == MeasurementsType::Standard) if (found == false && builInFunctions.contains(i.value()))
{ {// We have found built-in function
if (i.value() == data->SizeName()) found = true;
{
vVarVal[0] = data->size();
DefineVar(data->SizeName(), &vVarVal[0]);
found = true;
}
if (i.value() == data->HeightName())
{
vVarVal[1] = data->height();
DefineVar(data->HeightName(), &vVarVal[1]);
found = true;
}
} }
if (found == false) if (found == false)

View File

@ -48,23 +48,21 @@ 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(data); * Calculator *cal = new Calculator();
* result = cal->EvalFormula(formula); * result = cal->EvalFormula(data->PlainVariables(), formula);
* delete cal;//Here can be memory leak, but dialog already check this formula and probability very low. * 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
{ {
public: public:
Calculator(const VContainer *data, MeasurementsType patternType); Calculator();
virtual ~Calculator(); virtual ~Calculator();
qreal EvalFormula(const QString &formula); qreal EvalFormula(const QHash<QString, qreal *> &vars, const QString &formula);
private: private:
Q_DISABLE_COPY(Calculator) Q_DISABLE_COPY(Calculator)
qreal *vVarVal; void InitVariables(const QHash<QString, qreal *> &vars, const QMap<int, QString> &tokens,
const VContainer *data; const QString &formula);
MeasurementsType patternType;
void InitVariables(const VContainer *data, const QMap<int, QString> &tokens, const QString &formula);
static void RemoveAll(QMap<int, QString> &map, const QString &val); static void RemoveAll(QMap<int, QString> &map, const QString &val);
}; };

View File

@ -489,9 +489,9 @@ const QMap<QString, QSharedPointer<VSplineAngle> > VContainer::DataAnglesCurves(
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
const QHash<QString, const qreal *> VContainer::PlainVariables() const const QHash<QString, qreal *> VContainer::PlainVariables() const
{ {
QHash<QString, const qreal *> vars; QHash<QString, qreal *> vars;
QHash<QString, QSharedPointer<VInternalVariable>>::const_iterator i = d->variables.constBegin(); QHash<QString, QSharedPointer<VInternalVariable>>::const_iterator i = d->variables.constBegin();
while (i != d->variables.constEnd()) while (i != d->variables.constEnd())
@ -504,6 +504,8 @@ const QHash<QString, const qreal *> VContainer::PlainVariables() const
m->SetValue(size(), height(), qApp->patternUnit()); m->SetValue(size(), height(), qApp->patternUnit());
} }
vars.insert(i.key(), var->GetValue()); vars.insert(i.key(), var->GetValue());
++i;
} }
if (qApp->patternType() == MeasurementsType::Standard) if (qApp->patternType() == MeasurementsType::Standard)
@ -619,7 +621,7 @@ qreal VContainer::size()
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
const qreal *VContainer::rsize() qreal *VContainer::rsize()
{ {
return &_size; return &_size;
} }
@ -641,7 +643,7 @@ qreal VContainer::height()
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
const qreal *VContainer::rheight() qreal *VContainer::rheight()
{ {
return &_height; return &_height;
} }

View File

@ -138,10 +138,10 @@ public:
static void SetHeight(qreal height); static void SetHeight(qreal height);
void SetHeightName(const QString &name); void SetHeightName(const QString &name);
static qreal size(); static qreal size();
static const qreal *rsize(); static qreal *rsize();
QString SizeName() const; QString SizeName() const;
static qreal height(); static qreal height();
static const qreal *rheight(); static qreal *rheight();
QString HeightName()const; QString HeightName()const;
bool VariableExist(const QString& name); bool VariableExist(const QString& name);
@ -162,7 +162,7 @@ public:
const QMap<QString, QSharedPointer<VArcAngle> > DataAnglesArcs() const; const QMap<QString, QSharedPointer<VArcAngle> > DataAnglesArcs() const;
const QMap<QString, QSharedPointer<VSplineAngle> > DataAnglesCurves() const; const QMap<QString, QSharedPointer<VSplineAngle> > DataAnglesCurves() const;
const QHash<QString, const qreal *> PlainVariables() const; const QHash<QString, qreal *> PlainVariables() const;
static bool IsUnique(const QString &name); static bool IsUnique(const QString &name);

View File

@ -226,9 +226,9 @@ void VFormula::Eval()
{ {
try try
{ {
Calculator *cal = new Calculator(data, qApp->patternType()); 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(expression); const qreal result = cal->EvalFormula(data->PlainVariables(), expression);
delete cal; delete cal;
//if result equal 0 //if result equal 0

View File

@ -364,8 +364,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(data, qApp->patternType()); Calculator *cal = new Calculator();
result = cal->EvalFormula(formula); result = cal->EvalFormula(data->PlainVariables(), formula);
delete cal; delete cal;
//if result equal 0 //if result equal 0

View File

@ -285,8 +285,8 @@ qreal VDrawTool::CheckFormula(const quint32 &toolId, QString &formula, VContaine
Calculator *cal = nullptr; Calculator *cal = nullptr;
try try
{ {
cal = new Calculator(data, qApp->patternType()); cal = new Calculator();
result = cal->EvalFormula(formula); result = cal->EvalFormula(data->PlainVariables(), formula);
delete cal; delete cal;
} }
catch (qmu::QmuParserError &e) catch (qmu::QmuParserError &e)
@ -316,8 +316,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(data, qApp->patternType()); Calculator *cal1 = new Calculator();
result = cal1->EvalFormula(formula); result = cal1->EvalFormula(data->PlainVariables(), formula);
delete cal1; /* Here can be memory leak, but dialog already check this formula and probability delete cal1; /* Here can be memory leak, but dialog already check this formula and probability
* very low. */ * very low. */
break; break;

View File

@ -147,8 +147,8 @@ qreal Visualization::FindVal(const QString &expression)
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(Visualization::data, qApp->patternType()); Calculator *cal = new Calculator();
val = cal->EvalFormula(formula); val = cal->EvalFormula(data->PlainVariables(), formula);
delete cal; delete cal;
} }
catch (qmu::QmuParserError &e) catch (qmu::QmuParserError &e)