Skip to content

Commit

Permalink
Prefer reference over pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMarechal25 committed May 24, 2024
1 parent 3f98747 commit 2cef96a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
36 changes: 21 additions & 15 deletions src/api/SimulationObserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,45 @@
*/

#include "SimulationObserver.h"

#include "antares/solver/optimisation/HebdoProblemToLpsTranslator.h"

namespace Antares::API
{
namespace
{
auto translate(const PROBLEME_HEBDO* problemeHebdo,
std::string_view name,
const Solver::HebdoProblemToLpsTranslator& translator,
const unsigned int year,
const unsigned int week)
auto translate(const PROBLEME_HEBDO& problemeHebdo,
std::string_view name,
const Solver::HebdoProblemToLpsTranslator& translator,
const unsigned int year,
const unsigned int week)
{
auto weekly_data = translator.translate(problemeHebdo->ProblemeAResoudre.get(), name);
auto weekly_data = translator.translate(problemeHebdo.ProblemeAResoudre.get(), name);
Solver::ConstantDataFromAntares common_data;
if (year == 1 && week == 1)
{
common_data = translator.commonProblemData(problemeHebdo->ProblemeAResoudre.get());
common_data = translator.commonProblemData(problemeHebdo.ProblemeAResoudre.get());
}
return std::make_pair(common_data, weekly_data);
}
}
void SimulationObserver::notifyHebdoProblem(const PROBLEME_HEBDO* problemeHebdo,
} // namespace

void SimulationObserver::notifyHebdoProblem(const PROBLEME_HEBDO& problemeHebdo,
int optimizationNumber,
std::string_view name)
{
if (optimizationNumber != 1) return; //We only care about first optimization
if (optimizationNumber != 1)
{
return; // We only care about first optimization
}
Solver::HebdoProblemToLpsTranslator translator;
const unsigned int year = problemeHebdo->year + 1;
const unsigned int week = problemeHebdo->weekInTheYear + 1;
//common_data and weekly_data computed before the mutex lock to prevent blocking the thread
const unsigned int year = problemeHebdo.year + 1;
const unsigned int week = problemeHebdo.weekInTheYear + 1;
// common_data and weekly_data computed before the mutex lock to prevent blocking the thread
auto [common_data, weekly_data] = translate(problemeHebdo, name, translator, year, week);
std::lock_guard lock(mutex_);
if (year == 1 && week == 1) {
if (year == 1 && week == 1)
{
lps_.setConstantData(common_data);
}
lps_.addWeeklyData({year, week}, weekly_data);
Expand All @@ -64,4 +70,4 @@ Solver::LpsFromAntares&& SimulationObserver::acquireLps() noexcept
std::lock_guard lock(mutex_);
return std::move(lps_);
}
} // namespace Api
} // namespace Antares::API
2 changes: 1 addition & 1 deletion src/api/private/SimulationObserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SimulationObserver: public Solver::Simulation::ISimulationObserver
* @param optimizationNumber The number of the optimization.
* @param name The name of the problem.
*/
void notifyHebdoProblem(const PROBLEME_HEBDO* problemeHebdo,
void notifyHebdoProblem(const PROBLEME_HEBDO& problemeHebdo,
int optimizationNumber,
std::string_view name) override;

Expand Down
2 changes: 1 addition & 1 deletion src/solver/optimisation/opt_optimisation_lineaire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void notifyProblemHebdo(const PROBLEME_HEBDO* problemeHebdo,
Solver::Simulation::ISimulationObserver& simulationObserver,
const OptPeriodStringGenerator* optPeriodStringGenerator)
{
simulationObserver.notifyHebdoProblem(problemeHebdo,
simulationObserver.notifyHebdoProblem(*problemeHebdo,
optimizationNumber,
createMPSfilename(*optPeriodStringGenerator,
optimizationNumber));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ISimulationObserver
* @param optimizationNumber The number of the optimization.
* @param name The name of the problem.
*/
virtual void notifyHebdoProblem(const PROBLEME_HEBDO* problemeHebdo,
virtual void notifyHebdoProblem(const PROBLEME_HEBDO& problemeHebdo,
int optimizationNumber,
std::string_view name)
= 0;
Expand All @@ -57,7 +57,7 @@ class NullSimulationObserver : public ISimulationObserver
{
public:
~NullSimulationObserver() override = default;
void notifyHebdoProblem(const PROBLEME_HEBDO*, int, std::string_view) override
void notifyHebdoProblem(const PROBLEME_HEBDO&, int, std::string_view) override
{
//null object pattern
}
Expand Down

0 comments on commit 2cef96a

Please sign in to comment.