From 534ac2cea6eecf6f151c59a94f464b702dd2cbc3 Mon Sep 17 00:00:00 2001
From: Giovanni Bussi <giovanni.bussi@gmail.com>
Date: Tue, 19 Sep 2017 08:21:29 +0200
Subject: [PATCH] unique_ptr: DataFetchingObject

---
 src/core/DataFetchingObject.cpp | 6 +++---
 src/core/DataFetchingObject.h   | 3 ++-
 src/core/PlumedMain.cpp         | 4 ++--
 3 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/src/core/DataFetchingObject.cpp b/src/core/DataFetchingObject.cpp
index 8cb51f349..bb07a3951 100644
--- a/src/core/DataFetchingObject.cpp
+++ b/src/core/DataFetchingObject.cpp
@@ -40,11 +40,11 @@ public:
   void finishDataGrab();
 };
 
-DataFetchingObject* DataFetchingObject::create(unsigned n, PlumedMain& p) {
+std::unique_ptr<DataFetchingObject> DataFetchingObject::create(unsigned n, PlumedMain& p) {
   if(n==sizeof(double)) {
-    return new DataFetchingObjectTyped<double>(p);
+    return std::unique_ptr<DataFetchingObjectTyped<double>>(new DataFetchingObjectTyped<double>(p));
   } else  if(n==sizeof(float)) {
-    return new DataFetchingObjectTyped<float>(p);
+    return std::unique_ptr<DataFetchingObjectTyped<float>>(new DataFetchingObjectTyped<float>(p));
   }
   std::string pp; Tools::convert(n,pp);
   plumed_merror("cannot create an MD interface with sizeof(real)=="+ pp);
diff --git a/src/core/DataFetchingObject.h b/src/core/DataFetchingObject.h
index ab38eab93..e2bdd5dc5 100644
--- a/src/core/DataFetchingObject.h
+++ b/src/core/DataFetchingObject.h
@@ -26,6 +26,7 @@
 #include <vector>
 #include <set>
 #include <map>
+#include <memory>
 
 namespace PLMD {
 
@@ -43,7 +44,7 @@ protected:
 /// A copy of the plumed main object
   PlumedMain & plumed;
 public:
-  static DataFetchingObject* create(unsigned n, PlumedMain& p);
+  static std::unique_ptr<DataFetchingObject> create(unsigned n, PlumedMain& p);
 /// A constructor so that we can create the plumed main object
   explicit DataFetchingObject(PlumedMain&p);
   virtual ~DataFetchingObject() {}
diff --git a/src/core/PlumedMain.cpp b/src/core/PlumedMain.cpp
index f23f87d11..afb83393b 100644
--- a/src/core/PlumedMain.cpp
+++ b/src/core/PlumedMain.cpp
@@ -72,6 +72,7 @@ PlumedMain::PlumedMain():
   citations_fwd(new Citations),
   step(0),
   active(false),
+  mydatafetcher(DataFetchingObject::create(sizeof(double),*this)),
   endPlumed(false),
   atoms_fwd(new Atoms(*this)),
   actionSet_fwd(new ActionSet(*this)),
@@ -88,7 +89,6 @@ PlumedMain::PlumedMain():
 {
   log.link(comm);
   log.setLinePrefix("PLUMED: ");
-  mydatafetcher.reset(DataFetchingObject::create(sizeof(double),*this));
   stopwatch.start();
   stopwatch.pause();
 }
@@ -291,7 +291,7 @@ void PlumedMain::cmd(const std::string & word,void*val) {
       CHECK_NOTINIT(initialized,word);
       CHECK_NOTNULL(val,word);
       atoms.setRealPrecision(*static_cast<int*>(val));
-      mydatafetcher.reset(DataFetchingObject::create(*static_cast<int*>(val),*this));
+      mydatafetcher=DataFetchingObject::create(*static_cast<int*>(val),*this);
       break;
     case cmd_setMDLengthUnits:
       CHECK_NOTINIT(initialized,word);
-- 
GitLab