Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rewrite in a simpler manner.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 15 Oct 2020 12:45:42 +0000 (14:45 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 15 Oct 2020 13:07:40 +0000 (15:07 +0200)
include/xbt/functional.hpp
src/bindings/python/simgrid_python.cpp

index c09d278..cc053a5 100644 (file)
@@ -12,6 +12,7 @@
 #include <cstdlib>
 #include <cstring>
 
+#include <algorithm>
 #include <array>
 #include <exception>
 #include <functional>
@@ -38,15 +39,9 @@ public:
   {
     const int argc                = args_->size();
     std::vector<std::string> args = *args_;
-    if (not args.empty()) {
-      char noarg[] = {'\0'};
-      auto argv    = std::make_unique<char*[]>(argc + 1);
-      for (int i = 0; i != argc; ++i)
-        argv[i] = args[i].empty() ? noarg : &args[i].front();
-      argv[argc] = nullptr;
-      code_(argc, argv.get());
-    } else
-      code_(argc, nullptr);
+    std::vector<char*> argv(args.size() + 1); // argv[argc] is nullptr
+    std::transform(begin(args), end(args), begin(argv), [](std::string& s) { return &s.front(); });
+    code_(argc, argv.data());
   }
 };
 
index 75d9f75..ce10be9 100644 (file)
@@ -31,6 +31,7 @@
 #include <simgrid/s4u/Mailbox.hpp>
 #include <simgrid/version.h>
 
+#include <algorithm>
 #include <memory>
 #include <string>
 #include <vector>
@@ -151,14 +152,11 @@ PYBIND11_MODULE(simgrid, m)
   /* Class Engine */
   py::class_<Engine>(m, "Engine", "Simulation Engine")
       .def(py::init([](std::vector<std::string> args) {
-        static char noarg[] = {'\0'};
         auto argc           = static_cast<int>(args.size());
-        auto argv           = std::make_unique<char*[]>(argc + 1);
-        for (int i = 0; i != argc; ++i)
-          argv[i] = args[i].empty() ? noarg : &args[i].front();
-        argv[argc] = nullptr;
+        std::vector<char*> argv(args.size() + 1); // argv[argc] is nullptr
+        std::transform(begin(args), end(args), begin(argv), [](std::string& s) { return &s.front(); });
         // Currently this can be dangling, we should wrap this somehow.
-        return new simgrid::s4u::Engine(&argc, argv.get());
+        return new simgrid::s4u::Engine(&argc, argv.data());
       }))
       .def_static("get_clock", &Engine::get_clock,
                   "The simulation time, ie the amount of simulated seconds since the simulation start.")