[Date Prev][Date Next][Thread Prev][Thread Next][Author Index][Date Index][Thread Index]

Initialization in X++



With the introduction of the initTime hook in smalltalk, we have once
again found a situation where C++'s static initialization and its random
order is not good enough.  Some time ago, Markm proposed a mechanism to
have a list of functions to perform at initialization, but after the static
stuff.  With this new round, I've gone and implemented such a creature.
Note, however, that presently I don't have a hook to specify the order of
execution of these initializers.  What is guaranteed is that, if used as
indicated below, they will all execute after static initialization is
complete.  What follows is extracted from initx.hxx:

--- initx.hxx ---

Initializers orchestrate the execution of initialization code that cannot
run prior to the initialization of memory allocation, fluid variables,
the category structures, or in any other case where the code must run after
static construction time.  Each module's initialization is specified by

	INIT_TIME(initName, { code });

This macro creates a subclass of Initializer called initNameInitializer,
along with the declaration of an instance of initNameInitializer which is
linked onto a list at static construction time.
The constructor for the Initializer base class takes a boolean argument
where TRUE causes the list of initializers to execute.  The main() function
should then instanciate an Initializer object as in:

	Initializer mainInit(TRUE);

#define INIT_TIME(name,block)					\
	class CAT(name,Initializer) : public Initializer {	\
	  public:						\
	    CAT(name,Initializer) : Initializer(FALSE) {}	\
	  protected:						\
	    LEAF void initialize () { block }			\
	};

--- end of hxx ---

In smalltalk, the translator produces INIT_TIMEs for initTimeInherited and
initTimeNonInherited with the names className_initTimeInherited and
className_initTimeNonInherited, respectively.  With this change to the
translator, all class variables used by the cxx files should appear in a
linkTime(Non)Inherited method in order for them to be declared in the cxx file.
Values for link time initialization are obviously simple ones, such as NULL or 0.
The instanciation of the Initializer in main() is taken care of by the
translator.

I have already put in all the necessary linkTime...s in xpp, but there are
many that will be needed in the other sections.  I'll leave those to the
responsible parties.

While waiting for the current (apr16) merge, I'll start work on an EXIT_TIME
facility to coordinate shutdown functions.  I thought of having exit time
being handled by Initializer destructors, but it would be a real nuisance to
have the names mangled correctly.  Therefore, there will be a Terminator
class whose destruction on exit from main causes termination code to run.

	- e -