From 4af42d3c081d374f5c2d7a526eb64c23291f5280 Mon Sep 17 00:00:00 2001 From: mquinson Date: Sat, 11 Apr 2009 16:40:44 +0000 Subject: [PATCH] allow to define a default value to registered functions. This allows to use generic functions deciding what to do from the name used, and thus deal with unknown function names in deployment files git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@6231 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- include/msg/msg.h | 1 + src/include/simix/simix.h | 1 + src/msg/deployment.c | 11 +++++++++++ src/simix/smx_deployment.c | 18 +++++++++++++++++- 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/include/msg/msg.h b/include/msg/msg.h index 28bbc28776..05142be44f 100644 --- a/include/msg/msg.h +++ b/include/msg/msg.h @@ -23,6 +23,7 @@ XBT_PUBLIC(int) MSG_get_channel_number(void); XBT_PUBLIC(MSG_error_t) MSG_main(void); XBT_PUBLIC(MSG_error_t) MSG_clean(void); XBT_PUBLIC(void) MSG_function_register(const char *name, xbt_main_func_t code); +XBT_PUBLIC(void) MSG_function_register_default(xbt_main_func_t code); XBT_PUBLIC(xbt_main_func_t) MSG_get_registered_function(const char *name); XBT_PUBLIC(void) MSG_launch_application(const char *file); XBT_PUBLIC(void) MSG_paje_output(const char *filename); diff --git a/src/include/simix/simix.h b/src/include/simix/simix.h index 6f147c0820..9c0a13a94c 100644 --- a/src/include/simix/simix.h +++ b/src/include/simix/simix.h @@ -24,6 +24,7 @@ XBT_PUBLIC(void) SIMIX_config(const char *name, va_list pa); XBT_PUBLIC(void) SIMIX_global_init(int *argc, char **argv); XBT_PUBLIC(void) SIMIX_clean(void); XBT_PUBLIC(void) SIMIX_function_register(const char *name, xbt_main_func_t code); +XBT_PUBLIC(void) SIMIX_function_register_default(xbt_main_func_t code); XBT_PUBLIC(xbt_main_func_t) SIMIX_get_registered_function(const char *name); XBT_PUBLIC(void) SIMIX_launch_application(const char *file); diff --git a/src/msg/deployment.c b/src/msg/deployment.c index 35fc38e46a..7c310f4a9f 100644 --- a/src/msg/deployment.c +++ b/src/msg/deployment.c @@ -52,6 +52,17 @@ void MSG_function_register(const char *name, xbt_main_func_t code) return; } +/** \ingroup msg_easier_life + * \brief Registers a function as the default main function of agents. + * + * Registers a code function as being the default value. This function will get used by MSG_launch_application() when there is no registered function of the requested name in. + * \param code the function (must have the same prototype than the main function of any C program: int ..(int argc, char *argv[])) + */ +void MSG_function_register_default(xbt_main_func_t code) +{ + SIMIX_function_register_default(code); +} + /** \ingroup msg_easier_life * \brief Retrieves a registered main function * diff --git a/src/simix/smx_deployment.c b/src/simix/smx_deployment.c index 2ca297f52f..a19e2b4d4c 100644 --- a/src/simix/smx_deployment.c +++ b/src/simix/smx_deployment.c @@ -149,6 +149,21 @@ void SIMIX_function_register(const char *name, xbt_main_func_t code) xbt_dict_set(simix_global->registered_functions, name, code, NULL); } +static xbt_main_func_t default_function = NULL; +/** + * \brief Registers a #smx_process_code_t code as default value. + * + * Registers a code function as being the default value. This function will get used by SIMIX_launch_application() when there is no registered function of the requested name in. + * \param code the function + */ +void SIMIX_function_register_default(xbt_main_func_t code) +{ + xbt_assert0(simix_global, + "SIMIX_global_init has to be called before SIMIX_function_register."); + + default_function = code; +} + /** * \brief Gets a #smx_process_t code from the global table. * @@ -162,5 +177,6 @@ xbt_main_func_t SIMIX_get_registered_function(const char *name) xbt_assert0(simix_global, "SIMIX_global_init has to be called before SIMIX_get_registered_function."); - return xbt_dict_get_or_null(simix_global->registered_functions, name); + xbt_main_func_t res = xbt_dict_get_or_null(simix_global->registered_functions, name); + return res ? res : default_function; } -- 2.20.1