X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/8cd17b69afcbf269eb99ab0c197d9343ab4c90e1..7e2d0f730f1bdbc0b207f6ac26c0b5ff4f0765fa:/include/xbt/functional.hpp 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,