next up previous contents
Next: 4.2 Initial conditions Up: 4 Writing new objects Previous: 4 Writing new objects   Contents

4.1 A template for new object classes

GTS comes with a simple script called gtstemplate which will generate a C code template for the new class you want to create. For a summary of its syntax just type:

% gtstemplate
Usage: gtstemplate [OPTIONS] Class ParentClass
Options:
        [--no-extra-data]
        [--no-extra-method]
        [--overload=METHOD]
As an example we are going to try and create a template for a new initial condition class called InitPeriodic. Just type:
% gtstemplate --overload=read --overload=write --overload=event \
InitPeriodic GfsInit > init_periodic.c
We have just created a template for a new class called InitPeriodic derived from GfsInit and where the read, write and event methods are overloaded. Fire up your favourite editor and have a look at the file generated: init_periodic.c. The first thing you see is that the file is divided in two sections starting with
/* InitPeriodic: Header */
and
/* InitPeriodic: Object */
As their names indicate these sections correspond respectively to the declaration of the structures and functions (header part) and to the corresponding definitions (object part). If we were to use these functions and structures in a library, these two parts would be in separate files (a .h and a corresponding .c file). For what we are interested in (a gerris module) they are fine staying in the same file. There is a function we will not need: init_periodic_new, just remove the lines declaring and defining it in the header and object sections. We are left with only one function: init_periodic_class. This function essentially registers our new class and its associated attributes. Things like: If we now look at what the init_periodic_class_init function does, we see that it indeed overloads the event, read and write methods of our class with locally defined functions. The GFS_EVENT_CLASS and GTS_OBJECT_CLASS macro calls are casting operators which change the type of our new class (InitPeriodicClass) to the types of one of its parent class. GtsObjectClass is the ancestor of all object classes and we see that the read and write methods are thus defined for all objects. The GfsInit class is also a descendant of the GfsEvent class (as we have seen before it is a special type of event occurring once at the start of the simulation) which has and associated event method.


next up previous contents
Next: 4.2 Initial conditions Up: 4 Writing new objects Previous: 4 Writing new objects   Contents