Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use std::unique_ptr with std::ifstream.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 23 Nov 2020 15:15:20 +0000 (16:15 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 23 Nov 2020 15:18:54 +0000 (16:18 +0100)
src/kernel/resource/profile/Profile.cpp
src/plugins/file_system/s4u_FileSystem.cpp

index 9a85416..03b6da0 100644 (file)
@@ -13,6 +13,7 @@
 
 #include <boost/algorithm/string.hpp>
 #include <fstream>
+#include <memory>
 #include <ostream>
 #include <sstream>
 #include <unordered_map>
@@ -211,12 +212,11 @@ Profile* Profile::from_file(const std::string& path)
   xbt_assert(not path.empty(), "Cannot parse a trace from an empty filename");
   xbt_assert(trace_list.find(path) == trace_list.end(), "Refusing to define trace %s twice", path.c_str());
 
-  const std::ifstream* f = surf_ifsopen(path);
+  auto f = std::unique_ptr<std::ifstream>(surf_ifsopen(path));
   xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", path.c_str(), (boost::join(surf_path, ":")).c_str());
 
   std::stringstream buffer;
   buffer << f->rdbuf();
-  delete f;
 
   return Profile::from_string(path, buffer.str(), -1);
 }
index 25c6caf..7308985 100644 (file)
@@ -511,7 +511,7 @@ std::map<std::string, sg_size_t>* FileSystemDiskExt::parse_content(const std::st
 
   auto* parse_content = new std::map<std::string, sg_size_t>();
 
-  std::ifstream* fs = surf_ifsopen(filename);
+  auto fs = std::unique_ptr<std::ifstream>(surf_ifsopen(filename));
   xbt_assert(not fs->fail(), "Cannot open file '%s' (path=%s)", filename.c_str(),
              (boost::join(surf_path, ":")).c_str());
 
@@ -529,7 +529,6 @@ std::map<std::string, sg_size_t>* FileSystemDiskExt::parse_content(const std::st
       parse_content->insert({tokens.front(), size});
     }
   } while (not fs->eof());
-  delete fs;
   return parse_content;
 }
 
@@ -540,7 +539,7 @@ std::map<std::string, sg_size_t>* FileSystemStorageExt::parse_content(const std:
 
   auto* parse_content = new std::map<std::string, sg_size_t>();
 
-  std::ifstream* fs = surf_ifsopen(filename);
+  auto fs = std::unique_ptr<std::ifstream>(surf_ifsopen(filename));
   xbt_assert(not fs->fail(), "Cannot open file '%s' (path=%s)", filename.c_str(),
              (boost::join(surf_path, ":")).c_str());
 
@@ -558,7 +557,6 @@ std::map<std::string, sg_size_t>* FileSystemStorageExt::parse_content(const std:
       parse_content->insert({tokens.front(), size});
     }
   } while (not fs->eof());
-  delete fs;
   return parse_content;
 }