next up previous contents
Next: 9 Optional Arguements in Up: IMPL_repdoc Previous: 7 Pointer Size   Contents

Subsections

8 Function Pointers

Code for the example is in function_pointers/.

All of the languages considered in this document contain some type of mechanism for the use of function pointers. C and C++ have an extensive capability, allowing functions to be stored, tabled, and passed as arguments. F90 allows only the last of these capabilities. Once again, by combining these languages, FORTRAN is enhanced so that F90 can have a complete set of operations with function pointers.

8.1 Basic C function Pointers

In C, each function has an address in the code segment. This address may be stored in a pointer (with special syntax), and later invoked from this pointer. This is the mechanism that is commonly used in callbacks and virtual function tables.

An example:

void (*event_handler)(Event*) = NULL;

void my_event_handler(Event *event) {
     printf("An event of type:%s occurred\n", event->type);
}

void do_tasks() {
   Event event;
   /* ....... Do some tasks */
   /* if event occurs */
   if (event_handler != NULL) {
     event_handler(event);
   }
}

main() {
    event_handler = my_event_handler;
    do_tasks();
}

An F90 program can use the interface with C to store off F90 functions for later calling. Thus F90 can take advantage of the callback mechanism that is so prevalant in C programs. The callback can be either a C or an F90 function.

The example code demonstrates storing three different sort routines in a callback table. One sort is written in C, another in F90, and another is function within an F90 module. The routine ctable.c contains some C routines which provide an API for storing and later calling the sort routines. In the example each sort function takes a two dimensional array and the lenghts of each dimension as argument. Each column is considered to be a list of integers to be sorted. The sort routines sorts all columns in place.

typedef struct  {
        void (*func)(int*, int*,int*);
} VFUNC_TABLE;

VFUNC_TABLE func_table[20];

static int nfuncs = 0;

void FTN(registersortfunc)(void (*func)(int*,int*,int*)) {
        func_table[nfuncs++].func = func; 
}

void FTN(callsortfunc)(int *i, int *array, int *nel,int *nel1) {
        func_table[(*i) - 1].func(array, nel, nel1);
}

The main program is written in F90. It registers the three sort functions, then randomizes a list and calls each sort routine. The functions from ctable.c are used to store the function pointers.

        program fptr
        use mod_fptr
        integer :: i
        integer, target :: int_list(3,100)
        integer, pointer :: int_slice(:,:)

! Functions used as pointers must be external (or module)
        external csort, f90sort 

        call random_seed()

! Function fills list with random numbers
        call fill_random_list(int_list)

! Demonstrate passing a slice to a sort routine
        int_slice => int_list(1:2, 1:50)

        call print_list(int_list)

! Register the sort functions (i.e. save off pointers)
        call registersortfunc(csort)
        call registersortfunc(f90sort)
        call registersortfunc(f90modsort)

! Demonstrate calling a c sort routine with an array slice
        print *, "Using csort"
        call callsortfunc(1, int_slice,
     &  size(int_slice,1),size(int_slice,2))
        call print_list(int_list)

        call fill_random_list(int_list)

! Now call an f90 function that has been stored away
        print *, "Using f90sort"
        call callsortfunc(2, int_list, size(int_list,1),
     &  size(int_list,2))
        call print_list(int_list)

        call fill_random_list(int_list)

! Now call an f90 module function that has been stored away
        print *, "Using f90modsort"
        call callsortfunc(3, int_list,
     &  size(int_list,1),size(int_list,2))
        call print_list(int_list)

This example demonstrates how function pointers may be used in F90 and how F90 functions can be stored as callbacks, using a C library of function pointer routines. The sort example was chosen to demonstrate that such callback functions can be passed arguments, in this case a two dimensional array.


next up previous contents
Next: 9 Optional Arguements in Up: IMPL_repdoc Previous: 7 Pointer Size   Contents
esmf_support@list.woc.noaa.gov