From 05e3eea7de69c8e49dcfa3a14e2e18007687a2aa Mon Sep 17 00:00:00 2001
From: Giovanni Bussi <giovanni.bussi@gmail.com>
Date: Wed, 11 Oct 2017 22:52:36 +0200
Subject: [PATCH] unique_ptr: some in mapping

---
 src/mapping/Mapping.cpp   |  6 +-----
 src/mapping/Mapping.h     |  4 ++--
 src/mapping/PCAVars.cpp   | 11 +++--------
 src/mapping/PathTools.cpp |  6 +++---
 4 files changed, 9 insertions(+), 18 deletions(-)

diff --git a/src/mapping/Mapping.cpp b/src/mapping/Mapping.cpp
index c0006dd68..bcfa92497 100644
--- a/src/mapping/Mapping.cpp
+++ b/src/mapping/Mapping.cpp
@@ -56,7 +56,7 @@ Mapping::Mapping(const ActionOptions&ao):
   std::string mtype; parse("TYPE",mtype);
   bool skipchecks; parseFlag("DISABLE_CHECKS",skipchecks);
   // Setup the object that does the mapping
-  mymap = new PointWiseMapping( mtype, skipchecks );
+  mymap.reset( new PointWiseMapping( mtype, skipchecks ) );
 
   // Read the properties we require
   if( keywords.exists("PROPERTY") ) {
@@ -124,10 +124,6 @@ void Mapping::turnOnDerivatives() {
   needsDerivatives();
 }
 
-Mapping::~Mapping() {
-  delete mymap;
-}
-
 void Mapping::prepare() {
   if( mymap->mappingNeedsSetup() ) {
     // Get the arguments and atoms that are required
diff --git a/src/mapping/Mapping.h b/src/mapping/Mapping.h
index 1fea8be58..aa6143ca1 100644
--- a/src/mapping/Mapping.h
+++ b/src/mapping/Mapping.h
@@ -28,6 +28,7 @@
 #include "vesselbase/ActionWithVessel.h"
 #include "reference/PointWiseMapping.h"
 #include <vector>
+#include <memory>
 
 namespace PLMD {
 
@@ -46,7 +47,7 @@ private:
 //  The derivative wrt to the distance from the frame
   std::vector<double> dfframes;
 /// This holds all the reference information
-  PointWiseMapping* mymap;
+  std::unique_ptr<PointWiseMapping> mymap;
 /// The forces on each of the derivatives (used in apply)
   std::vector<double> forcesToApply;
 protected:
@@ -69,7 +70,6 @@ protected:
 public:
   static void registerKeywords( Keywords& keys );
   explicit Mapping(const ActionOptions&);
-  ~Mapping();
 /// Overload the virtual functions that appear in both ActionAtomistic and ActionWithArguments
   void turnOnDerivatives();
   void calculateNumericalDerivatives( ActionWithValue* a=NULL );
diff --git a/src/mapping/PCAVars.cpp b/src/mapping/PCAVars.cpp
index 02d7c4776..c85a55d4a 100644
--- a/src/mapping/PCAVars.cpp
+++ b/src/mapping/PCAVars.cpp
@@ -176,7 +176,7 @@ private:
   MultiValue myvals;
   ReferenceValuePack mypack;
 /// The position of the reference configuration (the one we align to)
-  ReferenceConfiguration* myref;
+  std::unique_ptr<ReferenceConfiguration> myref;
 /// The eigenvectors we are interested in
   std::vector<Direction> directions;
 /// Stuff for applying forces
@@ -184,7 +184,6 @@ private:
 public:
   static void registerKeywords( Keywords& keys );
   explicit PCAVars(const ActionOptions&);
-  ~PCAVars();
   unsigned getNumberOfDerivatives();
   void lockRequests();
   void unlockRequests();
@@ -239,8 +238,8 @@ PCAVars::PCAVars(const ActionOptions& ao):
     expandArgKeywordInPDB( mypdb );
     if(do_read) {
       if( nfram==0 ) {
-        myref = metricRegister().create<ReferenceConfiguration>( mtype, mypdb );
-        Direction* tdir = dynamic_cast<Direction*>( myref );
+        myref.reset( metricRegister().create<ReferenceConfiguration>( mtype, mypdb ) );
+        Direction* tdir = dynamic_cast<Direction*>( myref.get() );
         if( tdir ) error("first frame should be reference configuration - not direction of vector");
         if( !myref->pcaIsEnabledForThisReference() ) error("can't do PCA with reference type " + mtype );
         std::vector<std::string> remarks( mypdb.getRemark() ); std::string rtype;
@@ -311,10 +310,6 @@ PCAVars::PCAVars(const ActionOptions& ao):
   for(unsigned i=0; i<getNumberOfComponents(); ++i) getPntrToComponent(i)->resizeDerivatives(nder);
 }
 
-PCAVars::~PCAVars() {
-  delete myref;
-}
-
 unsigned PCAVars::getNumberOfDerivatives() {
   if( getNumberOfAtoms()>0 ) {
     return 3*getNumberOfAtoms() + 9 + getNumberOfArguments();
diff --git a/src/mapping/PathTools.cpp b/src/mapping/PathTools.cpp
index b10e3d8a8..c3bd8a2e4 100644
--- a/src/mapping/PathTools.cpp
+++ b/src/mapping/PathTools.cpp
@@ -207,14 +207,14 @@ int PathTools::main(FILE* in, FILE*out,Communicator& pc) {
   std::string istart; parse("--start",istart); FILE* fp2=fopen(istart.c_str(),"r"); PDB mystartpdb;
   if( istart.length()==0 ) error("input is missing use --istart + --iend or --path");
   if( !mystartpdb.readFromFilepointer(fp2,false,0.1) ) error("could not read fila " + istart);
-  ReferenceConfiguration* sframe=metricRegister().create<ReferenceConfiguration>( mtype, mystartpdb );
+  std::unique_ptr<ReferenceConfiguration> sframe( metricRegister().create<ReferenceConfiguration>( mtype, mystartpdb ) );
   fclose(fp2);
 
 // Read final frame
   std::string iend; parse("--end",iend); FILE* fp1=fopen(iend.c_str(),"r"); PDB myendpdb;
   if( iend.length()==0 ) error("input is missing using --istart + --iend or --path");
   if( !myendpdb.readFromFilepointer(fp1,false,0.1) ) error("could not read fila " + iend);
-  ReferenceConfiguration* eframe=metricRegister().create<ReferenceConfiguration>( mtype, myendpdb );
+  std::unique_ptr<ReferenceConfiguration> eframe( metricRegister().create<ReferenceConfiguration>( mtype, myendpdb ) );
   fclose(fp1);
 
 // Get atoms and arg requests
@@ -286,7 +286,7 @@ int PathTools::main(FILE* in, FILE*out,Communicator& pc) {
   for(unsigned i=0; i<final_path.size(); ++i) { final_path[i]->print( ofile, ofmt, 10. ); delete final_path[i]; }
 // Delete the args as we don't need them anymore
   for(unsigned i=0; i<args.size(); ++i) delete args[i];
-  ofile.close(); delete sframe; delete eframe; return 0;
+  ofile.close(); return 0;
 }
 
 } // End of namespace
-- 
GitLab