#include <BESGlobalIQ.h>
Static Public Member Functions | |
static bool | BESGlobalInit (int argc, char **argv) |
initialize global objects in an orderly fasion. | |
static bool | BESGlobalQuit (void) |
Runs the termination functions in reverse order of initialization, providing the application to clean up its global objects. |
C++ does not provide a good method for the initialization and termination of global objects. The main problem is ordering. This mechanism provides for the order of initialization and an easy to use means of initializing and terminating global objects. Only those global objects that are used within the application are linked into the application, just as in normal global objects. Termination of global objects using this mechanism occurs in the reverse order that they were initialized.
SETTING UP FOR GLOBAL INITIALIZATION
To set up a global object to be initialized you first need to determine in what order the global object should be initialized. The header file BESInitOrder lists the current order of initialization for BES objects.
Levels of initialization can have multiple objects being initialized. Initialization occurs in the order of the level. Within the same level initialization is random.
INTEGRATING THE INITIALIZATION CODE
Once you have the correct order for your object, it is time to set up the code that will initialize and terminate your global object. This code should reside in a header and source file named for the global object. For example, TheBESLog.[hC] is used to initialize TheBESLog instance. The header file merely declares a pointer to the global object just as you would for normal global objects.
TheGlobalObject.h
ifndef E_TheGlobalObject_H define E_TheGlobalObject_H
include <GlobalObject.h>
extern GlobalObject *TheGlobalObject;
endif
The source file is where you implement the initialization function and, if necessary, the termination function.
TheGlobalObject.C
include <TheGlobalObject.h> include <globalObject.h> include <BESInitList.h>
GlobalObject *TheGlobalObject = 0;
static bool buildTheGlobalObject(int argc, char **argv) { TheGlobalObject = new globalObject(...) ; return true ; }
static bool destroyTheGlobalObject(void) { if(TheGlobalObject) delete TheGlobalObject ; return true ; }
FUNINITQUIT(buildTheGlobalObject, destroyTheGlobalObject, APPL_INIT2)
The macro FUNINITQUIT takes an initialization function and a termination function to be used in the order specified by the order macro APPL_INIT2, which would be defined in one of TheInit.h header files. If there is nothing to terminate then you could have used the following macro:
FUNINIT(buildTheGlobalObject, APPL_INIT2)
The macro FUNINIT only takes an initialization function, no termination function, and the order macro APPL_INIT2.
Each time one of these two macros is used a BESGlobalInit object is instantiated, passing the specified initialization function and terminiation function (if one is needed) to the constructor along with the level at which these functions will be called and the current BESGlobalInit object first in line for that level. Remember, multiple initialization functions can be called at any given level, and the calling of these functions within a level is random. What this mechanism provides is an ordering for objects in different levels.
HOW TO INCORPORATE THE CODE INTO YOUR APPLICATION
In the main routine of your application the function BESGlobalInit is run, which causes the initialization of the global objects to occur. Before exiting your application the function BESGlobalQuit is run, which causues the terminiation functions to be called, cleaning up all of your global objects.
Definition at line 139 of file BESGlobalIQ.h.
bool BESGlobalIQ::BESGlobalInit | ( | int | argc, | |
char ** | argv | |||
) | [static] |
initialize global objects in an orderly fasion.
There are multiple levels of initialization, which allows for the ordering of global objects to be created, functions to be called, whatever initialization needs to take place for your application. This function traverses the different levels, in order, calling the first initialization function for that level. The initialization object is BESGlobalInit, which contains an initialization function, a termination function (if one is provided), and a pointer to the next BESGlobalInit object. So there can be multiple initialization routines called per level.
argc | number of arguments being passed in the argv parameter. This is the same as the argc command line argument passed to main. | |
argv | the actual arguments being passed. This is the same as the command line arguments passed to main. |
Definition at line 62 of file BESGlobalIQ.cc.
References BESGlobalInitList, and BESInitializer::initialize().
Referenced by BESApacheWrapper::BESApacheWrapper(), BESBaseApp::initialize(), baseApp::initialize(), and main().
bool BESGlobalIQ::BESGlobalQuit | ( | void | ) | [static] |
Runs the termination functions in reverse order of initialization, providing the application to clean up its global objects.
This function traverses the levels in reverse order calling the first termination function in that level.
Definition at line 95 of file BESGlobalIQ.cc.
References BESGlobalInitList, and BESInitializer::terminate().
Referenced by main(), BESBaseApp::terminate(), baseApp::terminate(), and BESApacheWrapper::~BESApacheWrapper().