From aeb81a7cdff3ef5efe223f5e1aa0305a71fed3d7 Mon Sep 17 00:00:00 2001 From: Giovanni Bussi <giovanni.bussi@gmail.com> Date: Thu, 12 Oct 2017 08:38:33 +0200 Subject: [PATCH] unique_ptr changed some interfaces to return a unique_ptr --- src/pamm/PammObject.cpp | 6 ++++-- src/tools/BiasRepresentation.cpp | 8 ++++---- src/tools/BiasRepresentation.h | 2 +- src/tools/KernelFunctions.cpp | 8 ++++---- src/tools/KernelFunctions.h | 3 ++- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/pamm/PammObject.cpp b/src/pamm/PammObject.cpp index 544306722..7606d7f1b 100644 --- a/src/pamm/PammObject.cpp +++ b/src/pamm/PammObject.cpp @@ -21,6 +21,7 @@ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ #include "PammObject.h" #include "tools/IFile.h" +#include <memory> namespace PLMD { namespace pamm { @@ -65,10 +66,11 @@ void PammObject::setup( const std::string& filename, const double& reg, const st ifile.open(filename); ifile.allowIgnoredFields(); kernels.resize(0); for(unsigned k=0;; ++k) { - KernelFunctions* kk = KernelFunctions::read( &ifile, false, valnames ); + std::unique_ptr<KernelFunctions> kk = KernelFunctions::read( &ifile, false, valnames ); if( !kk ) break ; kk->normalize( pos ); - kernels.push_back( kk ); + kernels.push_back( kk.release() ); // kernels should be changed into a vector<unique_ptr>. + // meanwhile, I just release the unique_ptr herelease the unique_ptr here. GB ifile.scanField(); } ifile.close(); diff --git a/src/tools/BiasRepresentation.cpp b/src/tools/BiasRepresentation.cpp index c81524de7..bafae408d 100644 --- a/src/tools/BiasRepresentation.cpp +++ b/src/tools/BiasRepresentation.cpp @@ -115,24 +115,24 @@ Value* BiasRepresentation::getPtrToValue(unsigned i) { return values[i]; } -KernelFunctions* BiasRepresentation::readFromPoint(IFile *ifile) { +std::unique_ptr<KernelFunctions> BiasRepresentation::readFromPoint(IFile *ifile) { vector<double> cc( names.size() ); for(unsigned i=0; i<names.size(); ++i) { ifile->scanField(names[i],cc[i]); } double h=1.0; - return new KernelFunctions(cc,histosigma,"gaussian","DIAGONAL",h); + return std::unique_ptr<KernelFunctions>( new KernelFunctions(cc,histosigma,"gaussian","DIAGONAL",h) ); } void BiasRepresentation::pushKernel( IFile *ifile ) { std::unique_ptr<KernelFunctions> kk; // here below the reading of the kernel is completely hidden if(histosigma.size()==0) { ifile->allowIgnoredFields(); - kk.reset(KernelFunctions::read(ifile,true,names)); + kk=KernelFunctions::read(ifile,true,names); } else { // when doing histogram assume gaussian with a given diagonal sigma // and neglect all the rest - kk.reset(readFromPoint(ifile)); + kk=readFromPoint(ifile); } // the bias factor is not something about the kernels but // must be stored to keep the bias/free energy duality diff --git a/src/tools/BiasRepresentation.h b/src/tools/BiasRepresentation.h index a775a0120..93c613a34 100644 --- a/src/tools/BiasRepresentation.h +++ b/src/tools/BiasRepresentation.h @@ -76,7 +76,7 @@ public: /// get the pointer to the grid Grid* getGridPtr(); /// get a new histogram point from a file - KernelFunctions* readFromPoint(IFile *ifile); + std::unique_ptr<KernelFunctions> readFromPoint(IFile *ifile); /// get an automatic min/max from the set so to know how to configure the grid void getMinMaxBin(std::vector<double> &vmin, std::vector<double> &vmax, std::vector<unsigned> &vbin); /// clear the representation (grid included) diff --git a/src/tools/KernelFunctions.cpp b/src/tools/KernelFunctions.cpp index f9f61cd37..deea4c209 100755 --- a/src/tools/KernelFunctions.cpp +++ b/src/tools/KernelFunctions.cpp @@ -382,7 +382,7 @@ double KernelFunctions::evaluate( const std::vector<Value*>& pos, std::vector<do return kval; } -KernelFunctions* KernelFunctions::read( IFile* ifile, const bool& cholesky, const std::vector<std::string>& valnames ) { +std::unique_ptr<KernelFunctions> KernelFunctions::read( IFile* ifile, const bool& cholesky, const std::vector<std::string>& valnames ) { double h; if( !ifile->scanField("height",h) ) return NULL;; @@ -401,7 +401,7 @@ KernelFunctions* KernelFunctions::read( IFile* ifile, const bool& cholesky, cons ifile->scanField("sigma_"+valnames[i],sig[i]); if( !cholesky ) sig[i]=sqrt(sig[i]); } - return new KernelFunctions( cc, sig, ktype, "DIAGONAL", h ); + return std::unique_ptr<KernelFunctions>(new KernelFunctions( cc, sig, ktype, "DIAGONAL", h ) ); } unsigned ncv=valnames.size(); @@ -419,8 +419,8 @@ KernelFunctions* KernelFunctions::read( IFile* ifile, const bool& cholesky, cons for(unsigned i=0; i<ncv; i++) { for(unsigned j=i; j<ncv; j++) { sig[k]=invmatrix(i,j); k++; } } - if( sss=="true" ) return new KernelFunctions( cc, sig, ktype, "MULTIVARIATE", h ); - return new KernelFunctions( cc, sig, ktype, "VON-MISSES", h ); + if( sss=="true" ) return std::unique_ptr<KernelFunctions>(new KernelFunctions( cc, sig, ktype, "MULTIVARIATE", h ) ); + return std::unique_ptr<KernelFunctions>(new KernelFunctions( cc, sig, ktype, "VON-MISSES", h ) ); } } diff --git a/src/tools/KernelFunctions.h b/src/tools/KernelFunctions.h index 32966973e..ac26c23d3 100644 --- a/src/tools/KernelFunctions.h +++ b/src/tools/KernelFunctions.h @@ -26,6 +26,7 @@ #include "Matrix.h" #include "core/Value.h" #include <vector> +#include <memory> namespace PLMD { @@ -64,7 +65,7 @@ public: /// Evaluate the kernel function with constant intervals double evaluate( const std::vector<Value*>& pos, std::vector<double>& derivatives, bool usederiv=true, bool doInt=false, double lowI_=-1, double uppI_=-1 ) const; /// Read a kernel function from a file - static KernelFunctions* read( IFile* ifile, const bool& cholesky, const std::vector<std::string>& valnames ); + static std::unique_ptr<KernelFunctions> read( IFile* ifile, const bool& cholesky, const std::vector<std::string>& valnames ); }; inline -- GitLab