Previous Section Next Section

16.5 Calling e Methods from C

Calling an e method from C is done by means of the SN_DISPATCH macro. You can use SN_DISPATCH in C routines that have been called from e or in C routines called from other C code. The syntax for the SN_DISPATCH macro is as follows:

SN_DISPATCH(method-name, enclosing-struct, type-name, (enclosing-struct, params,...))

The arguments for SN_DISPATCH macro are shown in Table 16-1.

Table 16-1. SN_DISPATCH Macro Arguments

method-name

The name of the method to be called, exactly as declared in e.

enclosing-struct

The instance of the struct that contains the method. If SN_DISPATCH is used in a C routine that is called from e, then the method's enclosing struct instance was passed implicitly to the C routine when the C routine was called.

type-name

The e type of the struct.

params, …

Parameters passed to the called method (if any parameters are required). The parentheses around the parameters are required.

The e method must be exported from e to C with C export as shown below:

C export your_struct_type.your_method();

Example 16-11 shows use of an e method call from a C routine by means of the SN_DISPATCH macro.

Example 16-11 Calling e Methods from C using SN_DISPATCH Macro
e File name: print_method.e
<'
struct event_methods {
   print_message(i : int) is { -- e method definition
      outf("Message to print this number: %d\n", i);
   };
};

C export event_methods.print_reply(); -- Export the e method
'>
--------------------------------------------------------------
C File name: print_method.c
#include <stdio.h>
#include "print_method.h"

/*Define C routine c_message. Whenever this C routine is
called, it will in turn call the e routine print_message*/
void c_message(int i) {
   /*Declare a variable in C of struct type event_methods*/
   SN_TYPE(event_methods) c_em = SN_STRUCT_NEW(event_methods);
   /*Call the print_message e method with argument i from C*/
   SN_DISPATCH(print_message, c_em, event_methods, (c_em, i));
}
Previous Section Next Section