next up previous contents
Next: With CMOD Up: Define the Pike Level Previous: Define the Pike Level   Contents

Without CMOD

As stated earlier Pike needs the function pike_module_init() and the function pike_module_exit() to recognize the module as a module.

Since the ReversedString module is that simple the pike_module_init() function was just the following few lines:

struct data{
   char buf[50];
}
 
void pike_module_init(void)
{
   start_new_program();
   ADD_STORAGE(struct data);
   ADD_FUNCTION("create", reversed_string_create, 
                 tFunc(tString, tVoid), 0);
   ADD_FUNCTION("write", reversed_string_write, 
                 tFunc(tInt, tVoid),0);
   set_init_callback(init_reversed_string);
   set_exit_callback(exit_reversed_string);
   end_class("ReversedString",0);
}
The effect of each line of code is described below.

start_new_program();
Tells the system that this is the beginning of a new class.

ADD_STORAGE (struct data);
Creates storage for a struct data. This is the first ADD_STORAGE() in the class. As a consequence of this the struct data is accessed through the global variable Pike_fp-current_storage which points directly to the struct.

ADD_FUNCTION("create", reversed_string_create, 
              tFunc(tString, tVoid),0);
Adds the Pike level create() method, or in other words the constructor, of the class and maps it to the reversed_string_create() C level function. The first parameter of ADD_FUNCTION() is the name of the Pike level method, the second parameter is the underlying C level function to which it is mapped, that means the low level function that is called when the high level method is invoked. The third parameter specifies what data types the Pike level method's arguments and return values are. The fourth argument specifies whether the Pike level method is static or public or...

ADD_FUNCTION("write", reversed_string_write, 
               tFunc(tInt, tVoid), 0);
This line adds the write() method. It works in the same way as the addition of the create method.

set_init_callback(init_reversed_string);
This line sets the init callback. Each time a ReversedString object is initialized init_reversed_string() will be run.

set_exit_callback(exit_reversed_string);
This line sets the exit callback. The exit_reversed_string() function will be run each time a ReversedString object is about to be garbage collected.
end_class("ReversedString",0);
This function call is the terminating mark for the class that started at the start_new_program() call ends here, and that the name of that class is 'ReversedString'.

So that was the whole pike_module_init(). The module also needs a pike_module_exit() function. In this case there is nothing to be done in that function. Still it needs to exist, and that is why there is such an empty function.


next up previous contents
Next: With CMOD Up: Define the Pike Level Previous: Define the Pike Level   Contents
2003-03-04