~ubuntu-branches/ubuntu/edgy/swig1.3/edgy

« back to all changes in this revision

Viewing changes to Doc/Manual/Arguments.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-12-05 01:16:04 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051205011604-ygx904it6413k3go
Tags: 1.3.27-1ubuntu1
Resynchronise with Debian again, for the new subversion packages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
<html>
3
3
<head>
4
4
<title>Argument Handling</title>
 
5
<link rel="stylesheet" type="text/css" href="style.css">
5
6
</head>
6
7
 
7
8
<body bgcolor="#ffffff">
8
9
<H1><a name="Arguments"></a>9 Argument Handling</H1>
9
10
<!-- INDEX -->
 
11
<div class="sectiontoc">
10
12
<ul>
11
13
<li><a href="#Arguments_nn2">The typemaps.i library</a>
12
14
<ul>
23
25
<li><a href="#Arguments_nn11">Applying constraints to new datatypes</a>
24
26
</ul>
25
27
</ul>
 
28
</div>
26
29
<!-- INDEX -->
27
30
 
28
31
 
44
47
<H2><a name="Arguments_nn2"></a>9.1 The typemaps.i library</H2>
45
48
 
46
49
 
 
50
<p>
47
51
This section describes the <tt>typemaps.i</tt> library file--commonly used to
48
52
change certain properties of argument conversion.
 
53
</p>
49
54
 
50
55
<H3><a name="Arguments_nn3"></a>9.1.1 Introduction</H3>
51
56
 
52
57
 
 
58
<p>
53
59
Suppose you had a C function like this:
 
60
</p>
54
61
 
55
 
<blockquote><pre>
 
62
<div class="code"><pre>
56
63
void add(double a, double b, double *result) {
57
64
        *result = a + b;
58
65
}
59
 
</pre></blockquote>
 
66
</pre></div>
60
67
 
61
68
<p>
62
69
From reading the source code, it is clear that the function is storing
70
77
<tt>typemaps.i</tt> library file and write interface code like this:
71
78
</p>
72
79
 
73
 
<blockquote><pre>
 
80
<div class="code"><pre>
74
81
// Simple example using typemaps
75
82
%module example
76
83
%include "typemaps.i"
77
84
 
78
85
%apply double *OUTPUT { double *result };
 
86
%inlne %{
79
87
extern void add(double a, double b, double *result);
80
 
</pre></blockquote>
 
88
%}
 
89
</pre></div>
81
90
 
82
91
<p>
83
92
The <tt>%apply</tt> directive tells SWIG that you are going to apply
91
100
like this (shown for Python):
92
101
</p>
93
102
 
94
 
<blockquote><pre>
 
103
<div class="targetlang"><pre>
95
104
&gt;&gt;&gt; a = add(3,4)
96
105
&gt;&gt;&gt; print a
97
106
7
98
107
&gt;&gt;&gt;
99
 
</pre></blockquote>
 
108
</pre></div>
100
109
 
 
110
<p>
101
111
In this case, you can see how the output value normally returned in
102
112
the third argument has magically been transformed into a function
103
113
return value.  Clearly this makes the function much easier to use
104
114
since it is no longer necessary to manufacture a special <tt>double
105
115
*</tt> object and pass it to the function somehow.
 
116
</p>
106
117
 
107
118
<p>
108
119
Once a typemap has been applied to a type, it stays in effect for all future occurrences
109
120
of the type and name.  For example, you could write the following:
110
121
</p>
111
122
 
112
 
<blockquote><pre>
 
123
<div class="code"><pre>
113
124
%module example
114
125
%include "typemaps.i"
115
126
 
116
127
%apply double *OUTPUT { double *result };
 
128
 
 
129
%inline %{
117
130
extern void add(double a, double b, double *result);
118
131
extern void sub(double a, double b, double *result);
119
132
extern void mul(double a, double b, double *result);
120
133
extern void div(double a, double b, double *result);
 
134
%}
121
135
...
122
 
</pre></blockquote>
 
136
</pre></div>
123
137
 
 
138
<p>
124
139
In this case, the <tt>double *OUTPUT</tt> rule is applied to all of the functions that follow.
 
140
</p>
125
141
 
126
142
<p>
127
143
Typemap transformations can even be extended to multiple return values.
128
144
For example, consider this code:
129
145
</p>
130
146
 
131
 
<blockquote>
 
147
<div class="code">
132
148
<pre>
133
149
%include "typemaps.i"
134
150
%apply int *OUTPUT { int *width, int *height };
136
152
// Returns a pair (width,height)
137
153
void getwinsize(int winid, int *width, int *height);
138
154
</pre>
139
 
</blockquote>
 
155
</div>
140
156
 
 
157
<p>
141
158
In this case, the function returns multiple values, allowing it to be used like this:
 
159
</p>
142
160
 
143
 
<blockquote><pre>
 
161
<div class="targetlang"><pre>
144
162
&gt;&gt;&gt; w,h = genwinsize(wid)
145
163
&gt;&gt;&gt; print w
146
164
400
148
166
300
149
167
&gt;&gt;&gt;
150
168
</pre>
151
 
</blockquote>
 
169
</div>
152
170
 
153
171
<p>
154
172
It should also be noted that although the <tt>%apply</tt> directive is
156
174
rule names directly in arguments.  For example, you could write this:
157
175
</p>
158
176
 
159
 
<blockquote><pre>
 
177
<div class="code"><pre>
160
178
// Simple example using typemaps
161
179
%module example
162
180
%include "typemaps.i"
163
181
 
164
 
extern void add(double a, double b, double *OUTPUT);
165
 
</pre></blockquote>
 
182
%{
 
183
extern void add(double a, double b, double *OUTPUT);
 
184
%}
 
185
extern void add(double a, double b, double *OUTPUT);
 
186
</pre></div>
166
187
 
 
188
<p>
167
189
Typemaps stay in effect until they are explicitly deleted or redefined to something
168
190
else.   To clear a typemap, the <tt>%clear</tt> directive should be used.  For example:
 
191
</p>
169
192
 
170
 
<blockquote>
 
193
<div class="code">
171
194
<pre>
172
195
%clear double *result;      // Remove all typemaps for double *result
173
196
</pre>
174
 
</blockquote>
 
197
</div>
175
198
 
176
199
<H3><a name="Arguments_nn4"></a>9.1.2 Input parameters</H3>
177
200
 
181
204
input value:
182
205
</p>
183
206
 
184
 
<blockquote><pre>
 
207
<div class="code"><pre>
185
208
int *INPUT              
186
209
short *INPUT
187
210
long *INPUT
190
213
unsigned long *INPUT
191
214
double *INPUT
192
215
float *INPUT
193
 
</pre></blockquote>
 
216
</pre></div>
194
217
 
 
218
<p>
195
219
When used, it allows values to be passed instead of pointers.  For example, consider this
196
220
function:
 
221
</p>
197
222
 
198
 
<blockquote><pre>
 
223
<div class="code"><pre>
199
224
double add(double *a, double *b) {
200
225
        return *a+*b;
201
226
}
202
 
</pre></blockquote>
 
227
</pre></div>
203
228
 
 
229
<p>
204
230
Now, consider this SWIG interface:
 
231
</p>
205
232
 
206
 
<blockquote><pre>
 
233
<div class="code"><pre>
207
234
%module example
208
235
%include "typemaps.i"
209
236
...
 
237
%{
 
238
extern double add(double *, double *);
 
239
%}
210
240
extern double add(double *INPUT, double *INPUT);
211
241
 
212
 
</pre></blockquote>
 
242
</pre></div>
213
243
 
214
244
<p>
215
245
When the function is used in the scripting language interpreter, it will work like this:
216
246
</p>
217
247
 
218
 
<blockquote><pre>
 
248
<div class="targetlang"><pre>
219
249
result = add(3,4)
220
 
</pre></blockquote>
 
250
</pre></div>
221
251
 
222
252
<H3><a name="Arguments_nn5"></a>9.1.3 Output parameters</H3>
223
253
 
228
258
calling the function. Instead, one or more output values are returned. 
229
259
</p>
230
260
 
231
 
<blockquote><pre>
 
261
<div class="code"><pre>
232
262
int *OUTPUT
233
263
short *OUTPUT
234
264
long *OUTPUT
238
268
double *OUTPUT
239
269
float *OUTPUT
240
270
 
241
 
</pre></blockquote>
 
271
</pre></div>
242
272
<p>
243
273
These methods can be used as shown in an earlier example. For example, if you have this C function :</p>
244
274
 
245
 
<blockquote><pre>
 
275
<div class="code"><pre>
246
276
void add(double a, double b, double *c) {
247
277
        *c = a+b;
248
278
}
249
 
</pre></blockquote>
 
279
</pre></div>
250
280
 
251
281
<p>
252
282
A SWIG interface file might look like this :</p>
253
283
 
254
 
<blockquote><pre>
 
284
<div class="code"><pre>
255
285
%module example
256
286
%include "typemaps.i"
257
287
...
 
288
%inline %{
258
289
extern void add(double a, double b, double *OUTPUT);
259
 
 
260
 
</pre></blockquote>
261
 
 
 
290
%}
 
291
 
 
292
</pre></div>
 
293
 
 
294
<p>
262
295
In this case, only a single output value is returned, but this is not
263
296
a restriction.  An arbitrary number of output values can be returned by applying
264
297
the output rules to more than one argument (as shown previously).
 
298
</p>
265
299
 
266
300
<p>
267
301
If the function also returns a value, it is returned along with the argument. For example,
268
302
if you had this:
269
303
</p>
270
304
 
271
 
<blockquote><pre>
 
305
<div class="code"><pre>
272
306
extern int foo(double a, double b, double *OUTPUT);
273
 
</pre></blockquote>
 
307
</pre></div>
274
308
 
 
309
<p>
275
310
The function will return two values like this:
 
311
</p>
276
312
 
277
 
<blockquote>
 
313
<div class="targetlang">
278
314
<pre>
279
315
iresult, dresult = foo(3.5, 2)
280
316
</pre>
281
 
</blockquote>
 
317
</div>
282
318
 
283
319
<H3><a name="Arguments_nn6"></a>9.1.4 Input/Output parameters</H3>
284
320
 
287
323
When a pointer serves as both an input and output value you can use
288
324
the following typemaps :</p>
289
325
 
290
 
<blockquote><pre>
 
326
<div class="code"><pre>
291
327
int *INOUT
292
328
short *INOUT
293
329
long *INOUT
297
333
double *INOUT
298
334
float *INOUT
299
335
 
300
 
</pre></blockquote>
 
336
</pre></div>
301
337
 
302
338
<p>
303
339
A C function that uses this might be something like this:</p>
304
340
 
305
 
<blockquote><pre>
 
341
<div class="code"><pre>
306
342
void negate(double *x) {
307
343
        *x = -(*x);
308
344
}
309
345
 
310
 
</pre></blockquote>
 
346
</pre></div>
311
347
 
312
348
<p>
313
349
To make x function as both and input and output value, declare the
314
350
function like this in an interface file :</p>
315
351
 
316
 
<blockquote><pre>
 
352
<div class="code"><pre>
317
353
%module example
318
354
%include typemaps.i
319
355
...
 
356
%{
 
357
extern void negate(double *);
 
358
%}
320
359
extern void negate(double *INOUT);
321
360
 
322
 
</pre></blockquote>
 
361
</pre></div>
323
362
 
324
363
<p>
325
364
Now within a script, you can simply call the function normally :</p>
326
365
 
327
 
<blockquote><pre>
 
366
<div class="targetlang"><pre>
328
367
a = negate(3);         # a = -3 after calling this
329
 
</pre></blockquote>
 
368
</pre></div>
330
369
 
 
370
<p>
331
371
One subtle point of the <tt>INOUT</tt> rule is that many scripting languages
332
372
enforce mutability constraints on primitive objects (meaning that simple objects
333
373
like integers and strings aren't supposed to change).   Because of this, you can't
334
374
just modify the object's value in place as the underlying C function does in this example.
335
375
Therefore, the <tt>INOUT</tt> rule returns the modified value as a new object
336
376
rather than directly overwriting the value of the original input object.
 
377
</p>
337
378
 
338
379
<p>
339
380
<b>Compatibility note :</b> The <tt>INOUT</tt> rule used to be known as <tt>BOTH</tt> in earlier versions of
348
389
<tt>INOUT</tt> typemaps to different argument names.  For example:
349
390
</p>
350
391
 
351
 
<blockquote><pre>
 
392
<div class="code"><pre>
352
393
// Make double *result an output value
353
394
%apply double *OUTPUT { double *result };
354
395
 
358
399
// Make long *x inout
359
400
%apply long *INOUT {long *x};
360
401
 
361
 
</pre></blockquote>
 
402
</pre></div>
362
403
 
 
404
<p>
363
405
To clear a rule, the <tt>%clear</tt> directive is used:
 
406
</p>
364
407
 
365
 
<blockquote><pre>
 
408
<div class="code"><pre>
366
409
%clear double *result;
367
410
%clear Int32 *in, long *x;
368
 
</pre></blockquote>
 
411
</pre></div>
369
412
 
 
413
<p>
370
414
Typemap declarations are lexically scoped so a typemap takes effect from the point of definition to the end of the
371
415
file or a matching <tt>%clear</tt> declaration.
 
416
</p>
372
417
 
373
418
<H2><a name="Arguments_nn8"></a>9.2 Applying constraints to input values</H2>
374
419
 
375
420
 
 
421
<p>
376
422
In addition to changing the handling of various input values, it is
377
423
also possible to use typemaps to apply constraints. For example, maybe you want to
378
424
insure that a value is positive, or that a pointer is non-NULL. This
379
425
can be accomplished including the <tt>constraints.i</tt> library file.
 
426
</p>
380
427
 
381
428
<H3><a name="Arguments_nn9"></a>9.2.1 Simple constraint example</H3>
382
429
 
385
432
The constraints library is best illustrated by the following interface
386
433
file :</p>
387
434
 
388
 
<blockquote><pre>
 
435
<div class="code"><pre>
389
436
// Interface file with constraints
390
437
%module example
391
438
%include "constraints.i"
396
443
double inv(double NONZERO);          // Non-zero values
397
444
void   free(void *NONNULL);          // Non-NULL pointers only
398
445
 
399
 
</pre></blockquote>
 
446
</pre></div>
400
447
 
401
448
<p>
402
449
The behavior of this file is exactly as you would expect. If any of
410
457
<p>
411
458
The following constraints are currently available</p>
412
459
 
413
 
<blockquote><pre>
 
460
<div class="code"><pre>
414
461
POSITIVE                     Any number &gt; 0 (not zero)
415
462
NEGATIVE                     Any number &lt; 0 (not zero)
416
463
NONNEGATIVE                  Any number &gt;= 0
418
465
NONZERO                      Nonzero number
419
466
NONNULL                      Non-NULL pointer (pointers only).
420
467
 
421
 
</pre></blockquote>
 
468
</pre></div>
422
469
 
423
470
<H3><a name="Arguments_nn11"></a>9.2.3 Applying constraints to new datatypes</H3>
424
471
 
428
475
is easy to apply it to new datatypes using <tt>%apply</tt>. For
429
476
example :</p>
430
477
 
431
 
<blockquote><pre>
 
478
<div class="code"><pre>
432
479
// Apply a constraint to a Real variable
433
480
%apply Number POSITIVE { Real in };
434
481
 
435
482
// Apply a constraint to a pointer type
436
483
%apply Pointer NONNULL { Vector * };
437
484
 
438
 
</pre></blockquote>
 
485
</pre></div>
439
486
 
440
487
<p>
441
488
The special types of "Number" and "Pointer" can be applied to any
442
489
numeric and pointer variable type respectively. To later remove a
443
490
constraint, the <tt>%clear</tt> directive can be used :</p>
444
491
 
445
 
<blockquote><pre>
 
492
<div class="code"><pre>
446
493
%clear Real in;
447
494
%clear Vector *;
448
 
</pre></blockquote>
 
495
</pre></div>
449
496
 
450
497
</body>
451
498
</html>