Go to the first, previous, next, last section, table of contents.


Messaging from C

To invoke a method of a TOM object, use the TRT_SEND macro. For example, to retrieve the length of an array a:

tom_object a = ...;

tom_int len = TRT_SEND (, a, SEL (i_length));

The second argument to TRT_SEND is the receiver of the message. The third argument is the selector to be sent. A selector is an invocation of the SEL macro, with as argument the selector's name (see section Selectors) with any nasty characters replaced by an underscore.

A more elborate example shows almost all pitfalls when using TRT_SEND. In this example the method (int, float) split float f is invoked, which in TOM would be written as

SomeClass receiver = ...;
float fractional, number = ...;
int integer;

(integer, fractional) = [receiver split number]

is invoked from C as follows:

tom_object receiver = ...;
tom_float fractional, number = ...;
tom_int integer;

integer = TRT_SEND ((tom_int (*) (tom_object, selector,
                                  tom_float, tom_float *)),
                    receiver, SEL (_if__split_f), number, &fractional);

The first argument to TRT_SEND is a cast to the type of the function actually being invoked. This cast is mandatory if the return type of the function (implementing the method) invoked is not a tom_int. The argument prototypes in the cast are mandatory when needed to prevent the C compiler from doing undesirable type conversions due to it not having seen a full prototype of the function being invoked. For instance, usually, tom_float is simply a float, which the compiler will `upgrade' to a double when passed as an argument for which the prototype has not been seen.

This example also shows that for a method returning a tuple, the first element of the tuple (or the first element thereof in case it is a tuple too (or...)) is actually returned from the C function implementation, and any remaining elements of the tuple returned are stored in variables the address' of which has been passed as `invisible' trailing arguments.


Go to the first, previous, next, last section, table of contents.