From: Gabriel Corona Date: Mon, 27 Jun 2016 08:42:45 +0000 (+0200) Subject: Remove xbt::args: use vector instead X-Git-Tag: v3_14~855 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/1cce0801abcad7c884f9e72ceda87e36d6635104 Remove xbt::args: use vector instead --- diff --git a/examples/s4u/basic/s4u_basic.h b/examples/s4u/basic/s4u_basic.h index 7a3f9c6bed..f4e85e0d51 100644 --- a/examples/s4u/basic/s4u_basic.h +++ b/examples/s4u/basic/s4u_basic.h @@ -12,7 +12,7 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category"); class Worker { public: Worker() {}; - Worker(simgrid::xbt::args args) {} + Worker(std::vector args) {} void operator()() { XBT_INFO("Hello s4u, I'm ready to serve"); char *msg = static_cast(simgrid::s4u::this_actor::recv( @@ -25,7 +25,7 @@ public: class Master { public: Master() {}; - Master(simgrid::xbt::args args) {} + Master(std::vector args) {} void operator()() { const char *msg = "GaBuZoMeu"; XBT_INFO("Hello s4u, I have something to send"); diff --git a/examples/s4u/basic/s4u_basic_function.cpp b/examples/s4u/basic/s4u_basic_function.cpp index dd39a93778..2a21d5244d 100644 --- a/examples/s4u/basic/s4u_basic_function.cpp +++ b/examples/s4u/basic/s4u_basic_function.cpp @@ -3,6 +3,9 @@ /* 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. */ +#include +#include + #include #include @@ -14,7 +17,7 @@ int main(int argc, char **argv) { e->loadPlatform("../../platforms/two_hosts.xml"); e->registerFunction("worker"); e->registerFunction("master"); - simgrid::xbt::args args; + std::vector args; simgrid::s4u::Actor("worker", simgrid::s4u::Host::by_name("Tremblay"), "worker", args); simgrid::s4u::Actor("master", simgrid::s4u::Host::by_name("Jupiter"), "master", args); e->run(); diff --git a/include/simgrid/s4u/actor.hpp b/include/simgrid/s4u/actor.hpp index 88d8b01b17..4f3a1a9f5d 100644 --- a/include/simgrid/s4u/actor.hpp +++ b/include/simgrid/s4u/actor.hpp @@ -207,10 +207,12 @@ public: // Create actor from function name: - Actor(const char* name, s4u::Host *host, double killTime, const char* function, simgrid::xbt::args args); + Actor(const char* name, s4u::Host *host, double killTime, + const char* function, std::vector args); - Actor(const char* name, s4u::Host *host, const char* function, simgrid::xbt::args args) : - Actor(name, host, -1.0, function, std::move(args)) {} + Actor(const char* name, s4u::Host *host, const char* function, + std::vector args) + : Actor(name, host, -1.0, function, std::move(args)) {} /** Retrieves the actor that have the given PID (or NULL if not existing) */ //static Actor *byPid(int pid); not implemented diff --git a/include/simgrid/s4u/engine.hpp b/include/simgrid/s4u/engine.hpp index 07863e17e8..e7b184c175 100644 --- a/include/simgrid/s4u/engine.hpp +++ b/include/simgrid/s4u/engine.hpp @@ -64,7 +64,7 @@ public: template void registerFunction(const char* name) { - simgrid::simix::registerFunction(name, [](simgrid::xbt::args args){ + simgrid::simix::registerFunction(name, [](std::vector args){ return simgrid::simix::ActorCode([args] { F code(std::move(args)); code(); @@ -75,7 +75,7 @@ public: template void registerFunction(const char* name, F code) { - simgrid::simix::registerFunction(name, [code](simgrid::xbt::args args){ + simgrid::simix::registerFunction(name, [code](std::vector args){ return simgrid::simix::ActorCode([code,args] { code(std::move(args)); }); diff --git a/include/simgrid/simix.hpp b/include/simgrid/simix.hpp index a4824d0c37..d08f865bfe 100644 --- a/include/simgrid/simix.hpp +++ b/include/simgrid/simix.hpp @@ -175,7 +175,7 @@ XBT_PUBLIC(void) create_maestro(std::function code); typedef std::function ActorCode; // Create ActorCode based on argv: -typedef std::function ActorCodeFactory; +typedef std::function args)> ActorCodeFactory; XBT_PUBLIC(void) registerFunction(const char* name, ActorCodeFactory factory); diff --git a/include/xbt/functional.hpp b/include/xbt/functional.hpp index cf50e04fb2..f37b9dd0d7 100644 --- a/include/xbt/functional.hpp +++ b/include/xbt/functional.hpp @@ -12,8 +12,11 @@ #include #include -#include +#include +#include #include +#include +#include #include #include @@ -21,102 +24,39 @@ namespace simgrid { namespace xbt { -class args { +template +class MainFunction { private: - int argc_ = 0; - char** argv_ = nullptr; + F code_; + std::shared_ptr> args_; public: - - // Main constructors - args() {} - - void assign(int argc, const char*const* argv) - { - clear(); - char** new_argv = xbt_new(char*,argc + 1); - for (int i = 0; i < argc; i++) - new_argv[i] = xbt_strdup(argv[i]); - new_argv[argc] = nullptr; - this->argc_ = argc; - this->argv_ = new_argv; - } - args(int argc, const char*const* argv) - { - this->assign(argc, argv); - } - - char** to_argv() const + MainFunction(F code, std::vector args) : + code_(std::move(code)), + args_(std::make_shared>(std::move(args))) + {} + int operator()() const { - const int argc = argc_; - char** argv = xbt_new(char*, argc + 1); - for (int i=0; i< argc; i++) - argv[i] = xbt_strdup(argv_[i]); + const int argc = args_->size(); + std::vector args = *args_; + std::unique_ptr argv(new char*[argc + 1]); + for (int i = 0; i != argc; ++i) + argv[i] = args[i].empty() ? const_cast(""): &args[i].front(); argv[argc] = nullptr; - return argv; + return code_(argc, argv.get()); } - - // Free - void clear() - { - for (int i = 0; i < this->argc_; i++) - std::free(this->argv_[i]); - std::free(this->argv_); - this->argc_ = 0; - this->argv_ = nullptr; - } - ~args() { clear(); } - - // Copy - args(args const& that) - { - this->assign(that.argc(), that.argv()); - } - args& operator=(args const& that) - { - this->assign(that.argc(), that.argv()); - return *this; - } - - // Move: - args(args&& that) : argc_(that.argc_), argv_(that.argv_) - { - that.argc_ = 0; - that.argv_ = nullptr; - } - args& operator=(args&& that) - { - this->argc_ = that.argc_; - this->argv_ = that.argv_; - that.argc_ = 0; - that.argv_ = nullptr; - return *this; - } - - int argc() const { return argc_; } - char** argv() { return argv_; } - const char*const* argv() const { return argv_; } - char* operator[](std::size_t i) { return argv_[i]; } }; template inline -std::function wrapMain(F code, std::shared_ptr args) -{ - return [=]() { - code(args->argc(), args->argv()); - }; -} - -template inline -std::function wrapMain(F code, simgrid::xbt::args args) +std::function wrapMain(F code, std::vector args) { - return wrapMain(std::move(code), - std::unique_ptr(new simgrid::xbt::args(std::move(args)))); + return MainFunction(std::move(code), std::move(args)); } template inline -std::function wrapMain(F code, int argc, const char*const* argv) +std::function wrapMain(F code, int argc, const char*const argv[]) { - return wrapMain(std::move(code), args(argc, argv)); + std::vector args(argv, argv + argc); + return MainFunction(std::move(code), std::move(args)); } namespace bits { diff --git a/src/msg/msg_process.cpp b/src/msg/msg_process.cpp index 28c596effa..77ccfefb72 100644 --- a/src/msg/msg_process.cpp +++ b/src/msg/msg_process.cpp @@ -132,10 +132,11 @@ msg_process_t MSG_process_create_with_arguments(const char *name, xbt_main_func_ msg_process_t MSG_process_create_with_environment(const char *name, xbt_main_func_t code, void *data, msg_host_t host, int argc, char **argv, xbt_dict_t properties) { + std::function function; + if (code) + function = simgrid::xbt::wrapMain(code, argc, const_cast(argv)); msg_process_t res = MSG_process_create_with_environment(name, - code ? simgrid::xbt::wrapMain(code, argc, argv) : std::function(), - data, host, - properties); + std::move(function), data, host, properties); for (int i = 0; i != argc; ++i) xbt_free(argv[i]); xbt_free(argv); diff --git a/src/s4u/s4u_actor.cpp b/src/s4u/s4u_actor.cpp index 1a97502d0e..0c8b49ad6d 100644 --- a/src/s4u/s4u_actor.cpp +++ b/src/s4u/s4u_actor.cpp @@ -27,7 +27,8 @@ s4u::Actor::Actor(const char* name, s4u::Host *host, double killTime, std::funct killTime, nullptr, 0)); } -s4u::Actor::Actor(const char* name, s4u::Host *host, double killTime, const char* function, simgrid::xbt::args args) +s4u::Actor::Actor(const char* name, s4u::Host *host, double killTime, + const char* function, std::vector args) { simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function); simgrid::simix::ActorCode code = factory(std::move(args)); diff --git a/src/simix/smx_deployment.cpp b/src/simix/smx_deployment.cpp index a740981cf3..db1becc063 100644 --- a/src/simix/smx_deployment.cpp +++ b/src/simix/smx_deployment.cpp @@ -1,9 +1,12 @@ -/* Copyright (c) 2007, 2009-2015. The SimGrid Team. +/* Copyright (c) 2007, 2009-2016. 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. */ +#include +#include + #include "smx_private.h" #include "xbt/sysdep.h" #include "xbt/log.h" @@ -59,7 +62,7 @@ void SIMIX_launch_application(const char *file) // Wrap a main() function into a ActorCodeFactory: static simgrid::simix::ActorCodeFactory toActorCodeFactory(xbt_main_func_t code) { - return [code](simgrid::xbt::args args) { + return [code](std::vector args) { return simgrid::xbt::wrapMain(code, std::move(args)); }; } diff --git a/src/smpi/smpi_global.cpp b/src/smpi/smpi_global.cpp index 3501498fe7..d140c1aeeb 100644 --- a/src/smpi/smpi_global.cpp +++ b/src/smpi/smpi_global.cpp @@ -116,8 +116,6 @@ void smpi_process_init(int *argc, char ***argv) simdata->data = data; if (*argc > 3) { - free((*argv)[0]); - free((*argv)[1]); memmove(&(*argv)[0], &(*argv)[2], sizeof(char *) * (*argc - 2)); (*argv)[(*argc) - 1] = nullptr; (*argv)[(*argc) - 2] = nullptr; diff --git a/src/surf/sg_platf.cpp b/src/surf/sg_platf.cpp index 7b85965e5d..56a02ef0e2 100644 --- a/src/surf/sg_platf.cpp +++ b/src/surf/sg_platf.cpp @@ -577,7 +577,8 @@ void sg_platf_new_process(sg_platf_process_cbarg_t process) double kill_time = process->kill_time; int auto_restart = process->on_failure == SURF_PROCESS_ON_FAILURE_DIE ? 0 : 1; - std::function code = factory(simgrid::xbt::args(process->argc, process->argv)); + std::vector args(process->argv, process->argv + process->argc); + std::function code = factory(std::move(args)); smx_process_arg_t arg = nullptr; smx_process_t process_created = nullptr;