SPRAAK
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Module initilisation and finalisation

When a module needs specific initialisation or finalisation, you can define a ini and (optionaly) a fini routine. These routines have no argument and no return value and are marked with the appropriate marker (see spr_mark_ini_fini).

If your module can only be initialized when other modules are initialized or can only be terminated while other modules are still open (not terminated) or if the initialization or termination of other modules depend on your module, then you need to work according to the example below:

#include <spraak/core/ini_fini.h>
spr_private_func
void spr_my_module_fini(void)
{// your termination code
// will be called in the reverse order of the initialization routines
}
spr_private_func_ini
void spr_my_module_ini(void)
{static SprIniDep dep[] = {SPR_INI_DEP("other_module1"),SPR_INI_DEP("other_module2"),SPR_INI_DEP(NULL)};
static SprIniMod module = SPR_INI_MOD("my_module",&spr_my_module_ini,&spr_my_module_fini,dep);
if(spr_ini_register(&module))
{// your initialization code
// other_module1 and other_module2 are guaranteed to be initialized before
if(initialization_failed)
spr_ini_fail = "my_module";
}
}

In the above example, "my_module" depends for its initialization (or termination) on two other modules: "other_module1" and "other_module2" (this may be an empty list). The SprIniDep and SprIniMod structures describe these dependencies using the appropriate macro's to initialize their elements. The spr_ini_register() function checks whether all dependecies have been resolved, and if so, the actual initialisation is done. In case there are still dependencies, the initialisation will be postponed by the spr_ini_register() function. When the dependencies are resolved, the same function will be called again, but now spr_ini_register() is guaranteed to return "True". If the initialization fails, an error code can be written back in the global variable spr_ini_fail.

Termination (closing down the system) uses the reverse order of the initialization. Routines marked as 'spr_private_func_fini' are handled last. Routine marked with 'spr_private_func_fini' should never be enlisted via the spr_ini_register().