Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / examples / s4u / replay-io / s4u-replay-io.cpp
index 509995c..52f18b8 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2017-2020. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2017-2021. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -12,8 +12,6 @@
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(replay_io, "Messages specific for this example");
 
-static std::unordered_map<std::string, simgrid::s4u::File*> opened_files;
-
 #define ACT_DEBUG(...)                                                                                                 \
   if (XBT_LOG_ISENABLED(replay_io, xbt_log_priority_verbose)) {                                                        \
     std::string NAME = boost::algorithm::join(action, " ");                                                            \
@@ -21,22 +19,23 @@ static std::unordered_map<std::string, simgrid::s4u::File*> opened_files;
   } else                                                                                                               \
     ((void)0)
 
-static void log_action(const simgrid::xbt::ReplayAction& action, double date)
-{
-  if (XBT_LOG_ISENABLED(replay_io, xbt_log_priority_verbose)) {
-    std::string s = boost::algorithm::join(action, " ");
-    XBT_VERB("%s %f", s.c_str(), date);
-  }
-}
+class Replayer {
+  static std::unordered_map<std::string, simgrid::s4u::File> opened_files;
 
-static simgrid::s4u::File* get_file_descriptor(const std::string& file_name)
-{
-  std::string full_name = simgrid::s4u::this_actor::get_name() + ":" + file_name;
+  static void log_action(const simgrid::xbt::ReplayAction& action, double date)
+  {
+    if (XBT_LOG_ISENABLED(replay_io, xbt_log_priority_verbose)) {
+      std::string s = boost::algorithm::join(action, " ");
+      XBT_VERB("%s %f", s.c_str(), date);
+    }
+  }
 
-  return opened_files.at(full_name);
-}
+  static simgrid::s4u::File* get_file_descriptor(const std::string& file_name)
+  {
+    std::string full_name = simgrid::s4u::this_actor::get_name() + ":" + file_name;
+    return &opened_files.at(full_name);
+  }
 
-class Replayer {
 public:
   explicit Replayer(std::vector<std::string> args)
   {
@@ -57,9 +56,8 @@ public:
     std::string full_name = simgrid::s4u::this_actor::get_name() + ":" + file_name;
 
     ACT_DEBUG("Entering Open: %s (filename: %s)", NAME.c_str(), file_name.c_str());
-    simgrid::s4u::File* file = new simgrid::s4u::File(file_name, NULL);
-
-    opened_files.insert({full_name, file});
+    opened_files.emplace(std::piecewise_construct, std::forward_as_tuple(full_name),
+                         std::forward_as_tuple(file_name, nullptr));
 
     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
   }
@@ -81,17 +79,19 @@ public:
   static void close(simgrid::xbt::ReplayAction& action)
   {
     std::string file_name = action[2];
+    std::string full_name = simgrid::s4u::this_actor::get_name() + ":" + file_name;
     double clock          = simgrid::s4u::Engine::get_clock();
 
-    const simgrid::s4u::File* file = get_file_descriptor(file_name);
-
     ACT_DEBUG("Entering Close: %s (filename: %s)", NAME.c_str(), file_name.c_str());
-    delete file;
+    XBT_ATTRIB_UNUSED auto count = opened_files.erase(full_name);
+    xbt_assert(count == 1, "File not found in opened files: %s", full_name.c_str());
 
     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
   }
 };
 
+std::unordered_map<std::string, simgrid::s4u::File> Replayer::opened_files;
+
 int main(int argc, char* argv[])
 {
   simgrid::s4u::Engine e(&argc, argv);
@@ -113,16 +113,15 @@ int main(int argc, char* argv[])
   xbt_replay_action_register("read", Replayer::read);
   xbt_replay_action_register("close", Replayer::close);
 
+  std::ifstream ifs;
   if (argv[3]) {
-    simgrid::xbt::action_fs = new std::ifstream(argv[3], std::ifstream::in);
+    ifs.open(argv[3], std::ifstream::in);
+    simgrid::xbt::action_fs = &ifs;
   }
 
   e.run();
 
-  if (argv[3]) {
-    delete simgrid::xbt::action_fs;
-    simgrid::xbt::action_fs = nullptr;
-  }
+  simgrid::xbt::action_fs = nullptr;
 
   XBT_INFO("Simulation time %g", e.get_clock());