PlannerObjectiveStatement: turn model_tree into a std::unique_ptr

Avoids an unnecessary copy.
master
Sébastien Villemot 2023-12-01 14:59:20 +01:00
parent 5e89921ea1
commit c5cc61b110
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
3 changed files with 17 additions and 16 deletions

View File

@ -2400,8 +2400,8 @@ ModelComparisonStatement::writeJsonOutput(ostream& output) const
output << "}"; output << "}";
} }
PlannerObjectiveStatement::PlannerObjectiveStatement(const PlannerObjective& model_tree_arg) : PlannerObjectiveStatement::PlannerObjectiveStatement(unique_ptr<PlannerObjective> model_tree_arg) :
model_tree {model_tree_arg} model_tree {move(model_tree_arg)}
{ {
} }
@ -2409,8 +2409,8 @@ void
PlannerObjectiveStatement::checkPass(ModFileStructure& mod_file_struct, PlannerObjectiveStatement::checkPass(ModFileStructure& mod_file_struct,
[[maybe_unused]] WarningConsolidation& warnings) [[maybe_unused]] WarningConsolidation& warnings)
{ {
assert(model_tree.equation_number() == 1); assert(model_tree->equation_number() == 1);
if (model_tree.exoPresentInEqs()) if (model_tree->exoPresentInEqs())
{ {
cerr << "ERROR: You cannot include exogenous variables (or variables of undeclared type) in " cerr << "ERROR: You cannot include exogenous variables (or variables of undeclared type) in "
"the planner objective. Please " "the planner objective. Please "
@ -2424,13 +2424,13 @@ PlannerObjectiveStatement::checkPass(ModFileStructure& mod_file_struct,
const PlannerObjective& const PlannerObjective&
PlannerObjectiveStatement::getPlannerObjective() const PlannerObjectiveStatement::getPlannerObjective() const
{ {
return model_tree; return *model_tree;
} }
void void
PlannerObjectiveStatement::computingPass(const ModFileStructure& mod_file_struct) PlannerObjectiveStatement::computingPass(const ModFileStructure& mod_file_struct)
{ {
model_tree.computingPass(max(3, mod_file_struct.order_option), 0, {}, false, false, false); model_tree->computingPass(max(3, mod_file_struct.order_option), 0, {}, false, false, false);
computing_pass_called = true; computing_pass_called = true;
} }
@ -2439,14 +2439,14 @@ PlannerObjectiveStatement::writeOutput(ostream& output, const string& basename,
[[maybe_unused]] bool minimal_workspace) const [[maybe_unused]] bool minimal_workspace) const
{ {
output << "M_.NNZDerivatives_objective = ["; output << "M_.NNZDerivatives_objective = [";
for (int i = 1; i < static_cast<int>(model_tree.getNNZDerivatives().size()); i++) for (int i = 1; i < static_cast<int>(model_tree->getNNZDerivatives().size()); i++)
output << (i > model_tree.getComputedDerivsOrder() ? -1 : model_tree.getNNZDerivatives()[i]) output << (i > model_tree->getComputedDerivsOrder() ? -1 : model_tree->getNNZDerivatives()[i])
<< ";"; << ";";
output << "];" << endl << "M_.objective_tmp_nbr = ["; output << "];" << endl << "M_.objective_tmp_nbr = [";
for (const auto& temporary_terms_derivative : model_tree.getTemporaryTermsDerivatives()) for (const auto& temporary_terms_derivative : model_tree->getTemporaryTermsDerivatives())
output << temporary_terms_derivative.size() << "; "; output << temporary_terms_derivative.size() << "; ";
output << "];" << endl; output << "];" << endl;
model_tree.writeStaticFile(basename + ".objective", false, "", {}, false); model_tree->writeStaticFile(basename + ".objective", false, "", {}, false);
} }
void void
@ -2455,9 +2455,9 @@ PlannerObjectiveStatement::writeJsonOutput(ostream& output) const
output << R"({"statementName": "planner_objective")" output << R"({"statementName": "planner_objective")"
<< ", "; << ", ";
if (computing_pass_called) if (computing_pass_called)
model_tree.writeJsonComputingPassOutput(output, false); model_tree->writeJsonComputingPassOutput(output, false);
else else
model_tree.writeJsonOutput(output); model_tree->writeJsonOutput(output);
output << "}"; output << "}";
} }

View File

@ -20,6 +20,7 @@
#ifndef _COMPUTINGTASKS_HH #ifndef _COMPUTINGTASKS_HH
#define _COMPUTINGTASKS_HH #define _COMPUTINGTASKS_HH
#include <memory>
#include <optional> #include <optional>
#include <ostream> #include <ostream>
@ -618,11 +619,11 @@ public:
class PlannerObjectiveStatement : public Statement class PlannerObjectiveStatement : public Statement
{ {
private: private:
PlannerObjective model_tree; unique_ptr<PlannerObjective> model_tree;
bool computing_pass_called {false}; bool computing_pass_called {false};
public: public:
explicit PlannerObjectiveStatement(const PlannerObjective& model_tree_arg); explicit PlannerObjectiveStatement(unique_ptr<PlannerObjective> model_tree_arg);
/*! \todo check there are only endogenous variables at the current period in the objective /*! \todo check there are only endogenous variables at the current period in the objective
(no exogenous, no lead/lag) */ (no exogenous, no lead/lag) */
void checkPass(ModFileStructure& mod_file_struct, WarningConsolidation& warnings) override; void checkPass(ModFileStructure& mod_file_struct, WarningConsolidation& warnings) override;
@ -631,7 +632,7 @@ public:
void writeOutput(ostream& output, const string& basename, bool minimal_workspace) const override; void writeOutput(ostream& output, const string& basename, bool minimal_workspace) const override;
void writeJsonOutput(ostream& output) const override; void writeJsonOutput(ostream& output) const override;
//! Return a reference the Planner Objective model tree //! Return a reference the Planner Objective model tree
const PlannerObjective& getPlannerObjective() const; [[nodiscard]] const PlannerObjective& getPlannerObjective() const;
}; };
class BVARDensityStatement : public Statement class BVARDensityStatement : public Statement

View File

@ -2235,7 +2235,7 @@ ParsingDriver::end_planner_objective(expr_t expr)
expr_t eq = model_tree->AddEqual(expr, model_tree->Zero); expr_t eq = model_tree->AddEqual(expr, model_tree->Zero);
model_tree->addEquation(eq, location.begin.line); model_tree->addEquation(eq, location.begin.line);
mod_file->addStatement(make_unique<PlannerObjectiveStatement>(*planner_objective)); mod_file->addStatement(make_unique<PlannerObjectiveStatement>(move(planner_objective)));
// Handle undeclared variables (see #81) // Handle undeclared variables (see #81)
bool exit_after_write = false; bool exit_after_write = false;