Skip to content

Commit 78b1246

Browse files
author
Dilawar Singh
committed
Parser is a member object rather than member pointer; still some leak
but better version than before. [skip ci].
1 parent 5655a44 commit 78b1246

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

builtins/Function.cpp

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,17 @@ const Cinfo * Function::initCinfo()
304304
static const Cinfo * functionCinfo = Function::initCinfo();
305305

306306
Function::Function():
307-
valid_(false), numVar_(0), lastValue_(0.0)
308-
, value_(0.0), rate_(0.0), mode_(1)
309-
, useTrigger_(false), doEvalAtReinit_(false)
307+
valid_(false)
308+
, numVar_(0)
309+
, lastValue_(0.0)
310+
, value_(0.0)
311+
, rate_(0.0)
312+
, mode_(1)
313+
, useTrigger_(false)
314+
, doEvalAtReinit_(false)
310315
, t_(0.0)
311316
, independent_("x0")
312317
, stoich_(nullptr)
313-
, parser_(new moose::MooseParser())
314318
{
315319
}
316320

@@ -326,9 +330,9 @@ Function::Function(const Function& f) :
326330
independent_(f.independent_),
327331
xs_(f.xs_),
328332
ys_(f.ys_),
329-
stoich_(f.stoich_),
330-
parser_(f.parser_)
333+
stoich_(f.stoich_)
331334
{
335+
parser_.LinkVariables(xs_, ys_, &t_);
332336
}
333337

334338
// Careful: This is a critical function.
@@ -347,17 +351,16 @@ Function& Function::operator=(const Function& rhs)
347351
t_ = rhs.t_;
348352
rate_ = rhs.rate_;
349353
independent_ = rhs.independent_;
350-
parser_ = rhs.parser_;
354+
xs_ = rhs.xs_;
355+
ys_ = rhs.ys_;
356+
parser_.LinkVariables(xs_, ys_, &t_);
351357
return *this;
352358
}
353359

354360
Function::~Function()
355361
{
356362
for (size_t i = 0; i < ys_.size(); i++)
357363
delete ys_[i];
358-
359-
// if(parser_)
360-
// delete parser_;
361364
}
362365

363366

@@ -367,7 +370,7 @@ Function::~Function()
367370
void Function::clearBuffer()
368371
{
369372
numVar_ = 0;
370-
parser_->ClearVariables();
373+
parser_.ClearVariables();
371374
}
372375

373376
void Function::showError(moose::Parser::exception_type &e) const
@@ -409,12 +412,12 @@ void Function::addVariable(const string& name)
409412
{
410413
// Equality with index because we cound from 0.
411414
for (size_t i = xs_.size(); i <= (size_t) index; i++)
412-
xs_.push_back( new Variable() );
415+
xs_.push_back(new Variable(name));
413416
}
414417

415418
// This must be true.
416419
if( xs_[index] )
417-
parser_->DefineVar(name, xs_[index]->ref());
420+
parser_.DefineVar(name, xs_[index]->ref());
418421
else
419422
throw runtime_error( "Empty Variable." );
420423
numVar_ = xs_.size();
@@ -428,12 +431,12 @@ void Function::addVariable(const string& name)
428431
{
429432
// Equality with index because we cound from 0.
430433
for (size_t i = ys_.size(); i <= (size_t) index; i++)
431-
ys_.push_back( new double(0.0));
434+
ys_.push_back(new double(0.0));
432435
}
433-
parser_->DefineVar(name, ys_[index]);
436+
parser_.DefineVar(name, ys_[index]);
434437
}
435438
else if (name == "t")
436-
parser_->DefineVar("t", &t_);
439+
parser_.DefineVar("t", &t_);
437440
else
438441
{
439442
MOOSE_WARN( "Got an undefined symbol: " << name << endl
@@ -464,7 +467,7 @@ void Function::setExpr(const Eref& eref, const string expression)
464467
return;
465468
}
466469

467-
if(valid_ && expr == parser_->GetExpr())
470+
if(valid_ && expr == parser_.GetExpr())
468471
{
469472
MOOSE_WARN( "No change in expression.");
470473
return;
@@ -495,24 +498,24 @@ bool Function::innerSetExpr(const Eref& eref, const string expr)
495498
// Now create a map which maps the variable name to location of values. This
496499
// is critical to make sure that pointers remain valid when multi-threaded
497500
// encironment is used.
498-
for(auto &x : xs) addVariable(x.c_str());
499-
for(auto &y : ys) addVariable(y.c_str());
501+
for(auto &x : xs) addVariable(x);
502+
for(auto &y : ys) addVariable(y);
500503
addVariable("t");
501504

502505
// Set parser expression. Note that the symbol table is popultated by
503506
// addVariable function above.
504-
return parser_->SetExpr( expr );
507+
return parser_.SetExpr( expr );
505508
}
506509

507510
string Function::getExpr( const Eref& e ) const
508511
{
509512
if (!valid_)
510513
{
511514
cout << "Error: " << e.objId().path() << "::getExpr() - invalid parser state" << endl;
512-
cout << "\tExpression was : " << parser_->GetExpr() << endl;
515+
cout << "\tExpression was : " << parser_.GetExpr() << endl;
513516
return "";
514517
}
515-
return parser_->GetExpr();
518+
return parser_.GetExpr();
516519
}
517520

518521
void Function::setMode(unsigned int mode)
@@ -547,7 +550,7 @@ bool Function::getDoEvalAtReinit() const
547550

548551
double Function::getValue() const
549552
{
550-
return parser_->Eval( );
553+
return parser_.Eval( );
551554
}
552555

553556

@@ -588,7 +591,7 @@ double Function::getDerivative() const
588591
cout << "Error: Function::getDerivative() - invalid state" << endl;
589592
return value;
590593
}
591-
return parser_->Derivative(independent_);
594+
return parser_.Derivative(independent_);
592595
}
593596

594597
void Function::setNumVar(const unsigned int num)
@@ -626,12 +629,12 @@ Variable * Function::getVar(unsigned int ii)
626629

627630
void Function::setConst(string name, double value)
628631
{
629-
parser_->DefineConst(name.c_str(), value);
632+
parser_.DefineConst(name.c_str(), value);
630633
}
631634

632635
double Function::getConst(string name) const
633636
{
634-
moose::Parser::varmap_type cmap = parser_->GetConst();
637+
moose::Parser::varmap_type cmap = parser_.GetConst();
635638
if (! cmap.empty() )
636639
{
637640
moose::Parser::varmap_type::const_iterator it = cmap.find(name);
@@ -658,7 +661,7 @@ void Function::process(const Eref &e, ProcPtr p)
658661

659662
#ifdef DEBUG_THIS_FILE
660663
cout << "t= " << t_ << " value: " << getValue() << ", expr: "
661-
<< parser_->GetExpr() << endl;
664+
<< parser_.GetExpr() << endl;
662665
#endif
663666

664667
for (size_t ii = 0; (ii < databuf.size()) && (ii < ys_.size()); ++ii)
@@ -700,10 +703,10 @@ void Function::process(const Eref &e, ProcPtr p)
700703

701704
void Function::reinit(const Eref &e, ProcPtr p)
702705
{
703-
if (! (valid_ || parser_->GetExpr().empty()))
706+
if (! (valid_ || parser_.GetExpr().empty()))
704707
{
705708
cout << "Error: " << e.objId().path() << "::reinit() - invalid parser state" << endl;
706-
cout << " Expr: '" << parser_->GetExpr() << "'" << endl;
709+
cout << " Expr: '" << parser_.GetExpr() << "'" << endl;
707710
return;
708711
}
709712

builtins/Function.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class Function
120120
// Parser which should never be copied. Multithreaded programs may behave
121121
// strangely if copy-constructor or operator()= is implemented.
122122
// moose::MooseParser parser_;
123-
moose::MooseParser* parser_;
123+
moose::MooseParser parser_;
124124

125125
};
126126

0 commit comments

Comments
 (0)