Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beazley D.M.SWIG users manual.pdf
Скачиваний:
13
Добавлен:
23.08.2013
Размер:
1.53 Mб
Скачать

SWIG Users Guide

Pointers, Constraints, and Typemaps

102

 

 

 

void foo1(int *);

// Apply int * typemap

void foo2(int a[4]);

// Apply int[4] typemap

void foo3(int out[4]);

// Apply int out[4] typemap

void foo4(int *status);

// Apply int *status typemap

void foo5(int a[20]);

// Apply int * typemap (because int [20] is an int *)

Because SWIG uses a name-based approach, it is possible to attach special properties to named parameters. For example, we can make an argument of “int *OUTPUT” always be treated as an output value of a function or make a “char **argv” always accept a list of string values.

Common typemap methods

The following methods are supported by most SWIG language modules. Individual language may provide any number of other methods not listed here.

 

Common Typemap Methods

 

 

 

%typemap(lang,in)

 

Convert function arguments from the scripting language to a C

 

 

representation.

 

 

 

%typemap(lang,out)

 

Converts the return value from a function to a scripting lan-

 

 

guage representation.

 

 

 

%typemap(lang,ret)

 

Cleans up the return value of a function. For example, this

 

 

could be used to free up memory that might have been allo-

 

 

cated by the underlying C function. This code is executed

 

 

before a function returns control back to the scripting language.

 

 

 

%typemap(lang,freearg)

 

Cleans up arguments. This method can be used to clean up

 

 

function arguments that might have required memory alloca-

 

 

tion or other special processing.

 

 

 

%typemap(lang,argout)

 

Outputs argument values. This typemap can be used to make a

 

 

function return a value from one of its arguments.

 

 

 

%typemap(lang,check)

 

Checks validity of function inputs. Can be used to apply con-

 

 

straints, raise exceptions, or simply for debugging.

 

 

 

%typemap(lang,varin)

 

Used by some languages to set the value of a C global variable.

 

 

Converts a datatype from the scripting language to C. This

 

 

method is only used if the “in” method won’t work for some

 

 

reason.

 

 

 

%typemap(lang,varout)

 

Variable. Convert the value of a C global variable to a scripting

 

 

language representation.

 

 

 

%typemap(lang,const)

 

Specifies the code used to create a constant in the module ini-

 

 

tialization function. Not supported by all languages.

 

 

 

%typemap(lang,memberin)

 

Set structure member. Specifies special processing of structure

 

 

and class members when setting a value.

 

 

 

%typemap(lang,memberout)

 

Get structure member. Special processing applied when

 

 

retrieving a structure member.

 

 

 

Version 1.1, June 24, 1997

SWIG Users Guide Pointers, Constraints, and Typemaps 103

 

Common Typemap Methods

 

 

 

%typemap(lang,arginit)

 

Initializes a parameter to an initial value (for example, setting a

 

 

pointer to a NULL value). Sometimes useful in determining

 

 

whether a parameter was received correctly.

 

 

 

%typemap(lang,default)

 

Can be used to set a default argument value. Overrides any

 

 

other default arguments that might have been specified.

 

 

 

%typemap(lang,ignore)

 

Set an argument to a default value and ignore it for the pur-

 

 

poses of generating wrapper code. (An ignored argument

 

 

becomes a hidden argument in the scripting interface).

 

 

 

Understanding how some of these methods are applied takes a little practice and better understanding of what SWIG does when it creates a wrapper function. The next few diagrams show the anatomy of a wrapper function and how the typemaps get applied. More detailed examples of typemaps can be found on the chapters for each target language.

 

 

 

 

/* A generic wrapper function */

 

 

 

 

int wrap_func(int argc, Arg *args[]) {

 

 

 

 

 

 

 

int arg0;

%typemap(lang,default)

 

 

 

int retval;

%typemap(lang,arginit)

 

 

...

 

 

%typemap(lang,ignore)

 

 

 

/* Check argument count */

 

 

 

 

 

 

 

 

 

 

 

...

 

 

 

 

 

 

 

/* Parse arguments */

%typemap(lang,in)

 

 

 

arg0 = Get_Int(args[0]);

%typemap(lang,check)

 

 

 

 

...

 

 

 

 

 

 

 

 

 

 

 

/* Call the C function */

 

 

 

 

 

 

 

retval = func(arg0);

 

 

 

 

 

 

 

/* Return the result */

%typemap(lang,out)

 

 

 

Set_ResultInt(retval);

%typemap(lang,argout)

 

 

 

/* Clean up or return args */

...

%typemap(lang,freearg)

 

 

 

/* Clean up return result */

%typemap(lang,ret)

 

 

 

 

 

 

 

 

 

 

 

...

 

 

 

 

 

 

 

 

 

 

 

 

return OK;

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

 

Wrapper function typemaps and where they are applied

Version 1.1, June 24, 1997