/** 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<std::string>)
+ /** 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<std::string>));
/** Registers a function as the default main function of actors
*
#include <functional>
#include <string>
#include <unordered_map>
+#include <vector>
#endif
/* ******************************** Host ************************************ */
SG_END_DECL()
#ifdef __cplusplus
+XBT_PUBLIC void SIMIX_function_register(std::string name, void (*code)(std::vector<std::string>));
XBT_PUBLIC void SIMIX_function_register(std::string name, xbt_main_func_t code);
XBT_PUBLIC void SIMIX_launch_application(std::string file);
#endif
// What's executed as SIMIX actor code:
typedef std::function<void()> ActorCode;
-// Create ActorCode based on argv:
+// Create an ActorCode based on a std::string
typedef std::function<ActorCode(std::vector<std::string> args)> ActorCodeFactory;
XBT_PUBLIC void register_function(std::string name, ActorCodeFactory factory);
namespace simgrid {
namespace xbt {
-template<class F>
-class MainFunction {
+template <class F> class MainFunction {
private:
F code_;
std::shared_ptr<const std::vector<std::string>> args_;
+
public:
- MainFunction(F code, std::vector<std::string> args) :
- code_(std::move(code)),
- args_(std::make_shared<const std::vector<std::string>>(std::move(args)))
- {}
+ MainFunction(F code, std::vector<std::string> args)
+ : code_(std::move(code)), args_(std::make_shared<const std::vector<std::string>>(std::move(args)))
+ {
+ }
void operator()() const
{
- const int argc = args_->size();
+ const int argc = args_->size();
std::vector<std::string> args = *args_;
if (not args.empty()) {
char noarg[] = {'\0'};
std::unique_ptr<char* []> 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::string>);
+ std::shared_ptr<const std::vector<std::string>> args_;
+
+public:
+ MainStdFunction(void (*code)(std::vector<std::string>), std::vector<std::string> args)
+ : code_(std::move(code)), args_(std::make_shared<const std::vector<std::string>>(std::move(args)))
+ {
+ }
+ void operator()() const
+ {
+ std::vector<std::string> args = *args_;
+ code_(args);
+ }
+};
template <class F>
inline XBT_ATTRIB_DEPRECATED_v323("Please use wrap_main()") std::function<void()> wrapMain(
{
return MainFunction<F>(std::move(code), std::move(args));
}
+inline std::function<void()> wrap_main(void (*code)(std::vector<std::string>), std::vector<std::string> args)
+{
+ return MainStdFunction(std::move(code), std::move(args));
+}
template <class F>
inline XBT_ATTRIB_DEPRECATED_v323("Please use wrap_main()") std::function<void()> wrapMain(F code, int argc,
{
SIMIX_function_register(name, code);
}
+void Engine::register_function(std::string name, void (*code)(std::vector<std::string>))
+{
+ SIMIX_function_register(name, code);
+}
void Engine::register_default(int (*code)(int, char**))
{
SIMIX_function_register_default(code);
{
return [code](std::vector<std::string> args) { return simgrid::xbt::wrap_main(code, std::move(args)); };
}
+static simgrid::simix::ActorCodeFactory toActorCodeFactory(void (*code)(std::vector<std::string>))
+{
+ return [code](std::vector<std::string> args) { return simgrid::xbt::wrap_main(code, std::move(args)); };
+}
/**
* @brief Registers a #xbt_main_func_t code in a global table.
*/
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<std::string>))
+{
simix_global->registered_functions[name] = toActorCodeFactory(code);
}