From: Martin Quinson Date: Sun, 5 Aug 2018 23:01:27 +0000 (+0200) Subject: Allow to register functions that are void (*code)(std::vector) X-Git-Tag: v3_21~293 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/7e2d0f730f1bdbc0b207f6ac26c0b5ff4f0765fa Allow to register functions that are void (*code)(std::vector) --- diff --git a/include/simgrid/s4u/Engine.hpp b/include/simgrid/s4u/Engine.hpp index 27445d9702..fe486f403a 100644 --- a/include/simgrid/s4u/Engine.hpp +++ b/include/simgrid/s4u/Engine.hpp @@ -53,7 +53,8 @@ public: /** Registers the main function of an actor that will be launched from the deployment file */ void register_function(std::string name, int (*code)(int, char**)); - // FIXME: provide a register_function(std::string, std::vector) + /** Registers the main function of an actor that will be launched from the deployment file */ + void register_function(std::string name, void (*code)(std::vector)); /** Registers a function as the default main function of actors * diff --git a/include/simgrid/simix.h b/include/simgrid/simix.h index 0331d6b813..7f20595b19 100644 --- a/include/simgrid/simix.h +++ b/include/simgrid/simix.h @@ -14,6 +14,7 @@ #include #include #include +#include #endif /* ******************************** Host ************************************ */ @@ -134,6 +135,7 @@ XBT_PUBLIC void SIMIX_process_set_function(const char* process_host, const char* SG_END_DECL() #ifdef __cplusplus +XBT_PUBLIC void SIMIX_function_register(std::string name, void (*code)(std::vector)); XBT_PUBLIC void SIMIX_function_register(std::string name, xbt_main_func_t code); XBT_PUBLIC void SIMIX_launch_application(std::string file); #endif diff --git a/include/simgrid/simix.hpp b/include/simgrid/simix.hpp index 0af396308b..9720dc87c4 100644 --- a/include/simgrid/simix.hpp +++ b/include/simgrid/simix.hpp @@ -70,7 +70,7 @@ XBT_PUBLIC const std::vector& process_get_runnable(); // What's executed as SIMIX actor code: typedef std::function ActorCode; -// Create ActorCode based on argv: +// Create an ActorCode based on a std::string typedef std::function args)> ActorCodeFactory; XBT_PUBLIC void register_function(std::string name, ActorCodeFactory factory); diff --git a/include/xbt/functional.hpp b/include/xbt/functional.hpp index 4b5f0afc10..a0c4f26500 100644 --- a/include/xbt/functional.hpp +++ b/include/xbt/functional.hpp @@ -26,31 +26,47 @@ namespace simgrid { namespace xbt { -template -class MainFunction { +template class MainFunction { private: F code_; std::shared_ptr> args_; + public: - MainFunction(F code, std::vector args) : - code_(std::move(code)), - args_(std::make_shared>(std::move(args))) - {} + MainFunction(F code, std::vector args) + : code_(std::move(code)), args_(std::make_shared>(std::move(args))) + { + } void operator()() const { - const int argc = args_->size(); + const int argc = args_->size(); std::vector args = *args_; if (not args.empty()) { char noarg[] = {'\0'}; std::unique_ptr argv(new char*[argc + 1]); for (int i = 0; i != argc; ++i) - argv[i] = args[i].empty() ? noarg : &args[i].front(); + argv[i] = args[i].empty() ? noarg : &args[i].front(); argv[argc] = nullptr; code_(argc, argv.get()); } else code_(argc, nullptr); } }; +class MainStdFunction { +private: + void (*code_)(std::vector); + std::shared_ptr> args_; + +public: + MainStdFunction(void (*code)(std::vector), std::vector args) + : code_(std::move(code)), args_(std::make_shared>(std::move(args))) + { + } + void operator()() const + { + std::vector args = *args_; + code_(args); + } +}; template inline XBT_ATTRIB_DEPRECATED_v323("Please use wrap_main()") std::function wrapMain( @@ -63,6 +79,10 @@ template inline std::function wrap_main(F code, std::vector(std::move(code), std::move(args)); } +inline std::function wrap_main(void (*code)(std::vector), std::vector args) +{ + return MainStdFunction(std::move(code), std::move(args)); +} template inline XBT_ATTRIB_DEPRECATED_v323("Please use wrap_main()") std::function wrapMain(F code, int argc, diff --git a/src/s4u/s4u_Engine.cpp b/src/s4u/s4u_Engine.cpp index 6f5e4d65c8..afdc16fc64 100644 --- a/src/s4u/s4u_Engine.cpp +++ b/src/s4u/s4u_Engine.cpp @@ -80,6 +80,10 @@ void Engine::register_function(std::string name, int (*code)(int, char**)) { SIMIX_function_register(name, code); } +void Engine::register_function(std::string name, void (*code)(std::vector)) +{ + SIMIX_function_register(name, code); +} void Engine::register_default(int (*code)(int, char**)) { SIMIX_function_register_default(code); diff --git a/src/simix/smx_deployment.cpp b/src/simix/smx_deployment.cpp index e947067742..79045d9f53 100644 --- a/src/simix/smx_deployment.cpp +++ b/src/simix/smx_deployment.cpp @@ -67,6 +67,10 @@ static simgrid::simix::ActorCodeFactory toActorCodeFactory(xbt_main_func_t code) { return [code](std::vector args) { return simgrid::xbt::wrap_main(code, std::move(args)); }; } +static simgrid::simix::ActorCodeFactory toActorCodeFactory(void (*code)(std::vector)) +{ + return [code](std::vector args) { return simgrid::xbt::wrap_main(code, std::move(args)); }; +} /** * @brief Registers a #xbt_main_func_t code in a global table. @@ -78,8 +82,10 @@ static simgrid::simix::ActorCodeFactory toActorCodeFactory(xbt_main_func_t code) */ void SIMIX_function_register(std::string name, xbt_main_func_t code) { - xbt_assert(simix_global, - "SIMIX_global_init has to be called before SIMIX_function_register."); + simix_global->registered_functions[name] = toActorCodeFactory(code); +} +void SIMIX_function_register(std::string name, void (*code)(std::vector)) +{ simix_global->registered_functions[name] = toActorCodeFactory(code); }