class Worker {
public:
Worker() {};
- Worker(simgrid::xbt::args args) {}
+ Worker(std::vector<std::string> args) {}
void operator()() {
XBT_INFO("Hello s4u, I'm ready to serve");
char *msg = static_cast<char*>(simgrid::s4u::this_actor::recv(
class Master {
public:
Master() {};
- Master(simgrid::xbt::args args) {}
+ Master(std::vector<std::string> args) {}
void operator()() {
const char *msg = "GaBuZoMeu";
XBT_INFO("Hello s4u, I have something to send");
/* 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 <string>
+#include <vector>
+
#include <xbt/sysdep.h>
#include <simgrid/s4u.h>
e->loadPlatform("../../platforms/two_hosts.xml");
e->registerFunction<Worker>("worker");
e->registerFunction<Master>("master");
- simgrid::xbt::args args;
+ std::vector<std::string> 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();
// 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<std::string> 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<std::string> 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
template<class F>
void registerFunction(const char* name)
{
- simgrid::simix::registerFunction(name, [](simgrid::xbt::args args){
+ simgrid::simix::registerFunction(name, [](std::vector<std::string> args){
return simgrid::simix::ActorCode([args] {
F code(std::move(args));
code();
template<class F>
void registerFunction(const char* name, F code)
{
- simgrid::simix::registerFunction(name, [code](simgrid::xbt::args args){
+ simgrid::simix::registerFunction(name, [code](std::vector<std::string> args){
return simgrid::simix::ActorCode([code,args] {
code(std::move(args));
});
typedef std::function<void()> ActorCode;
// Create ActorCode based on argv:
-typedef std::function<ActorCode(simgrid::xbt::args args)> ActorCodeFactory;
+typedef std::function<ActorCode(std::vector<std::string> args)> ActorCodeFactory;
XBT_PUBLIC(void) registerFunction(const char* name, ActorCodeFactory factory);
#include <exception>
#include <functional>
-#include <utility>
+#include <memory>
+#include <string>
#include <tuple>
+#include <utility>
+#include <vector>
#include <xbt/sysdep.h>
#include <xbt/utility.hpp>
namespace simgrid {
namespace xbt {
-class args {
+template<class F>
+class MainFunction {
private:
- int argc_ = 0;
- char** argv_ = nullptr;
+ F code_;
+ std::shared_ptr<const std::vector<std::string>> 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<std::string> args) :
+ code_(std::move(code)),
+ args_(std::make_shared<const std::vector<std::string>>(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<std::string> args = *args_;
+ std::unique_ptr<char*[]> argv(new char*[argc + 1]);
+ for (int i = 0; i != argc; ++i)
+ argv[i] = args[i].empty() ? const_cast<char*>(""): &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<class F> inline
-std::function<void()> wrapMain(F code, std::shared_ptr<simgrid::xbt::args> args)
-{
- return [=]() {
- code(args->argc(), args->argv());
- };
-}
-
-template<class F> inline
-std::function<void()> wrapMain(F code, simgrid::xbt::args args)
+std::function<void()> wrapMain(F code, std::vector<std::string> args)
{
- return wrapMain(std::move(code),
- std::unique_ptr<simgrid::xbt::args>(new simgrid::xbt::args(std::move(args))));
+ return MainFunction<F>(std::move(code), std::move(args));
}
template<class F> inline
-std::function<void()> wrapMain(F code, int argc, const char*const* argv)
+std::function<void()> wrapMain(F code, int argc, const char*const argv[])
{
- return wrapMain(std::move(code), args(argc, argv));
+ std::vector<std::string> args(argv, argv + argc);
+ return MainFunction<F>(std::move(code), std::move(args));
}
namespace bits {
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<void()> function;
+ if (code)
+ function = simgrid::xbt::wrapMain(code, argc, const_cast<const char*const*>(argv));
msg_process_t res = MSG_process_create_with_environment(name,
- code ? simgrid::xbt::wrapMain(code, argc, argv) : std::function<void()>(),
- data, host,
- properties);
+ std::move(function), data, host, properties);
for (int i = 0; i != argc; ++i)
xbt_free(argv[i]);
xbt_free(argv);
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<std::string> args)
{
simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
simgrid::simix::ActorCode code = factory(std::move(args));
-/* 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 <string>
+#include <vector>
+
#include "smx_private.h"
#include "xbt/sysdep.h"
#include "xbt/log.h"
// 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<std::string> args) {
return simgrid::xbt::wrapMain(code, std::move(args));
};
}
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;
double kill_time = process->kill_time;
int auto_restart = process->on_failure == SURF_PROCESS_ON_FAILURE_DIE ? 0 : 1;
- std::function<void()> code = factory(simgrid::xbt::args(process->argc, process->argv));
+ std::vector<std::string> args(process->argv, process->argv + process->argc);
+ std::function<void()> code = factory(std::move(args));
smx_process_arg_t arg = nullptr;
smx_process_t process_created = nullptr;