Previous Section Next Section

16.3 Accessing the e Environment from C

This section discusses how to access the e environment, its objects, fields, variables, types, enumerated types, and other environment objects from C code.

16.3.1 SN_TYPE Macro

The SN_TYPE macro declares an e type in C. Use this macro whenever an e enumerated type or struct must be declared in C. The syntax for the SN_TYPE C construct is shown below. The type_name can be any e type.

SN_TYPE(type-name)

Example 16-5 shows usage of the SN_TYPE macro. This example shows the declaration of a C routine that receives a string argument and returns a boolean value.

Example 16-5 SN_TYPE Macro
SN_TYPE(bool) check_name (SN_TYPE(string) name) {
....
}

16.3.2 SN_LIST Macro

The SN_LIST macro declares an e list type in C. Use this macro whenever an e list must be declared in C. The syntax for the SN_LIST C construct is shown below. The type_name can be any e type.

SN_LIST(type-name)

Example 16-6 shows usage of the SN_LIST macro. This example shows the declaration of a C routine that receives a list of names and a string argument and returns a boolean value.

Example 16-6 SN_LIST Macro
SN_TYPE(bool) item_is_in_list(
    SN_LIST(string) list_of_names, /*List of names*/
    SN_TYPE(string) name
)
{
....
}

16.3.3 SN_ENUM Macro

Any enum defined in e can be used in C like any other e type using the SN_TYPE macro. An enumerated value name can be specified with the SN_ENUM macro. The syntax for the SN_ENUM macro is shown below. The enum_type_name can be any e type.

SN_ENUM(enum-type-name,value-name)

Example 16-7 shows usage of the SN_ENUM macro.

Example 16-7 SN_ENUM Macro
In e code:
<'
type color: [red, green, blue]; -- Define enumerated type in e
C export color; -- Export the enumerated type to C
'>
---------------------------------------------------------------------
In C code:

SN_TYPE(color) last_color; /*Define a variable of enum color*/
last_color = SN_ENUM(color,red); /*Call to SN_ENUM*/

16.3.4 SN_DEFINE Macro

The SN_DEFINE macro specifies an e defined name in C. Use this macro whenever an e defined name is to be used in C. The syntax for the SN_DEFINE C construct is shown below.

SN_DEFINE(defined-name)

Example 16-8 shows usage of the SN_DEFINE macro.

Example 16-8 SN_DEFINE Macro
In e:
<'
define NUM_OF_PORTS 5;
'>
----------------------------------------------------------------------
In C:
unsigned int num_of_ports = SN_DEFINE(NUM_OF_PORTS);

16.3.5 Passing Objects between e and C

A struct in e is represented in C as a pointer to a C struct. You access its fields using the -> operator. The name of the C struct field is the same as defined in e. A struct field whose name is a keyword in C (like int or for) is not accessible in C. Example 16-9 shows how to access e struct fields in C code.

Example 16-9 Accessing e Struct Fields in C
File Name: example.e
<'
struct cell { -- Define new struct
    data: int;
};

struct packet { -- Define new struct
    len: int; -- Length field
        keep len == 50; -- Constraint on len field
    cell: cell; -- Instantiate another struct
};
C export packet;

extend sys {
    packet :packet; -- Instantiate the packet
    run() is also {
        pkt(); -- Call an e routine that is implemented as a C routine.
    };
};
'>

<'
-- Define an e routine pkt() that points to C routine
-- my_c_func
routine pkt() is C routine my_c_func;
'>

File name: example_me.c
#include "e_ifc_file_.h"

/*Define the routine to implement the pkt() e routine*/
void my_c_func()
{
    SN_TYPE(packet) p_packet; /*Define an e struct type in C*/
    SN_TYPE(cell) p_cell; /*Define an e struct type in C*/
    p_packet = SN_SYS->packet; /*Access field packet of sys struct,
                               SN_SYS corresponds to the sys struct
                               in e*/
    p_cell = p_packet->cell; /*Access field cell of packet struct*/
    printf("\n PACKET_LEN = %d \n",p_packet->len); /*print field*/
}
Previous Section Next Section