From c4103f5fc7e70b839effb82ef3b0a29da23f762f Mon Sep 17 00:00:00 2001 From: Simon Homes Date: Tue, 30 Jul 2024 10:01:47 +0200 Subject: [PATCH] Use shared ptr instead of raw pointers --- src/ensemble/EnsembleBase.cpp | 17 ++++++----------- src/ensemble/EnsembleBase.h | 5 +++-- src/io/ASCIIReader.cpp | 3 ++- src/molecules/Comp2Param.h | 3 ++- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/ensemble/EnsembleBase.cpp b/src/ensemble/EnsembleBase.cpp index 5aada10e51..c4d52ab334 100644 --- a/src/ensemble/EnsembleBase.cpp +++ b/src/ensemble/EnsembleBase.cpp @@ -15,11 +15,6 @@ Ensemble::~Ensemble() { delete _domain; _domain = nullptr; - for(auto const &cid1 : _mixingrules) { - for(auto const &cid2 : cid1.second) { - delete(cid2.second); - } -} } void Ensemble::readXML(XMLfileUnits& xmlconfig) { @@ -54,13 +49,13 @@ void Ensemble::readXML(XMLfileUnits& xmlconfig) { for(mixingruletIter = query.begin(); mixingruletIter; mixingruletIter++) { xmlconfig.changecurrentnode(mixingruletIter); // MixingRuleBase* mixingrule = nullptr; - std::unique_ptr mixingrule; + std::shared_ptr mixingrule; std::string mixingruletype; xmlconfig.getNodeValue("@type", mixingruletype); Log::global_log->info() << "Mixing rule type: " << mixingruletype << std::endl; if ("LB" == mixingruletype) { - mixingrule = std::make_unique(); + mixingrule = std::make_shared(); } else { Log::global_log->error() << "Unknown mixing rule " << mixingruletype << std::endl; @@ -77,17 +72,17 @@ void Ensemble::readXML(XMLfileUnits& xmlconfig) { << numComponents << ")" << std::endl; Simulation::exit(1); } - _mixingrules[cid1][cid2] = mixingrule.get(); + _mixingrules[cid1][cid2] = mixingrule; } // Use xi=eta=1.0 as default if no rule was specified for (int cidi = 0; cidi < numComponents; ++cidi) { for (int cidj = cidi+1; cidj < numComponents; ++cidj) { // cidj is always larger than cidi if (_mixingrules[cidi].count(cidj) == 0) { // Only LorentzBerthelot is supported until now - std::unique_ptr mixingrule = std::make_unique(); + std::shared_ptr mixingrule = std::make_shared(); mixingrule->setCid1(cidi); mixingrule->setCid2(cidj); - _mixingrules[cidi][cidj] = mixingrule.get(); + _mixingrules[cidi][cidj] = mixingrule; Log::global_log->warning() << "Mixing coefficients for components " << mixingrule->getCid1()+1 << " + " << mixingrule->getCid2()+1 // +1 due to internal cid << " set to default (LB with xi=eta=1.0)" << std::endl; @@ -113,7 +108,7 @@ void Ensemble::setComponentLookUpIDs() { } } -void Ensemble::setMixingrule(MixingRuleBase* mixingrule) { +void Ensemble::setMixingrule(std::shared_ptr mixingrule) { const int cid1 = mixingrule->getCid1(); const int cid2 = mixingrule->getCid2(); // Check if cids are valid diff --git a/src/ensemble/EnsembleBase.h b/src/ensemble/EnsembleBase.h index 53aad90b0f..e3daf551d5 100644 --- a/src/ensemble/EnsembleBase.h +++ b/src/ensemble/EnsembleBase.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "DomainBase.h" #include "molecules/MoleculeForwardDeclaration.h" @@ -123,7 +124,7 @@ class Ensemble { auto & getMixingrules() { return _mixingrules; } // Set one mixing rule - void setMixingrule(MixingRuleBase* mixingrule); + void setMixingrule(std::shared_ptr mixingrule); protected: @@ -132,7 +133,7 @@ class Ensemble { std::map _componentnamesToIds; // The mixing rules (xi,eta) can be accessed by _mixingrules[cid1][cid2] // Note that cid1 < cid2 and that cid is in internal format, i.e. starts with 0 - std::map> _mixingrules; + std::map>> _mixingrules; DomainBase* _domain; Type _type = undefined; diff --git a/src/io/ASCIIReader.cpp b/src/io/ASCIIReader.cpp index daa32f8e2a..81752bfc52 100644 --- a/src/io/ASCIIReader.cpp +++ b/src/io/ASCIIReader.cpp @@ -6,6 +6,7 @@ #include #include +#include #include "Domain.h" #include "ensemble/BoxDomain.h" @@ -219,7 +220,7 @@ void ASCIIReader::readPhaseSpaceHeader(Domain* domain, double timestep) { << " : xi=" << xi << " eta=" << eta << std::endl; #endif // Only LB mixing rule is supported for now - LorentzBerthelotMixingRule* mixingrule = new LorentzBerthelotMixingRule(); + std::shared_ptr mixingrule = std::make_shared(); mixingrule->setCid1(cidi); mixingrule->setCid2(cidj); mixingrule->setEta(eta); diff --git a/src/molecules/Comp2Param.h b/src/molecules/Comp2Param.h index 6ae2e8db4b..43ed9857f9 100644 --- a/src/molecules/Comp2Param.h +++ b/src/molecules/Comp2Param.h @@ -2,6 +2,7 @@ #define COMP2PARAM_H_ #include +#include #include "molecules/Component.h" #include "molecules/Array2D.h" @@ -15,7 +16,7 @@ */ class Comp2Param { - using MixRulesType = std::map>; + using MixRulesType = std::map>>; public: /** Create a new empty parameter stream. */