Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / html / swig / SWIG.html
CommitLineData
86530b38
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<html>
3<head>
4<title>SWIG Basics</title>
5<link rel="stylesheet" type="text/css" href="style.css">
6</head>
7
8<body bgcolor="#ffffff">
9<H1><a name="SWIG"></a>5 SWIG Basics</H1>
10<!-- INDEX -->
11<div class="sectiontoc">
12<ul>
13<li><a href="#SWIG_nn2">Running SWIG</a>
14<ul>
15<li><a href="#SWIG_nn3">Input format</a>
16<li><a href="#output">SWIG Output</a>
17<li><a href="#SWIG_nn5">Comments</a>
18<li><a href="#SWIG_nn6">C Preprocessor</a>
19<li><a href="#SWIG_nn7">SWIG Directives</a>
20<li><a href="#SWIG_nn8">Parser Limitations</a>
21</ul>
22<li><a href="#SWIG_nn9">Wrapping Simple C Declarations</a>
23<ul>
24<li><a href="#SWIG_nn10">Basic Type Handling</a>
25<li><a href="#SWIG_nn11">Global Variables</a>
26<li><a href="#SWIG_nn12">Constants</a>
27<li><a href="#SWIG_nn13">A brief word about <tt>const</tt></a>
28<li><a href="#SWIG_nn14">A cautionary tale of <tt>char *</tt></a>
29</ul>
30<li><a href="#SWIG_nn15">Pointers and complex objects</a>
31<ul>
32<li><a href="#SWIG_nn16">Simple pointers</a>
33<li><a href="#SWIG_nn17">Run time pointer type checking</a>
34<li><a href="#SWIG_nn18">Derived types, structs, and classes</a>
35<li><a href="#SWIG_nn19">Undefined datatypes</a>
36<li><a href="#SWIG_nn20">Typedef</a>
37</ul>
38<li><a href="#SWIG_nn21">Other Practicalities</a>
39<ul>
40<li><a href="#SWIG_nn22">Passing structures by value</a>
41<li><a href="#SWIG_nn23">Return by value</a>
42<li><a href="#SWIG_nn24">Linking to structure variables</a>
43<li><a href="#SWIG_nn25">Linking to <tt>char *</tt></a>
44<li><a href="#SWIG_nn26">Arrays</a>
45<li><a href="#SWIG_readonly_variables">Creating read-only variables</a>
46<li><a href="#SWIG_nn28">Renaming and ignoring declarations</a>
47<li><a href="#SWIG_default_args">Default/optional arguments</a>
48<li><a href="#SWIG_nn30">Pointers to functions and callbacks</a>
49</ul>
50<li><a href="#SWIG_nn31">Structures and unions</a>
51<ul>
52<li><a href="#SWIG_nn32">Typedef and structures</a>
53<li><a href="#SWIG_nn33">Character strings and structures</a>
54<li><a href="#SWIG_nn34">Array members</a>
55<li><a href="#SWIG_nn35">Structure data members</a>
56<li><a href="#SWIG_nn36">C constructors and destructors </a>
57<li><a href="#SWIG_adding_member_functions">Adding member functions to C structures</a>
58<li><a href="#SWIG_nn38">Nested structures</a>
59<li><a href="#SWIG_nn39">Other things to note about structure wrapping</a>
60</ul>
61<li><a href="#SWIG_nn40">Code Insertion</a>
62<ul>
63<li><a href="#SWIG_nn41">The output of SWIG</a>
64<li><a href="#SWIG_nn42">Code insertion blocks</a>
65<li><a href="#SWIG_nn43">Inlined code blocks</a>
66<li><a href="#SWIG_nn44">Initialization blocks</a>
67</ul>
68<li><a href="#SWIG_nn45">An Interface Building Strategy</a>
69<ul>
70<li><a href="#SWIG_nn46">Preparing a C program for SWIG</a>
71<li><a href="#SWIG_nn47">The SWIG interface file</a>
72<li><a href="#SWIG_nn48">Why use separate interface files?</a>
73<li><a href="#SWIG_nn49">Getting the right header files</a>
74<li><a href="#SWIG_nn50">What to do with main()</a>
75</ul>
76</ul>
77</div>
78<!-- INDEX -->
79
80
81
82<p>
83This chapter describes the basic operation of SWIG, the structure of its
84input files, and how it handles standard ANSI C declarations. C++ support is
85described in the next chapter. However, C++ programmers should still read this
86chapter to understand the basics.
87Specific details about each target language are described in later
88chapters.
89</p>
90
91<H2><a name="SWIG_nn2"></a>5.1 Running SWIG</H2>
92
93
94<p>
95To run SWIG, use the <tt>swig</tt> command with one or more of the
96following options and a filename like this:
97</p>
98
99<div class="shell"><pre>
100swig [ <em>options</em> ] filename
101
102-chicken Generate CHICKEN wrappers
103-csharp Generate C# wrappers
104-guile Generate Guile wrappers
105-java Generate Java wrappers
106-mzscheme Generate Mzscheme wrappers
107-ocaml Generate Ocaml wrappers
108-perl Generate Perl wrappers
109-php Generate PHP wrappers
110-pike Generate Pike wrappers
111-python Generate Python wrappers
112-ruby Generate Ruby wrappers
113-sexp Generate Lisp S-Expressions wrappers
114-tcl Generate Tcl wrappers
115-xml Generate XML wrappers
116-c++ Enable C++ parsing
117-D<em>symbol</em> Define a preprocessor symbol
118-Fstandard Display error/warning messages in commonly used format
119-Fmicrosoft Display error/warning messages in Microsoft format
120-help Display all options
121-I<em>dir</em> Add a directory to the file include path
122-l<em>file</em> Include a SWIG library file.
123-module <em>name</em> Set the name of the SWIG module
124-o <em>outfile</em> Name of output file
125-outdir <em>dir</em> Set language specific files output directory
126-swiglib Show location of SWIG library
127-version Show SWIG version number
128
129</pre></div>
130<p>
131This is a subset of commandline options.
132Additional options are also defined for each target language. A full list
133can be obtained by typing <tt>swig -help</tt> or <tt>swig
134-<em>lang</em> -help</tt>.
135</p>
136
137<H3><a name="SWIG_nn3"></a>5.1.1 Input format</H3>
138
139
140<p>
141As input, SWIG expects a file containing ANSI C/C++ declarations and
142special SWIG directives. More often than not, this is a special SWIG
143interface file which is usually denoted with a special <tt>.i</tt> or
144<tt>.swg</tt> suffix. In certain cases, SWIG can be used directly on
145raw header files or source files. However, this is not the most
146typical case and there are several reasons why you might not want to
147do this (described later).
148</p>
149
150<p>
151The most common format of a SWIG interface is as follows:
152</p>
153
154<div class="code"><pre>
155%module mymodule
156%{
157#include "myheader.h"
158%}
159// Now list ANSI C/C++ declarations
160int foo;
161int bar(int x);
162...
163</pre></div>
164<p>
165The name of the module is supplied using the special <tt>%module</tt>
166directive (or the <tt>-module</tt> command line option). This
167directive must appear at the beginning of the file and is used to name
168the resulting extension module (in addition, this name often defines
169a namespace in the target language). If the module name is supplied on the
170command line, it overrides the name specified with the
171<tt>%module</tt> directive.
172</p>
173
174<p>
175Everything in the <tt>%{ ... %}</tt> block is simply copied verbatim
176to the resulting wrapper file created by SWIG. This section is almost
177always used to include header files and other declarations that are
178required to make the generated wrapper code compile. It is important
179to emphasize that just because you include a declaration in a SWIG
180input file, that declaration does <em>not</em> automatically appear in
181the generated wrapper code---therefore you need to make sure you
182include the proper header files in the <tt>%{ ... %}</tt> section. It
183should be noted that the text enclosed in <tt>%{ ... %}</tt> is not
184parsed or interpreted by SWIG. The <tt>%{...%}</tt> syntax and
185semantics in SWIG is analogous to that of the declarations section
186used in input files to parser generation tools such as yacc or bison.
187</p>
188
189<H3><a name="output"></a>5.1.2 SWIG Output</H3>
190
191
192<p>
193The output of SWIG is a C/C++ file that contains all of the wrapper
194code needed to build an extension module. SWIG may generate some
195additional files depending on the target language. By default, an input file
196with the name <tt>file.i</tt> is transformed into a file
197<tt>file_wrap.c</tt> or <tt>file_wrap.cxx</tt> (depending on whether
198or not the <tt>-c++</tt> option has been used). The name of the
199output file can be changed using the <tt>-o</tt> option. In certain
200cases, file suffixes are used by the compiler to determine the source
201language (C, C++, etc.). Therefore, you have to use the
202<tt>-o</tt> option to change the suffix of the SWIG-generated wrapper
203file if you want something different than the default. For example:
204</p>
205
206<div class="shell"><pre>
207$ swig -c++ -python -o example_wrap.cpp example.i
208</pre></div>
209
210<p>
211The C/C++ output file created by SWIG often
212contains everything that is needed to construct a extension module
213for the target scripting language. SWIG is not a stub compiler nor is it
214usually necessary to edit the output file (and if you look at the output,
215you probably won't want to). To build the final extension module, the
216SWIG output file is compiled and linked with the rest of your C/C++
217program to create a shared library.
218</p>
219
220<p>
221Many target languages will also generate proxy class files in the
222target language. The default output directory for these language
223specific files is the same directory as the generated C/C++ file. This can
224can be modified using the <tt>-outdir</tt> option. For example:
225</p>
226
227<div class="shell"><pre>
228$ swig -c++ -python -outdir pyfiles -o cppfiles/example_wrap.cpp example.i
229</pre></div>
230<p>
231If the directories <tt>cppfiles</tt> and <tt>pyfiles</tt> exist, the following
232will be generated:</p>
233<div class="shell"><pre>
234cppfiles/example_wrap.cpp
235pyfiles/example.py
236</pre></div>
237
238<H3><a name="SWIG_nn5"></a>5.1.3 Comments</H3>
239
240
241<p>
242C and C++ style comments may appear anywhere in interface files. In
243previous versions of SWIG, comments were used to generate
244documentation files. However, this feature is currently under repair
245and will reappear in a later SWIG release.
246</p>
247
248<H3><a name="SWIG_nn6"></a>5.1.4 C Preprocessor</H3>
249
250
251<p>
252Like C, SWIG preprocesses all input files through an enhanced version
253of the C preprocessor. All standard preprocessor features are
254supported including file inclusion, conditional compilation and
255macros. However, <tt>#include</tt> statements are ignored unless the
256<tt>-includeall</tt> command line option has been supplied. The
257reason for disabling includes is that SWIG is sometimes used to
258process raw C header files. In this case, you usually only want the
259extension module to include functions in the supplied header file
260rather than everything that might be included by that header file
261(i.e., system headers, C library functions, etc.).
262</p>
263
264<p>
265It should also be noted that the SWIG preprocessor skips all text
266enclosed inside a <tt>%{...%}</tt> block. In addition, the
267preprocessor includes a number of macro handling enhancements that
268make it more powerful than the normal C preprocessor. These
269extensions are described in the "<a href="Preprocessor.html#Preprocessor">Preprocessor</a>" chapter.
270</p>
271
272<H3><a name="SWIG_nn7"></a>5.1.5 SWIG Directives</H3>
273
274
275<p>
276Most of SWIG's operation is controlled by special directives that are
277always preceded by a "<tt>%</tt>" to distinguish them from normal C
278declarations. These directives are used to give SWIG hints or to alter
279SWIG's parsing behavior in some manner.
280</p>
281
282<p>
283Since SWIG directives are not legal C syntax, it is generally not
284possible to include them in header files. However, SWIG directives can be
285included in C header files using conditional compilation like this:
286</p>
287
288<div class="code"><pre>
289/* header.h --- Some header file */
290
291/* SWIG directives -- only seen if SWIG is running */
292#ifdef SWIG
293%module foo
294#endif
295</pre>
296</div>
297
298<p>
299<tt>SWIG</tt> is a special preprocessing symbol defined by SWIG when
300it is parsing an input file.
301</p>
302
303<H3><a name="SWIG_nn8"></a>5.1.6 Parser Limitations</H3>
304
305
306<p>
307Although SWIG can parse most C/C++ declarations, it does not
308provide a complete C/C++ parser implementation. Most of these
309limitations pertain to very complicated type declarations and certain
310advanced C++ features. Specifically, the following features are not
311currently supported:
312</p>
313
314<ul>
315<li>Non-conventional type declarations.
316For example, SWIG does not support declarations such as the following
317(even though this is legal C):
318
319<div class="code">
320<pre>
321/* Non-conventional placement of storage specifier (extern) */
322const int extern Number;
323
324/* Extra declarator grouping */
325Matrix (foo); // A global variable
326
327/* Extra declarator grouping in parameters */
328void bar(Spam (Grok)(Doh));
329
330</pre>
331</div>
332
333<p>
334In practice, few (if any) C programmers actually write code like
335this since this style is never featured in programming books. However,
336if you're feeling particularly obfuscated, you can certainly break SWIG (although why would you want to?).
337</p>
338</li>
339
340<li>Running SWIG on C++ source files (what would appear in a .C or .cxx file)
341is not recommended. Even though SWIG can parse C++ class declarations,
342it ignores declarations that are decoupled from their
343original class definition (the declarations are parsed, but a lot of warning
344messages may be generated). For example:
345
346<div class="code">
347<pre>
348/* Not supported by SWIG */
349int foo::bar(int) {
350 ... whatever ...
351}
352</pre>
353</div>
354</li>
355
356<li>Certain advanced features of C++ such as nested classes
357are not yet supported. Please see the section on using SWIG
358with C++ for more information.
359</ul>
360
361<p>
362In the event of a parsing error, conditional compilation can be used to skip
363offending code. For example:
364</p>
365
366<div class="code">
367<pre>
368#ifndef SWIG
369... some bad declarations ...
370#endif
371</pre>
372</div>
373<p>
374Alternatively, you can just delete the offending code from the interface file.
375</p>
376
377<p>
378One of the reasons why SWIG does not provide a full C++ parser
379implementation is that it has been designed to work with incomplete
380specifications and to be very permissive in its handling of C/C++
381datatypes (e.g., SWIG can generate interfaces even when there are
382missing class declarations or opaque datatypes). Unfortunately, this
383approach makes it extremely difficult to implement certain parts of a
384C/C++ parser as most compilers use type information to assist in the
385parsing of more complex declarations (for the truly curious, the
386primary complication in the implementation is that the SWIG parser
387does not utilize a separate <em>typedef-name</em> terminal symbol as
388described on p. 234 of K&amp;R).
389</p>
390
391<H2><a name="SWIG_nn9"></a>5.2 Wrapping Simple C Declarations</H2>
392
393
394<p>
395SWIG wraps simple C declarations by creating an interface that closely matches
396the way in which the declarations would be used in a C program.
397For example, consider the following interface file:
398</p>
399
400<div class="code"><pre>
401%module example
402
403%inline %{
404extern double sin(double x);
405extern int strcmp(const char *, const char *);
406extern int Foo;
407%}
408#define STATUS 50
409#define VERSION "1.1"
410</pre></div>
411<p>
412In this file, there are two functions <tt>sin()</tt> and <tt>strcmp()</tt>,
413a global variable <tt>Foo</tt>, and two constants <tt>STATUS</tt> and
414<tt>VERSION</tt>. When SWIG creates an extension module, these
415declarations are accessible as scripting language functions, variables, and
416constants respectively. For example, in Tcl:
417</p>
418
419<div class="targetlang"><pre>
420% sin 3
4215.2335956
422% strcmp Dave Mike
423-1
424% puts $Foo
42542
426% puts $STATUS
42750
428% puts $VERSION
4291.1
430</pre></div>
431<p>
432Or in Python:
433</p>
434
435<div class="targetlang"><pre>
436&gt;&gt;&gt; example.sin(3)
4375.2335956
438&gt;&gt;&gt; example.strcmp('Dave','Mike')
439-1
440&gt;&gt;&gt; print example.cvar.Foo
44142
442&gt;&gt;&gt; print example.STATUS
44350
444&gt;&gt;&gt; print example.VERSION
4451.1
446</pre></div>
447<p>
448Whenever possible, SWIG creates an interface that closely matches the underlying C/C++
449code. However, due to subtle differences between languages, run-time
450environments, and semantics, it is not always possible to do so. The
451next few sections describes various aspects of this mapping.
452</p>
453
454<H3><a name="SWIG_nn10"></a>5.2.1 Basic Type Handling</H3>
455
456
457<p>
458In order to build an interface, SWIG has to convert C/C++ datatypes to
459equivalent types in the target language. Generally,
460scripting languages provide a more limited set of primitive types than C.
461Therefore, this conversion process involves a certain amount of type
462coercion.
463</p>
464
465<p>
466Most scripting languages provide a single integer type that is implemented using
467the <tt>int</tt> or <tt>long</tt> datatype in C. The following list shows
468all of the C datatypes that SWIG will convert to and from integers in the target language:
469</p>
470
471<div class="code"><pre>
472int
473short
474long
475unsigned
476signed
477unsigned short
478unsigned long
479unsigned char
480signed char
481bool
482</pre></div>
483
484<p>
485When an integral value is converted from C, a cast is used to convert it to
486the representation in the target language.
487Thus, a 16 bit short in C may be promoted to a 32 bit integer. When integers are
488converted in the other direction, the value is cast back into the original C type.
489If the value is too large to fit, it is silently truncated.
490<!-- Dave: Maybe we should fix this -->
491</p>
492
493<p>
494<tt>unsigned char</tt> and <tt>signed char</tt> are special cases that
495are handled as small 8-bit integers. Normally, the <tt>char</tt>
496datatype is mapped as a one-character ASCII string. </p>
497
498<p>
499The <tt>bool</tt> datatype is cast to and from an integer value of 0
500and 1 unless the target language provides a special boolean type.</p>
501
502<p>
503Some care is required when working with large integer values. Most
504scripting languages use 32-bit integers so mapping a 64-bit long
505integer may lead to truncation errors. Similar problems may arise with
50632 bit unsigned integers (which may appear as large negative
507numbers). As a rule of thumb, the <tt>int</tt> datatype and all
508variations of <tt>char</tt> and <tt>short</tt> datatypes are safe to
509use. For <tt>unsigned int</tt> and <tt>long</tt> datatypes, you will
510need to carefully check the correct operation of your program after
511it has been wrapped with SWIG.
512</p>
513
514<p>
515Although the SWIG parser supports the <tt>long long</tt> datatype, not
516all language modules support it. This is because <tt>long long</tt>
517usually exceeds the integer precision available in the target
518language. In certain modules such as Tcl and Perl5, <tt>long
519long</tt> integers are encoded as strings. This allows the full range
520of these numbers to be represented. However, it does not allow
521<tt>long long</tt> values to be used in arithmetic expressions. It
522should also be noted that although <tt>long long</tt> is part
523of the ISO C99 standard, it is not universally supported by all C
524compilers. Make sure you are using a compiler that supports <tt>long
525long</tt> before trying to use this type with SWIG.
526</p>
527
528<p>
529SWIG recognizes the following floating point types :</p>
530
531<div class="code"><pre>
532float
533double
534</pre></div>
535
536<p>
537Floating point numbers are mapped to and from the natural
538representation of floats in the target language. This is almost always
539a C <tt>double</tt>. The rarely used datatype of <tt>long double</tt>
540is not supported by SWIG.</p>
541
542<p>
543The <tt>char</tt> datatype is mapped into a NULL terminated ASCII
544string with a single character. When used in a scripting language it
545shows up as a tiny string containing the character value. When
546converting the value back into C, SWIG takes a character string
547from the scripting language and strips off the first character as the
548char value. Thus if the value "foo" is assigned to a
549<tt>char</tt> datatype, it gets the value `f'.</p>
550
551<p>
552The <tt>char *</tt> datatype is handled as a NULL-terminated ASCII
553string. SWIG maps this into a 8-bit character string in the target
554scripting language. SWIG converts character strings in the target
555language to NULL terminated strings before passing them into
556C/C++. The default handling of these strings does not allow them to
557have embedded NULL bytes. Therefore, the <tt>char *</tt> datatype is
558not generally suitable for passing binary data. However, it is
559possible to change this behavior by defining a SWIG typemap. See the chapter
560on <a href="Typemaps.html#Typemaps">Typemaps</a> for details about this.
561</p>
562
563<p>
564At this time, SWIG does not provide any special support for Unicode or
565wide-character strings (the C <tt>wchar_t</tt> type). This is a
566delicate topic that is poorly understood by many programmers and not
567implemented in a consistent manner across languages. For those
568scripting languages that provide Unicode support, Unicode strings are
569often available in an 8-bit representation such as UTF-8 that can be
570mapped to the <tt>char *</tt> type (in which case the SWIG interface
571will probably work). If the program you are wrapping uses Unicode,
572there is no guarantee that Unicode characters in the target language
573will use the same internal representation (e.g., UCS-2 vs. UCS-4).
574You may need to write some special conversion functions.
575</p>
576
577<H3><a name="SWIG_nn11"></a>5.2.2 Global Variables</H3>
578
579
580<p>
581Whenever possible, SWIG maps C/C++ global variables into scripting language
582variables. For example,
583</p>
584
585<div class="code"><pre>
586%module example
587double foo;
588
589</pre></div>
590<p>
591results in a scripting language variable like this:
592</p>
593
594<div class="code"><pre>
595# Tcl
596set foo [3.5] ;# Set foo to 3.5
597puts $foo ;# Print the value of foo
598
599# Python
600cvar.foo = 3.5 # Set foo to 3.5
601print cvar.foo # Print value of foo
602
603# Perl
604$foo = 3.5; # Set foo to 3.5
605print $foo,"\n"; # Print value of foo
606
607# Ruby
608Module.foo = 3.5 # Set foo to 3.5
609print Module.foo, "\n" # Print value of foo
610</pre></div>
611<p>
612Whenever the scripting language variable is used, the underlying C
613global variable is accessed. Although SWIG makes every
614attempt to make global variables work like scripting language
615variables, it is not always possible to do so. For instance, in
616Python, all global variables must be accessed through a special
617variable object known as <tt>cvar</tt> (shown above). In Ruby, variables are
618accessed as attributes of the module. Other languages may
619convert variables to a pair of accessor functions. For example, the
620Java module generates a pair of functions <tt>double get_foo()</tt>
621and <tt>set_foo(double val)</tt> that are used to manipulate the
622value.
623</p>
624
625<p>
626Finally, if a global variable has been declared as <tt>const</tt>, it
627only supports read-only access. Note: this behavior is new to SWIG-1.3.
628Earlier versions of SWIG incorrectly handled <tt>const</tt> and created
629constants instead.
630</p>
631
632<H3><a name="SWIG_nn12"></a>5.2.3 Constants</H3>
633
634
635<p>
636Constants can be created using <tt>#define</tt>, enumerations,
637or a special <tt>%constant</tt> directive. The following
638interface file shows a few valid constant declarations :</p>
639
640<div class="code"><pre>
641#define I_CONST 5 // An integer constant
642#define PI 3.14159 // A Floating point constant
643#define S_CONST "hello world" // A string constant
644#define NEWLINE '\n' // Character constant
645
646enum boolean {NO=0, YES=1};
647enum months {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG,
648 SEP, OCT, NOV, DEC};
649%constant double BLAH = 42.37;
650#define F_CONST (double) 5 // A floating pointer constant with cast
651#define PI_4 PI/4
652#define FLAGS 0x04 | 0x08 | 0x40
653
654</pre></div>
655<p>
656In <tt>#define</tt> declarations, the type of a constant is inferred
657by syntax. For example, a number with a decimal point is assumed to be
658floating point. In addition, SWIG must be able to fully resolve all
659of the symbols used in a <tt>#define</tt> in order for a constant to
660actually be created. This restriction is necessary because
661<tt>#define</tt> is also used to define preprocessor macros that are
662definitely not meant to be part of the scripting language interface.
663For example:
664</p>
665
666<div class="code">
667<pre>
668#define EXTERN extern
669
670EXTERN void foo();
671</pre>
672</div>
673<p>
674In this case, you probably don't want to create a constant called
675<tt>EXTERN</tt> (what would the value be?). In general,
676SWIG will not create constants for macros unless the value can
677be completely determined by the preprocessor. For instance, in the above example,
678the declaration
679</p>
680
681<div class="code">
682<pre>
683#define PI_4 PI/4
684</pre>
685</div>
686<p>
687defines a constant because <tt>PI</tt> was already defined as a
688constant and the value is known.
689</p>
690
691<p>
692The use of constant expressions is allowed, but SWIG does not evaluate
693them. Rather, it passes them through to the output file and lets the C
694compiler perform the final evaluation (SWIG does perform a limited
695form of type-checking however).</p>
696
697<p>
698For enumerations, it is critical that the original enum definition be
699included somewhere in the interface file (either in a header file or
700in the <tt>%{,%}</tt> block). SWIG only translates the enumeration
701into code needed to add the constants to a scripting language. It
702needs the original enumeration declaration in order to get the correct
703enum values as assigned by the C compiler.
704</p>
705
706<p>
707The <tt>%constant</tt> directive is used to more precisely create
708constants corresponding to different C datatypes. Although it is not
709usually not needed for simple values, it is more useful when working
710with pointers and other more complex datatypes. Typically, <tt>%constant</tt>
711is only used when you want to add constants to the scripting language
712interface that are not defined in the original header file.
713</p>
714
715<H3><a name="SWIG_nn13"></a>5.2.4 A brief word about <tt>const</tt></H3>
716
717
718<p>
719A common confusion with C programming is the semantic meaning of the
720<tt>const</tt> qualifier in declarations--especially when it is mixed
721with pointers and other type modifiers. In fact, previous versions of SWIG
722handled <tt>const</tt> incorrectly--a situation that SWIG-1.3.7 and newer
723releases have fixed.
724</p>
725
726<p>
727Starting with SWIG-1.3, all variable declarations, regardless of any
728use of <tt>const</tt>, are wrapped as global variables. If a
729declaration happens to be declared as <tt>const</tt>, it is wrapped as
730a read-only variable. To tell if a variable is <tt>const</tt> or not,
731you need to look at the right-most occurrence of the <tt>const</tt>
732qualifier (that appears before the variable name). If the right-most
733<tt>const</tt> occurs after all other type modifiers (such as
734pointers), then the variable is <tt>const</tt>. Otherwise, it is not.
735</p>
736
737<p>
738Here are some examples of <tt>const</tt> declarations.
739</p>
740
741<div class="code">
742<pre>
743const char a; // A constant character
744char const b; // A constant character (the same)
745char *const c; // A constant pointer to a character
746const char *const d; // A constant pointer to a constant character
747</pre>
748</div>
749<p>
750Here is an example of a declaration that is not <tt>const</tt>:
751</p>
752
753<div class="code">
754<pre>
755const char *e; // A pointer to a constant character. The pointer
756 // may be modified.
757</pre>
758</div>
759<p>
760In this case, the pointer <tt>e</tt> can change---it's only the value
761being pointed to that is read-only.
762</p>
763
764<p>
765<b>Compatibility Note:</b> One reason for changing SWIG to handle
766<tt>const</tt> declarations as read-only variables is that there are
767many situations where the value of a <tt>const</tt> variable might
768change. For example, a library might export a symbol as
769<tt>const</tt> in its public API to discourage modification, but still
770allow the value to change through some other kind of internal
771mechanism. Furthermore, programmers often overlook the fact that with
772a constant declaration like <tt>char *const</tt>, the underlying data
773being pointed to can be modified--it's only the pointer itself that is
774constant. In an embedded system, a <tt>const</tt> declaration might
775refer to a read-only memory address such as the location of a
776memory-mapped I/O device port (where the value changes, but writing to
777the port is not supported by the hardware). Rather than trying to
778build a bunch of special cases into the <tt>const</tt> qualifier, the
779new interpretation of <tt>const</tt> as "read-only" is simple and
780exactly matches the actual semantics of <tt>const</tt> in C/C++. If
781you really want to create a constant as in older versions of SWIG, use
782the <tt>%constant</tt> directive instead. For example:
783</p>
784
785<div class="code">
786<pre>
787%constant double PI = 3.14159;
788</pre>
789</div>
790
791<p>
792or
793</p>
794
795<div class="code">
796<pre>
797#ifdef SWIG
798#define const %constant
799#endif
800const double foo = 3.4;
801const double bar = 23.4;
802const int spam = 42;
803#ifdef SWIG
804#undef const
805#endif
806...
807
808</pre>
809</div>
810
811<H3><a name="SWIG_nn14"></a>5.2.5 A cautionary tale of <tt>char *</tt></H3>
812
813
814<p>
815Before going any further, there is one bit of caution involving
816<tt>char *</tt> that must now be mentioned. When strings are passed
817from a scripting language to a C <tt>char *</tt>, the pointer usually
818points to string data stored inside the interpreter. It is almost
819always a really bad idea to modify this data. Furthermore, some
820languages may explicitly disallow it. For instance, in Python,
821strings are supposed be immutable. If you violate this, you will probably
822receive a vast amount of wrath when you unleash your module on the world.
823</p>
824
825<p>
826The primary source of problems are functions that might modify string data in place.
827A classic example would be a function like this:
828</p>
829
830<div class="code">
831<pre>
832char *strcat(char *s, const char *t)
833</pre>
834</div>
835
836<p>
837Although SWIG will certainly generate a wrapper for this, its behavior
838will be undefined. In fact, it will probably cause your application
839to crash with a segmentation fault or other memory related problem.
840This is because <tt>s</tt> refers to some internal data in the target
841language---data that you shouldn't be touching.
842</p>
843
844<p>
845The bottom line: don't rely on <tt>char *</tt> for anything other than read-only
846input values. However, it must be noted that you could change the behavior of SWIG
847using <a href="Typemaps.html#Typemaps">typemaps</a>.
848</p>
849
850<H2><a name="SWIG_nn15"></a>5.3 Pointers and complex objects</H2>
851
852
853<p>
854Most C programs manipulate arrays, structures, and other types of objects. This section
855discusses the handling of these datatypes.
856</p>
857
858<H3><a name="SWIG_nn16"></a>5.3.1 Simple pointers</H3>
859
860
861<p>
862Pointers to primitive C datatypes such as </p>
863
864<div class="code"><pre>
865int *
866double ***
867char **
868</pre></div>
869<p>
870are fully supported by SWIG. Rather than trying to convert the data being pointed to into a scripting
871representation, SWIG simply encodes the pointer itself into a
872representation that contains the actual value of the pointer and a type-tag.
873Thus, the SWIG representation of the above
874pointers (in Tcl), might look like this:</p>
875
876<div class="targetlang"><pre>
877_10081012_p_int
878_1008e124_ppp_double
879_f8ac_pp_char
880</pre></div>
881
882<p>
883A NULL pointer is represented by the string "NULL" or the value 0
884encoded with type information.</p>
885
886<p>
887All pointers are treated as opaque objects by SWIG. Thus, a pointer
888may be returned by a function and passed around to other C functions
889as needed. For all practical purposes, the scripting language
890interface works in exactly the same way as you would use the
891pointer in a C program. The only difference is that there is no mechanism for
892dereferencing the pointer since this would require the target language
893to understand the memory layout of the underlying object.
894</p>
895
896<p>
897The scripting language representation of a pointer value should never be
898manipulated directly. Even though the values shown look like hexadecimal
899addresses, the numbers used may differ from the actual machine address (e.g.,
900on little-endian machines, the digits may appear in reverse order).
901Furthermore, SWIG does not
902normally map pointers into high-level objects such as associative
903arrays or lists (for example, converting an
904<tt>int *</tt> into an list of integers). There are several reasons
905why SWIG does not do this:</p>
906
907<ul>
908<li>There is not enough information in a C declaration to properly map
909pointers into higher level constructs. For example, an <tt>int *</tt>
910may indeed be an array of integers, but if it contains ten million
911elements, converting it into a list object is probably a bad idea.
912</li>
913
914<li>The underlying semantics associated with a pointer is not known
915by SWIG. For instance, an <tt>int *</tt> might not be an array at all--perhaps it
916is an output value!
917</li>
918
919<li>By handling all pointers in a consistent manner, the implementation of SWIG is greatly
920simplified and less prone to error.
921</li>
922</ul>
923
924<H3><a name="SWIG_nn17"></a>5.3.2 Run time pointer type checking</H3>
925
926
927<p>
928By allowing pointers to be manipulated from a scripting language, extension modules
929effectively bypass compile-time type checking in the C/C++
930compiler. To prevent errors, a type signature is encoded into all
931pointer values and is used to perform run-time type checking. This
932type-checking process is an integral part of SWIG and can not be
933disabled or modified without using typemaps (described in later
934chapters).
935</p>
936
937<p>
938Like C, <tt>void *</tt> matches any kind of pointer. Furthermore,
939<tt>NULL</tt> pointers can be passed to any function that expects to
940receive a pointer. Although this has the potential to cause a crash,
941<tt>NULL</tt> pointers are also sometimes used
942as sentinel values or to denote a missing/empty value. Therefore,
943SWIG leaves NULL pointer checking up to the application.
944</p>
945
946<H3><a name="SWIG_nn18"></a>5.3.3 Derived types, structs, and classes</H3>
947
948
949<p>
950For everything else (structs, classes, arrays, etc...) SWIG applies a
951very simple rule :</p>
952
953<center>
954<b>Everything else is a pointer</b>
955</center>
956
957<p>
958In other words, SWIG manipulates everything else by reference. This
959model makes sense because most C/C++ programs make heavy use of
960pointers and SWIG can use the type-checked pointer mechanism already
961present for handling pointers to basic datatypes.</p>
962
963<p>
964Although this probably sounds complicated, it's really quite
965simple. Suppose you have an interface file like this :</p>
966
967<div class="code"><pre>
968%module fileio
969FILE *fopen(char *, char *);
970int fclose(FILE *);
971unsigned fread(void *ptr, unsigned size, unsigned nobj, FILE *);
972unsigned fwrite(void *ptr, unsigned size, unsigned nobj, FILE *);
973void *malloc(int nbytes);
974void free(void *);
975
976</pre></div>
977
978<p>
979In this file, SWIG doesn't know what a <tt>FILE</tt> is, but since it's used
980as a pointer, so it doesn't really matter what it is. If you wrapped
981this module into Python, you can use the functions just like you
982expect :</p>
983
984<div class="targetlang"><pre>
985# Copy a file
986def filecopy(source,target):
987 f1 = fopen(source,"r")
988 f2 = fopen(target,"w")
989 buffer = malloc(8192)
990 nbytes = fread(buffer,8192,1,f1)
991 while (nbytes &gt; 0):
992 fwrite(buffer,8192,1,f2)
993 nbytes = fread(buffer,8192,1,f1)
994 free(buffer)
995
996</pre></div>
997
998<p>
999In this case <tt>f1</tt>,<tt> f2</tt>, and <tt>buffer</tt> are all
1000opaque objects containing C pointers. It doesn't matter what value
1001they contain--our program works just fine without this knowledge.</p>
1002
1003<H3><a name="SWIG_nn19"></a>5.3.4 Undefined datatypes</H3>
1004
1005
1006<p>
1007When SWIG encounters an undeclared datatype, it automatically assumes
1008that it is a structure or class. For example, suppose the following
1009function appeared in a SWIG input file:</p>
1010
1011<div class="code"><pre>
1012void matrix_multiply(Matrix *a, Matrix *b, Matrix *c);
1013</pre></div>
1014
1015<p>
1016SWIG has no idea what a "<tt>Matrix</tt>" is. However, it is obviously
1017a pointer to something so SWIG generates a wrapper using its generic pointer
1018handling code.
1019</p>
1020
1021<p>
1022Unlike C or C++, SWIG does not actually care whether <tt>Matrix</tt>
1023has been previously defined in the interface file or not. This
1024allows SWIG to generate interfaces from
1025only partial or limited information. In some cases, you may not care
1026what a <tt>Matrix</tt> really is as long as you can pass an opaque reference to
1027one around in the scripting language interface.
1028</p>
1029
1030<p>
1031An important detail to mention is that SWIG will gladly generate
1032wrappers for an interface when there are unspecified type names.
1033However, <b>all unspecified types are internally handled as pointers
1034to structures or classes!</b> For example, consider the following declaration:
1035</p>
1036
1037<div class="code">
1038<pre>
1039void foo(size_t num);
1040</pre>
1041</div>
1042
1043<p>
1044If <tt>size_t</tt> is undeclared, SWIG generates wrappers
1045that expect to receive a type of <tt>size_t *</tt> (this mapping is described shortly).
1046As a result, the scripting interface might behave strangely. For example:
1047</p>
1048
1049<div class="code">
1050<pre>
1051foo(40);
1052TypeError: expected a _p_size_t.
1053</pre>
1054</div>
1055
1056<p>
1057The only way to fix this problem is to make sure you properly declare type names using
1058<tt>typedef</tt>.
1059</p>
1060
1061<!-- We might want to add an error reporting flag to swig -->
1062
1063<H3><a name="SWIG_nn20"></a>5.3.5 Typedef</H3>
1064
1065
1066<p>
1067Like C, <tt>typedef</tt> can be used to define new type names in SWIG. For example:
1068</p>
1069
1070<div class="code"><pre>
1071typedef unsigned int size_t;
1072</pre></div>
1073
1074<p>
1075<tt>typedef</tt> definitions appearing in a SWIG interface
1076are not propagated to the generated wrapper code. Therefore, they
1077either need to be defined in an included header file or placed in the
1078declarations section like this:
1079</p>
1080
1081<div class="code">
1082<pre>
1083%{
1084/* Include in the generated wrapper file */
1085typedef unsigned int size_t;
1086%}
1087/* Tell SWIG about it */
1088typedef unsigned int size_t;
1089</pre>
1090</div>
1091
1092<p>
1093or
1094</p>
1095
1096<div class="code">
1097<pre>
1098%inline %{
1099typedef unsigned int size_t;
1100%}
1101</pre>
1102</div>
1103
1104<p>
1105In certain cases, you might be able to include other header files to collect type information.
1106For example:
1107</p>
1108
1109<div class="code">
1110<pre>
1111%module example
1112%import "sys/types.h"
1113</pre>
1114</div>
1115
1116<p>
1117In this case, you might run SWIG as follows:
1118</p>
1119
1120<div class="shell">
1121<pre>
1122$ swig -I/usr/include -includeall example.i
1123</pre>
1124</div>
1125
1126<p>
1127It should be noted that your mileage will vary greatly here.
1128System headers are notoriously complicated and may rely upon a variety
1129of non-standard C coding extensions (e.g., such as special directives
1130to GCC). Unless you exactly specify the right include directories and
1131preprocessor symbols, this may not work correctly (you will have to
1132experiment).
1133</p>
1134
1135<p>
1136SWIG tracks <tt>typedef</tt> declarations and uses this information
1137for run-time type checking. For instance, if you use the above <tt>typedef</tt> and
1138had the following function declaration:
1139</p>
1140
1141<div class="code">
1142<pre>
1143void foo(unsigned int *ptr);
1144</pre>
1145</div>
1146
1147<p>
1148The corresponding wrapper function will accept arguments of
1149type <tt>unsigned int *</tt> or <tt>size_t *</tt>.
1150</p>
1151
1152<H2><a name="SWIG_nn21"></a>5.4 Other Practicalities</H2>
1153
1154
1155<p>
1156So far, this chapter has presented almost everything you need to know to use SWIG
1157for simple interfaces. However, some C programs use idioms that are somewhat
1158more difficult to map to a scripting language interface. This section describes
1159some of these issues.
1160</p>
1161
1162<H3><a name="SWIG_nn22"></a>5.4.1 Passing structures by value</H3>
1163
1164
1165<p>
1166Sometimes a C function takes structure parameters that are passed
1167by value. For example, consider the following function:
1168</p>
1169
1170<div class="code"><pre>
1171double dot_product(Vector a, Vector b);
1172</pre></div>
1173
1174<p>
1175To deal with this, SWIG transforms the function to use pointers by
1176creating a wrapper equivalent to the following:
1177</p>
1178
1179<div class="code"><pre>
1180double wrap_dot_product(Vector *a, Vector *b) {
1181 Vector x = *a;
1182 Vector y = *b;
1183 return dot_product(x,y);
1184}
1185</pre></div>
1186
1187<p>
1188In the target language, the <tt>dot_product()</tt> function now accepts pointers
1189to Vectors instead of Vectors. For the most part, this transformation
1190is transparent so you might not notice.
1191</p>
1192
1193<H3><a name="SWIG_nn23"></a>5.4.2 Return by value</H3>
1194
1195
1196<p>
1197C functions that return structures or classes datatypes by value are more difficult
1198to handle. Consider the following function:</p>
1199
1200<div class="code"><pre>
1201Vector cross_product(Vector v1, Vector v2);
1202</pre></div>
1203
1204<p>
1205This function wants to return <tt>Vector</tt>, but SWIG only really supports
1206pointers. As a result, SWIG creates a wrapper like this:
1207</p>
1208
1209<div class="code"><pre>
1210Vector *wrap_cross_product(Vector *v1, Vector *v2) {
1211 Vector x = *v1;
1212 Vector y = *v2;
1213 Vector *result;
1214 result = (Vector *) malloc(sizeof(Vector));
1215 *(result) = cross(x,y);
1216 return result;
1217}
1218</pre></div>
1219
1220<p>
1221or if SWIG was run with the <tt>-c++</tt> option:</p>
1222
1223<div class="code"><pre>
1224Vector *wrap_cross(Vector *v1, Vector *v2) {
1225 Vector x = *v1;
1226 Vector y = *v2;
1227 Vector *result = new Vector(cross(x,y)); // Uses default copy constructor
1228 return result;
1229}
1230</pre></div>
1231
1232<p>
1233In both cases, SWIG allocates a new object and returns a reference to it. It
1234is up to the user to delete the returned object when it is no longer
1235in use. Clearly, this will leak memory if you are unaware of the implicit
1236memory allocation and don't take steps to free the result. That said, it should be
1237noted that some language modules can now automatically track newly created objects and
1238reclaim memory for you. Consult the documentation for each language module for more details.
1239</p>
1240
1241<p>
1242It should also be noted that the handling of pass/return by value in
1243C++ has some special cases. For example, the above code fragments
1244don't work correctly if <tt>Vector</tt> doesn't define a default
1245constructor. The section on SWIG and C++ has more information about this case.
1246</p>
1247
1248<H3><a name="SWIG_nn24"></a>5.4.3 Linking to structure variables</H3>
1249
1250
1251<p>
1252When global variables or class members involving structures are
1253encountered, SWIG handles them as pointers. For example, a global
1254variable like this</p>
1255
1256<div class="code"><pre>
1257Vector unit_i;
1258</pre></div>
1259
1260<p>
1261gets mapped to an underlying pair of set/get functions like this :</p>
1262
1263<div class="code"><pre>
1264Vector *unit_i_get() {
1265 return &amp;unit_i;
1266}
1267void unit_i_set(Vector *value) {
1268 unit_i = *value;
1269}
1270</pre></div>
1271
1272<p>
1273Again some caution is in order. A global variable created in this
1274manner will show up as a pointer in the target scripting language. It
1275would be an extremely bad idea to free or destroy such a pointer. Also,
1276C++ classes must supply a properly defined copy constructor in order for
1277assignment to work correctly.
1278</p>
1279
1280<H3><a name="SWIG_nn25"></a>5.4.4 Linking to <tt>char *</tt></H3>
1281
1282
1283<p>
1284When a global variable of type <tt>char *</tt> appears, SWIG uses <tt>malloc()</tt> or
1285<tt>new</tt> to allocate memory for the new value. Specifically, if you have a variable
1286like this
1287</p>
1288
1289<div class="code">
1290<pre>
1291char *foo;
1292</pre>
1293</div>
1294
1295<p>
1296SWIG generates the following code:
1297</p>
1298
1299<div class="code">
1300<pre>
1301/* C mode */
1302void foo_set(char *value) {
1303 if (foo) free(foo);
1304 foo = (char *) malloc(strlen(value)+1);
1305 strcpy(foo,value);
1306}
1307
1308/* C++ mode. When -c++ option is used */
1309void foo_set(char *value) {
1310 if (foo) delete [] foo;
1311 foo = new char[strlen(value)+1];
1312 strcpy(foo,value);
1313}
1314</pre>
1315</div>
1316
1317<p>
1318If this is not the behavior that you want, consider making the variable read-only using the
1319<tt>%immutable</tt> directive. Alternatively, you might write a short assist-function to set the value
1320exactly like you want. For example:
1321</p>
1322
1323<div class="code">
1324<pre>
1325%inline %{
1326 void set_foo(char *value) {
1327 strncpy(foo,value, 50);
1328 }
1329%}
1330</pre>
1331</div>
1332
1333<p>
1334Note: If you write an assist function like this, you will have to call
1335it as a function from the target scripting language (it does not work
1336like a variable). For example, in Python you will have to write:
1337</p>
1338
1339<div class="targetlang">
1340<pre>
1341&gt;&gt;&gt; set_foo("Hello World")
1342</pre>
1343</div>
1344
1345<p>
1346A common mistake with <tt>char *</tt> variables is to link to a variable declared like this:
1347</p>
1348
1349<div class="code">
1350<pre>
1351char *VERSION = "1.0";
1352</pre>
1353</div>
1354
1355<p>
1356In this case, the variable will be readable, but any attempt to change
1357the value results in a segmentation or general protection fault. This
1358is due to the fact that SWIG is trying to release the old value using
1359<tt>free</tt> or <tt>delete</tt> when the string literal value currently assigned to the variable wasn't
1360allocated using <tt>malloc()</tt> or <tt>new</tt>.
1361To fix this behavior, you can
1362either mark the variable as read-only, write a typemap (as described in Chapter 6),
1363or write a special set function as shown. Another alternative is to declare the
1364variable as an array:
1365</p>
1366
1367<div class="code">
1368<pre>
1369char VERSION[64] = "1.0";
1370</pre>
1371</div>
1372
1373<p>
1374When variables of type <tt>const char *</tt> are declared, SWIG still generates functions for setting and
1375getting the value. However, the default behavior does <em>not</em> release the previous contents (resulting in
1376a possible memory leak). In fact, you may get a warning message such as this when wrapping such a variable:
1377</p>
1378
1379<div class="shell">
1380<pre>
1381example.i:20. Typemap warning. Setting const char * variable may leak memory
1382</pre>
1383</div>
1384
1385<p>
1386The reason for this behavior is that <tt>const char *</tt> variables are often used to point to string literals.
1387For example:
1388</p>
1389
1390<div class="code">
1391<pre>
1392const char *foo = "Hello World\n";
1393</pre>
1394</div>
1395
1396<p>
1397Therefore, it's a really bad idea to call <tt>free()</tt> on such a
1398pointer. On the other hand, it <em>is</em> legal to change the
1399pointer to point to some other value. When setting a variable of this
1400type, SWIG allocates a new string (using malloc or new) and changes
1401the pointer to point to the new value. However, repeated
1402modifications of the value will result in a memory leak since the old
1403value is not released.
1404</p>
1405
1406
1407
1408
1409<H3><a name="SWIG_nn26"></a>5.4.5 Arrays</H3>
1410
1411
1412<p>
1413Arrays are fully supported by SWIG, but they are always handled as pointers instead
1414of mapping them to a special array object or list in the target language. Thus, the
1415following declarations :</p>
1416
1417<div class="code"><pre>
1418int foobar(int a[40]);
1419void grok(char *argv[]);
1420void transpose(double a[20][20]);
1421</pre></div>
1422
1423<p>
1424are processed as if they were really declared like this:
1425</p>
1426
1427<div class="code"><pre>
1428int foobar(int *a);
1429void grok(char **argv);
1430void transpose(double (*a)[20]);
1431</pre></div>
1432
1433<p>
1434Like C, SWIG does not perform array bounds checking.
1435It is up to the
1436user to make sure the pointer points a suitably allocated region of memory.
1437</p>
1438
1439<p>
1440Multi-dimensional arrays are transformed into a pointer to an array of one less
1441dimension. For example:
1442</p>
1443
1444<div class="code">
1445<pre>
1446int [10]; // Maps to int *
1447int [10][20]; // Maps to int (*)[20]
1448int [10][20][30]; // Maps to int (*)[20][30]
1449</pre>
1450</div>
1451
1452<p>
1453It is important to note that in the C type system, a multidimensional
1454array <tt>a[][]</tt> is <b>NOT</b> equivalent to a single pointer
1455<tt>*a</tt> or a double pointer such as <tt>**a</tt>. Instead, a
1456pointer to an array is used (as shown above) where the actual value of
1457the pointer is the starting memory location of the array. The
1458reader is strongly advised to dust off their C book and re-read the
1459section on arrays before using them with SWIG.
1460</p>
1461
1462<p>
1463Array variables are supported, but are read-only by default. For example:
1464</p>
1465
1466<div class="code">
1467<pre>
1468int a[100][200];
1469</pre>
1470</div>
1471
1472<p>
1473In this case, reading the variable 'a' returns a pointer of type <tt>int (*)[200]</tt>
1474that points to the first element of the array <tt>&amp;a[0][0]</tt>. Trying to modify 'a' results
1475in an error. This is because SWIG does not know how to copy data from the target
1476language into the array. To work around this limitation, you may want to write
1477a few simple assist functions like this:
1478</p>
1479
1480<div class="code">
1481<pre>
1482%inline %{
1483void a_set(int i, int j, int val) {
1484 a[i][j] = val;
1485}
1486int a_get(int i, int j) {
1487 return a[i][j];
1488}
1489%}
1490</pre>
1491</div>
1492
1493<p>
1494To dynamically create arrays of various sizes and shapes, it may be useful to write
1495some helper functions in your interface. For example:
1496</p>
1497
1498<div class="code">
1499<pre>
1500// Some array helpers
1501%inline %{
1502 /* Create any sort of [size] array */
1503 int *int_array(int size) {
1504 return (int *) malloc(size*sizeof(int));
1505 }
1506 /* Create a two-dimension array [size][10] */
1507 int (*int_array_10(int size))[10] {
1508 return (int (*)[10]) malloc(size*10*sizeof(int));
1509 }
1510%}
1511</pre>
1512</div>
1513
1514<p>
1515Arrays of <tt>char</tt> are handled as a special case by SWIG. In this case, strings in the
1516target language can be stored in the array. For example, if you have a declaration like this,
1517</p>
1518
1519<div class="code">
1520<pre>
1521char pathname[256];
1522</pre>
1523</div>
1524
1525<p>
1526SWIG generates functions for both getting and setting the value that are equivalent to the following
1527code:
1528</p>
1529
1530<div class="code">
1531<pre>
1532char *pathname_get() {
1533 return pathname;
1534}
1535void pathname_set(char *value) {
1536 strncpy(pathname,value,256);
1537}
1538</pre>
1539</div>
1540
1541<p>
1542In the target language, the value can be set like a normal variable.
1543</p>
1544
1545<H3><a name="SWIG_readonly_variables"></a>5.4.6 Creating read-only variables</H3>
1546
1547
1548<p>
1549A read-only variable can be created by using the <tt>%immutable</tt>
1550directive as shown :</p>
1551
1552<div class="code"><pre>
1553// File : interface.i
1554
1555int a; // Can read/write
1556%immutable;
1557int b,c,d // Read only variables
1558%mutable;
1559double x,y // read/write
1560</pre></div>
1561
1562<p>
1563The <tt>%immutable</tt> directive enables read-only mode until it is
1564explicitly disabled using the <tt>%mutable</tt> directive. As an alternative to turning
1565read-only mode off and on like this, individual declarations can also be tagged as
1566immutable. For example:
1567</p>
1568
1569<div class="code"><pre>
1570%immutable x; // Make x read-only
1571...
1572double x; // Read-only (from earlier %immutable directive)
1573double y; // Read-write
1574...
1575</pre></div>
1576
1577<p>
1578The <tt>%mutable</tt> and <tt>%immutable</tt> directives are actually
1579<a href="Customization.html#features">%feature directives</a> defined like this:
1580</p>
1581
1582<div class="code"><pre>
1583#define %immutable %feature("immutable")
1584#define %mutable %feature("immutable","")
1585</pre></div>
1586
1587<p>
1588If you wanted to make all wrapped variables read-only, barring one or two, it might be easier to take this approach:
1589</p>
1590
1591<div class="code"><pre>
1592%immutable; // Make all variables read-only
1593%feature("immutable","0") x; // except, make x read/write
1594...
1595double x;
1596double y;
1597double z;
1598...
1599</pre></div>
1600
1601<p>
1602Read-only variables are also created when declarations are declared as <tt>const</tt>.
1603For example:
1604</p>
1605
1606<div class="code">
1607<pre>
1608const int foo; /* Read only variable */
1609char * const version="1.0"; /* Read only variable */
1610</pre></div>
1611
1612<p>
1613<b>Compatibility note:</b> Read-only access used to be controlled by a pair of directives
1614<tt>%readonly</tt> and <tt>%readwrite</tt>. Although these directives still work, they
1615generate a warning message. Simply change the directives to <tt>%immutable;</tt> and
1616<tt>%mutable;</tt> to silence the warning. Don't forget the extra semicolon!
1617</p>
1618
1619<H3><a name="SWIG_nn28"></a>5.4.7 Renaming and ignoring declarations</H3>
1620
1621
1622<p>
1623Normally, the name of a C declaration is used when that declaration is
1624wrapped into the target language. However, this may generate a
1625conflict with a keyword or already existing function in the scripting
1626language. To resolve a name conflict, you can use the <tt>%rename</tt>
1627directive as shown :</p>
1628
1629<div class="code"><pre>
1630// interface.i
1631
1632%rename(my_print) print;
1633extern void print(char *);
1634
1635%rename(foo) a_really_long_and_annoying_name;
1636extern int a_really_long_and_annoying_name;
1637
1638</pre></div>
1639
1640<p>
1641SWIG still calls the correct C function, but in this case the
1642function <tt>print()</tt> will really be called "<tt>my_print()</tt>"
1643in the target language. </p>
1644
1645<p>
1646The placement of the <tt>%rename</tt> directive is arbitrary as long as it appears
1647before the declarations to be renamed. A common technique is to write code for
1648wrapping a header file like this:
1649</p>
1650
1651<div class="code"><pre>
1652// interface.i
1653
1654%rename(my_print) print;
1655%rename(foo) a_really_long_and_annoying_name;
1656
1657%include "header.h"
1658</pre></div>
1659
1660<p>
1661<tt>%rename </tt>applies a renaming operation to all future
1662occurrences of a name. The renaming applies to functions, variables,
1663class and structure names, member functions, and member data. For
1664example, if you had two-dozen C++ classes, all with a member function
1665named `print' (which is a keyword in Python), you could rename them
1666all to `output' by specifying :</p>
1667
1668<div class="code"><pre>
1669%rename(output) print; // Rename all `print' functions to `output'
1670</pre></div>
1671
1672<p>
1673SWIG does not normally perform any checks to see if the functions it wraps are
1674already defined in the target scripting language. However, if you are
1675careful about namespaces and your use of modules, you can usually
1676avoid these problems.</p>
1677
1678<p>
1679Closely related to <tt>%rename</tt> is the <tt>%ignore</tt> directive. <tt>%ignore</tt> instructs SWIG
1680to ignore declarations that match a given identifier. For example:
1681</p>
1682
1683<div class="code">
1684<pre>
1685%ignore print; // Ignore all declarations named print
1686%ignore _HAVE_FOO_H; // Ignore an include guard constant
1687...
1688%include "foo.h" // Grab a header file
1689...
1690</pre>
1691</div>
1692
1693<p>
1694One use of <tt>%ignore</tt> is to selectively remove certain declarations from a header file without having
1695to add conditional compilation to the header. However, it should be stressed that this only works for simple
1696declarations. If you need to remove a whole section of problematic code, the SWIG preprocessor should be used instead.
1697</p>
1698
1699<p>
1700More powerful variants of <tt>%rename</tt> and <tt>%ignore</tt> directives can be used to help
1701wrap C++ overloaded functions and methods or C++ methods which use default arguments. This is described in the
1702<a href="SWIGPlus.html#ambiguity_resolution_renaming">Ambiguity resolution and renaming</a> section in the C++ chapter.
1703</p>
1704
1705<p>
1706<b>Compatibility note: </b> Older versions of SWIG provided a special <tt>%name</tt> directive for renaming declarations.
1707For example:
1708</p>
1709
1710<div class="code">
1711<pre>
1712%name(output) extern void print(char *);
1713</pre>
1714</div>
1715
1716<p>
1717This directive is still supported, but it is deprecated and should probably be avoided. The <tt>%rename</tt>
1718directive is more powerful and better supports wrapping of raw header file information.
1719</p>
1720
1721<H3><a name="SWIG_default_args"></a>5.4.8 Default/optional arguments</H3>
1722
1723
1724<p>
1725SWIG supports default arguments in both C and C++ code. For example:
1726</p>
1727
1728<div class="code"><pre>
1729int plot(double x, double y, int color=WHITE);
1730</pre></div>
1731
1732<p>
1733In this case, SWIG generates wrapper code where the
1734default arguments are optional in the target language. For example, this function could be
1735used in Tcl as follows :</p>
1736
1737<div class="targetlang"><pre>
1738% plot -3.4 7.5 # Use default value
1739% plot -3.4 7.5 10 # set color to 10 instead
1740
1741</pre></div>
1742
1743<p>
1744Although the ANSI C standard does not allow default arguments, default
1745arguments specified in a SWIG interface work with both C and C++.
1746</p>
1747
1748<p>
1749<b>Note:</b> There is a subtle semantic issue concerning the use
1750of default arguments and the SWIG generated wrapper code. When default
1751arguments are used in C code, the default values are emitted into the wrappers and the
1752function is invoked with a full set of arguments. This is different to when wrapping C++
1753where an overloaded wrapper method is generated for each defaulted argument.
1754Please refer to the section on <a href="SWIGPlus.html#SWIGPlus_default_args">default arguments</a>
1755in the C++ chapter for further details.
1756</p>
1757
1758<H3><a name="SWIG_nn30"></a>5.4.9 Pointers to functions and callbacks</H3>
1759
1760
1761<p>
1762Occasionally, a C library may include functions that expect to receive
1763pointers to functions--possibly to serve as callbacks. SWIG
1764provides full support for function pointers provided that the callback
1765functions are defined in C and not in the target language. For example,
1766consider a function like this:
1767</p>
1768
1769<div class="code"><pre>
1770int binary_op(int a, int b, int (*op)(int,int));
1771</pre></div>
1772
1773<p>
1774When you first wrap something like this into an extension module, you
1775may find the function to be impossible to use. For instance, in Python:
1776</p>
1777
1778<div class="targetlang"><pre>
1779&gt;&gt;&gt; def add(x,y):
1780... return x+y
1781...
1782&gt;&gt;&gt; binary_op(3,4,add)
1783Traceback (most recent call last):
1784 File "&lt;stdin&gt;", line 1, in ?
1785TypeError: Type error. Expected _p_f_int_int__int
1786&gt;&gt;&gt;
1787</pre>
1788</div>
1789
1790<p>
1791The reason for this error is that SWIG doesn't know how to map a scripting
1792language function into a C callback. However, existing C functions can
1793be used as arguments provided you install them as constants.
1794One way to do this is to use the <tt>%constant</tt> directive like this:
1795</p>
1796
1797<div class="code"><pre>
1798/* Function with a callback */
1799int binary_op(int a, int b, int (*op)(int,int));
1800
1801/* Some callback functions */
1802%constant int add(int,int);
1803%constant int sub(int,int);
1804%constant int mul(int,int);
1805</pre></div>
1806
1807<p>
1808In this case, <tt>add</tt>, <tt>sub</tt>, and <tt>mul</tt> become function pointer
1809constants in the target scripting language. This allows you to use them as follows:
1810</p>
1811
1812<div class="targetlang">
1813<pre>
1814&gt;&gt;&gt; binary_op(3,4,add)
18157
1816&gt;&gt;&gt; binary_op(3,4,mul)
181712
1818&gt;&gt;&gt;
1819</pre>
1820</div>
1821
1822<p>
1823Unfortunately, by declaring the callback functions as constants, they are no longer accesible
1824as functions. For example:
1825</p>
1826
1827<div class="targetlang">
1828<pre>
1829&gt;&gt;&gt; add(3,4)
1830Traceback (most recent call last):
1831 File "&lt;stdin&gt;", line 1, in ?
1832TypeError: object is not callable: '_ff020efc_p_f_int_int__int'
1833&gt;&gt;&gt;
1834</pre>
1835</div>
1836
1837<p>
1838If you want to make a function available as both a callback function and a function, you
1839can use the <tt>%callback</tt> and <tt>%nocallback</tt> directives like this:
1840</p>
1841
1842<div class="code">
1843<pre>
1844/* Function with a callback */
1845int binary_op(int a, int b, int (*op)(int,int));
1846
1847/* Some callback functions */
1848%callback("%s_cb")
1849int add(int,int);
1850int sub(int,int);
1851int mul(int,int);
1852%nocallback
1853</pre></div>
1854
1855<p>
1856The argument to <tt>%callback</tt> is a printf-style format string that
1857specifies the naming convention for the callback constants (<tt>%s</tt> gets replaced
1858by the function name). The callback mode remains in effect until it is explicitly
1859disabled using <tt>%nocallback</tt>. When you do this, the interface now works as follows:
1860</p>
1861
1862<div class="targetlang">
1863<pre>
1864&gt;&gt;&gt; binary_op(3,4,add_cb)
18657
1866&gt;&gt;&gt; binary_op(3,4,mul_cb)
186712
1868&gt;&gt;&gt; add(3,4)
18697
1870&gt;&gt;&gt; mul(3,4)
187112
1872</pre>
1873</div>
1874
1875<p>
1876Notice that when the function is used as a callback, special names
1877such as <tt>add_cb</tt> is used instead. To call the function
1878normally, just use the original function name such as <tt>add()</tt>.
1879</p>
1880
1881<p>
1882SWIG provides a number of extensions to standard C printf formatting
1883that may be useful in this context. For instance, the following
1884variation installs the callbacks as all upper-case constants such as
1885<tt>ADD</tt>, <tt>SUB</tt>, and <tt>MUL</tt>:
1886</p>
1887
1888<div class="code"><pre>
1889/* Some callback functions */
1890%callback("%(upper)s")
1891int add(int,int);
1892int sub(int,int);
1893int mul(int,int);
1894%nocallback
1895</pre></div>
1896
1897<p>
1898A format string of <tt>"%(lower)s"</tt> converts all characters to lower-case.
1899A string of <tt>"%(title)s"</tt> capitalizes the first character and converts the
1900rest to lower case.
1901</p>
1902
1903<p>
1904And now, a final note about function pointer support. Although SWIG
1905does not normally allow callback functions to be written in the target language, this
1906can be accomplished with the use of typemaps and other advanced SWIG features.
1907This is described in a later chapter.
1908</p>
1909
1910<H2><a name="SWIG_nn31"></a>5.5 Structures and unions</H2>
1911
1912
1913<p>
1914This section describes the behavior of SWIG when processing ANSI C structures and union declarations. Extensions to
1915handle C++ are described in the next section.
1916</p>
1917
1918<p>
1919If SWIG encounters the definition of a structure or union, it
1920creates a set of accessor functions. Although SWIG does not need
1921structure definitions to build an interface, providing definitions
1922make it possible to access structure members. The accessor functions
1923generated by SWIG simply take a pointer to an object and allow access
1924to an individual member. For example, the declaration :</p>
1925
1926<div class="code"><pre>
1927struct Vector {
1928 double x,y,z;
1929}
1930
1931</pre></div>
1932
1933<p>
1934gets transformed into the following set of accessor functions :</p>
1935
1936<div class="code"><pre>
1937double Vector_x_get(struct Vector *obj) {
1938 return obj-&gt;x;
1939}
1940double Vector_y_get(struct Vector *obj) {
1941 return obj-&gt;y;
1942}
1943double Vector_z_get(struct Vector *obj) {
1944 return obj-&gt;z;
1945}
1946void Vector_x_set(struct Vector *obj, double value) {
1947 obj-&gt;x = value;
1948}
1949void Vector_y_set(struct Vector *obj, double value) {
1950 obj-&gt;y = value;
1951}
1952void Vector_z_set(struct Vector *obj, double value) {
1953 obj-&gt;z = value;
1954}
1955</pre></div>
1956
1957<p>
1958In addition, SWIG creates default constructor and destructor functions if none are
1959defined in the interface. For example:
1960</p>
1961
1962<div class="code"><pre>
1963struct Vector *new_Vector() {
1964 return (Vector *) calloc(1,sizeof(struct Vector));
1965}
1966void delete_Vector(struct Vector *obj) {
1967 free(obj);
1968}
1969</pre>
1970</div>
1971
1972<p>
1973Using these low-level accessor functions, an object can be minimally manipulated from the target
1974language using code like this:
1975</p>
1976
1977<div class="code">
1978<pre>
1979v = new_Vector()
1980Vector_x_set(v,2)
1981Vector_y_set(v,10)
1982Vector_z_set(v,-5)
1983...
1984delete_Vector(v)
1985</pre>
1986</div>
1987
1988<p>
1989However, most of SWIG's language modules also provide a high-level interface that is more convenient. Keep reading.
1990</p>
1991
1992<H3><a name="SWIG_nn32"></a>5.5.1 Typedef and structures</H3>
1993
1994
1995<p>
1996SWIG supports the following construct which is quite common in C
1997programs :</p>
1998
1999<div class="code"><pre>
2000typedef struct {
2001 double x,y,z;
2002} Vector;
2003
2004</pre></div>
2005
2006<p>
2007When encountered, SWIG assumes that the name of the object is `Vector'
2008and creates accessor functions like before. The only difference is
2009that the use of <tt>typedef</tt> allows SWIG to drop the
2010<tt>struct</tt> keyword on its generated code. For example:
2011</p>
2012
2013<div class="code">
2014<pre>
2015double Vector_x_get(Vector *obj) {
2016 return obj-&gt;x;
2017}
2018</pre>
2019</div>
2020
2021<p>
2022If two different names are used like this :</p>
2023
2024<div class="code"><pre>
2025typedef struct vector_struct {
2026 double x,y,z;
2027} Vector;
2028
2029</pre></div>
2030
2031<p>
2032the name <tt>Vector</tt> is used instead of <tt>vector_struct</tt> since
2033this is more typical C programming style. If declarations defined later in the interface use the type <tt>struct
2034vector_struct</tt>, SWIG knows that this is the same as
2035<tt>Vector</tt> and it generates the appropriate type-checking code.
2036</p>
2037
2038<H3><a name="SWIG_nn33"></a>5.5.2 Character strings and structures</H3>
2039
2040
2041<p>
2042Structures involving character strings require some care. SWIG assumes
2043that all members of type <tt>char *</tt> have been dynamically
2044allocated using <tt>malloc()</tt> and that they are NULL-terminated
2045ASCII strings. When such a member is modified, the previously contents
2046will be released, and the new contents allocated. For example :</p>
2047
2048<div class="code"><pre>
2049%module mymodule
2050...
2051struct Foo {
2052 char *name;
2053 ...
2054}
2055
2056</pre></div>
2057
2058<p>
2059This results in the following accessor functions :</p>
2060
2061<div class="code"><pre>
2062char *Foo_name_get(Foo *obj) {
2063 return Foo-&gt;name;
2064}
2065
2066char *Foo_name_set(Foo *obj, char *c) {
2067 if (obj-&gt;name) free(obj-&gt;name);
2068 obj-&gt;name = (char *) malloc(strlen(c)+1);
2069 strcpy(obj-&gt;name,c);
2070 return obj-&gt;name;
2071}
2072</pre></div>
2073
2074<p>
2075If this behavior differs from what you need in your applications,
2076the SWIG "memberin" typemap can be used to change it. See the
2077typemaps chapter for further details.
2078</p>
2079
2080<p>
2081Note: If the <tt>-c++</tt> option is used, <tt>new</tt> and <tt>delete</tt> are used to
2082perform memory allocation.
2083</p>
2084
2085<H3><a name="SWIG_nn34"></a>5.5.3 Array members</H3>
2086
2087
2088<p>
2089Arrays may appear as the members of structures, but they will be
2090read-only. SWIG will write an accessor function that returns the
2091pointer to the first element of the array, but will not write a
2092function to change the contents of the array itself.
2093When this
2094situation is detected, SWIG may generate a warning message such as the
2095following :</p>
2096
2097<div class="shell"><pre>
2098interface.i:116. Warning. Array member will be read-only
2099</pre></div>
2100
2101<p>
2102To eliminate the warning message, typemaps can be used, but this is
2103discussed in a later chapter. In many cases, the warning message is
2104harmless.
2105</p>
2106
2107<H3><a name="SWIG_nn35"></a>5.5.4 Structure data members</H3>
2108
2109
2110<p>
2111Occasionally, a structure will contain data members that are themselves structures. For example:
2112</p>
2113
2114<div class="code">
2115<pre>
2116typedef struct Foo {
2117 int x;
2118} Foo;
2119
2120typedef struct Bar {
2121 int y;
2122 Foo f; /* struct member */
2123} Bar;
2124</pre>
2125</div>
2126
2127<p>
2128When a structure member is wrapped, it is always handled as a pointer.
2129For example:
2130</p>
2131
2132<div class="code">
2133<pre>
2134Foo *Bar_f_get(Bar *b) {
2135 return &amp;b-&gt;f;
2136}
2137void Bar_f_set(Bar *b, Foo *value) {
2138 b-&gt;f = *value;
2139}
2140</pre>
2141</div>
2142
2143<p>
2144The reasons for this are somewhat subtle but have to do with the
2145problem of modifying and accessing data inside the data member. For
2146example, suppose you wanted to modify the value of <tt>f.x</tt>
2147of a <tt>Bar</tt> object like this:
2148</p>
2149
2150<div class="code">
2151<pre>
2152Bar *b;
2153b-&gt;f.x = 37;
2154</pre>
2155</div>
2156
2157<p>
2158Translating this assignment to function calls (as would be used inside the scripting
2159language interface) results in the following code:
2160</p>
2161
2162<div class="code">
2163<pre>
2164Bar *b;
2165Foo_x_set(Bar_f_get(b),37);
2166</pre>
2167</div>
2168
2169<p>
2170In this code, if the <tt>Bar_f_get()</tt> function were to return a <tt>Foo</tt> instead of a
2171<tt>Foo *</tt>, then the resulting modification would be applied to a <em>copy</em> of <tt>f</tt> and not
2172the data member <tt>f</tt> itself. Clearly that's not what you want!
2173</p>
2174
2175<p>
2176It should be noted that this transformation to pointers only occurs if SWIG knows that a data member
2177is a structure or class. For instance, if you had a structure like this,
2178</p>
2179
2180<div class="code">
2181<pre>
2182struct Foo {
2183 WORD w;
2184};
2185</pre>
2186</div>
2187
2188<p>
2189and nothing was known about <tt>WORD</tt>, then SWIG will generate more normal accessor functions
2190like this:
2191</p>
2192
2193<div class="code">
2194<pre>
2195WORD Foo_w_get(Foo *f) {
2196 return f-&gt;w;
2197}
2198void Foo_w_set(FOO *f, WORD value) {
2199 f-&gt;w = value;
2200}
2201</pre>
2202</div>
2203
2204<p>
2205<b>Compatibility Note: </b> SWIG-1.3.11 and earlier releases transformed all non-primitive member datatypes
2206to pointers. Starting in SWIG-1.3.12, this transformation <em>only</em> occurs if a datatype is known to be a structure,
2207class, or union. This is unlikely to break existing code. However, if you need to tell SWIG that an undeclared
2208datatype is really a struct, simply use a forward struct declaration such as <tt>"struct Foo;"</tt>.
2209</p>
2210
2211<H3><a name="SWIG_nn36"></a>5.5.5 C constructors and destructors </H3>
2212
2213
2214<p>
2215When wrapping structures, it is generally useful to have a mechanism
2216for creating and destroying objects. If you don't do anything, SWIG
2217will automatically generate functions for creating and destroying
2218objects using <tt>malloc()</tt> and <tt>free()</tt>. Note: the use of
2219<tt>malloc()</tt> only applies when SWIG is used on C code (i.e., when the
2220<tt>-c++</tt> option is <em>not</em> supplied on the command line). C++ is handled
2221differently.
2222</p>
2223
2224<p>
2225If you don't want SWIG to generate constructors and destructors, you
2226can use the <tt>%nodefault</tt> directive or the <tt>-no_default</tt>
2227command line option. For example:
2228</p>
2229
2230<div class="shell"><pre>
2231swig -no_default example.i
2232</pre></div>
2233
2234<p>
2235or
2236</p>
2237
2238<div class="code"><pre>
2239%module foo
2240...
2241%nodefault; // Don't create default constructors/destructors
2242... declarations ...
2243%makedefault; // Reenable default constructors/destructors
2244</pre></div>
2245
2246<p>
2247If you need more precise control, <tt>%nodefault</tt> can selectively target individual structure
2248definitions. For example:
2249</p>
2250
2251<div class="code">
2252<pre>
2253%nodefault Foo; // No default constructor/destructors for Foo
2254...
2255struct Foo { // No default generated.
2256};
2257
2258struct Bar { // Default constructor/destructor generated.
2259};
2260</pre>
2261</div>
2262
2263<p>
2264<b>Compatibility note:</b> Prior to SWIG-1.3.7, SWIG did not generate default constructors
2265or destructors unless you explicitly turned them on using <tt>-make_default</tt>.
2266However, it appears that most users want to have constructor and destructor functions so it
2267has now been enabled as the default behavior.
2268</p>
2269
2270<H3><a name="SWIG_adding_member_functions"></a>5.5.6 Adding member functions to C structures</H3>
2271
2272
2273<p>
2274Most languages provide a mechanism for creating classes and
2275supporting object oriented programming. From a C standpoint, object
2276oriented programming really just boils down to the process of
2277attaching functions to structures. These functions normally operate
2278on an instance of the structure (or object). Although there is a
2279natural mapping of C++ to such a scheme, there is no direct mechanism
2280for utilizing it with C code. However, SWIG provides a special
2281<tt>%extend</tt> directive that makes it possible to attach
2282methods to C structures for purposes of building an object oriented
2283interface. Suppose you have a C header file with
2284the following declaration :</p>
2285
2286<div class="code"><pre>
2287/* file : vector.h */
2288...
2289typedef struct {
2290 double x,y,z;
2291} Vector;
2292
2293</pre></div>
2294
2295<p>
2296You can make a <tt>Vector</tt> look alot like a class by writing a SWIG interface like this:
2297</p>
2298
2299<div class="code"><pre>
2300// file : vector.i
2301%module mymodule
2302%{
2303#include "vector.h"
2304%}
2305
2306%include vector.h // Just grab original C header file
2307%extend Vector { // Attach these functions to struct Vector
2308 Vector(double x, double y, double z) {
2309 Vector *v;
2310 v = (Vector *) malloc(sizeof(Vector));
2311 v-&gt;x = x;
2312 v-&gt;y = y;
2313 v-&gt;z = z;
2314 return v;
2315 }
2316 ~Vector() {
2317 free(self);
2318 }
2319 double magnitude() {
2320 return sqrt(self-&gt;x*self-&gt;x+self-&gt;y*self-&gt;y+self-&gt;z*self-&gt;z);
2321 }
2322 void print() {
2323 printf("Vector [%g, %g, %g]\n", self-&gt;x,self-&gt;y,self-&gt;z);
2324 }
2325};
2326
2327</pre></div>
2328
2329<p>
2330Now, when used with proxy classes in Python, you can do things like
2331this :</p>
2332
2333<div class="targetlang"><pre>
2334&gt;&gt;&gt; v = Vector(3,4,0) # Create a new vector
2335&gt;&gt;&gt; print v.magnitude() # Print magnitude
23365.0
2337&gt;&gt;&gt; v.print() # Print it out
2338[ 3, 4, 0 ]
2339&gt;&gt;&gt; del v # Destroy it
2340</pre></div>
2341
2342<p>
2343The <tt>%extend</tt> directive can also be used inside the definition
2344of the Vector structure. For example:</p>
2345
2346<div class="code"><pre>
2347// file : vector.i
2348%module mymodule
2349%{
2350#include "vector.h"
2351%}
2352
2353typedef struct {
2354 double x,y,z;
2355 %extend {
2356 Vector(double x, double y, double z) { ... }
2357 ~Vector() { ... }
2358 ...
2359 }
2360} Vector;
2361</pre></div>
2362
2363<p>
2364Finally, <tt>%extend</tt> can be used to access externally written
2365functions provided they follow the naming convention used in this
2366example :</p>
2367
2368<div class="code"><pre>
2369/* File : vector.c */
2370/* Vector methods */
2371#include "vector.h"
2372Vector *new_Vector(double x, double y, double z) {
2373 Vector *v;
2374 v = (Vector *) malloc(sizeof(Vector));
2375 v-&gt;x = x;
2376 v-&gt;y = y;
2377 v-&gt;z = z;
2378 return v;
2379}
2380void delete_Vector(Vector *v) {
2381 free(v);
2382}
2383
2384double Vector_magnitude(Vector *v) {
2385 return sqrt(v-&gt;x*v-&gt;x+v-&gt;y*v-&gt;y+v-&gt;z*v-&gt;z);
2386}
2387
2388// File : vector.i
2389// Interface file
2390%module mymodule
2391%{
2392#include "vector.h"
2393%}
2394
2395typedef struct {
2396 double x,y,z;
2397 %extend {
2398 Vector(int,int,int); // This calls new_Vector()
2399 ~Vector(); // This calls delete_Vector()
2400 double magnitude(); // This will call Vector_magnitude()
2401 ...
2402 }
2403} Vector;
2404</pre>
2405</div>
2406
2407<p>
2408A little known feature of the <tt>%extend</tt> directive is that
2409it can also be used to add synthesized attributes or to modify the
2410behavior of existing data attributes. For example, suppose you wanted
2411to make <tt>magnitude</tt> a read-only attribute of <tt>Vector</tt>
2412instead of a method. To do this, you might write some code like this:
2413</p>
2414
2415<div class="code">
2416<pre>
2417// Add a new attribute to Vector
2418%extend Vector {
2419 const double magnitude;
2420}
2421// Now supply the implementation of the Vector_magnitude_get function
2422%{
2423const double Vector_magnitude_get(Vector *v) {
2424 return (const double) return sqrt(v-&gt;x*v-&gt;x+v-&gt;y*v-&gt;y+v-&gt;z*v-&gt;z);
2425}
2426%}
2427
2428</pre>
2429</div>
2430
2431<p>
2432Now, for all practial purposes, <tt>magnitude</tt> will appear like an attribute
2433of the object.
2434</p>
2435
2436<p>
2437A similar technique can also be used to work with problematic data members.
2438For example, consider this interface:
2439</p>
2440
2441<div class="code">
2442<pre>
2443struct Person {
2444 char name[50];
2445 ...
2446}
2447</pre>
2448</div>
2449
2450<p>
2451By default, the <tt>name</tt> attribute is read-only because SWIG does not
2452normally know how to modify arrays. However, you can rewrite the interface
2453as follows to change this:
2454</p>
2455
2456<div class="code">
2457<pre>
2458struct Person {
2459 %extend {
2460 char *name;
2461 }
2462...
2463}
2464
2465// Specific implementation of set/get functions
2466%{
2467char *Person_name_get(Person *p) {
2468 return p-&gt;name;
2469}
2470void Person_name_set(Person *p, char *val) {
2471 strncpy(p-&gt;name,val,50);
2472}
2473%}
2474</pre>
2475</div>
2476
2477<p>
2478Finally, it should be stressed that even though <tt>%extend</tt>
2479can be used to add new data members, these new members can not require
2480the allocation of additional storage in the object (e.g., their values must
2481be entirely synthesized from existing attributes of the structure).
2482</p>
2483
2484<p>
2485<b>Compatibility note:</b> The <tt>%extend</tt> directive is a new
2486name for the <tt>%addmethods</tt> directive. Since <tt>%addmethods</tt> could
2487be used to extend a structure with more than just methods, a more suitable
2488directive name has been chosen.
2489</p>
2490
2491<H3><a name="SWIG_nn38"></a>5.5.7 Nested structures</H3>
2492
2493
2494<p>
2495Occasionally, a C program will involve structures like this :</p>
2496
2497<div class="code"><pre>
2498typedef struct Object {
2499 int objtype;
2500 union {
2501 int ivalue;
2502 double dvalue;
2503 char *strvalue;
2504 void *ptrvalue;
2505 } intRep;
2506} Object;
2507
2508</pre></div>
2509
2510<p>
2511When SWIG encounters this, it performs a structure splitting operation
2512that transforms the declaration into the equivalent of the
2513following:</p>
2514
2515<div class="code"><pre>
2516typedef union {
2517 int ivalue;
2518 double dvalue;
2519 char *strvalue;
2520 void *ptrvalue;
2521} Object_intRep;
2522
2523typedef struct Object {
2524 int objType;
2525 Object_intRep intRep;
2526} Object;
2527
2528</pre></div>
2529
2530<p>
2531SWIG will then create an <tt>Object_intRep</tt> structure for use inside
2532the interface file. Accessor functions will be created for both
2533structures. In this case, functions like this would be created :</p>
2534
2535<div class="code"><pre>
2536Object_intRep *Object_intRep_get(Object *o) {
2537 return (Object_intRep *) &amp;o-&gt;intRep;
2538}
2539int Object_intRep_ivalue_get(Object_intRep *o) {
2540 return o-&gt;ivalue;
2541}
2542int Object_intRep_ivalue_set(Object_intRep *o, int value) {
2543 return (o-&gt;ivalue = value);
2544}
2545double Object_intRep_dvalue_get(Object_intRep *o) {
2546 return o-&gt;dvalue;
2547}
2548... etc ...
2549
2550</pre></div>
2551
2552<p>
2553Although this process is a little hairy, it works like you would expect in the
2554target scripting language--especially when proxy classes are used. For instance, in Perl:
2555</p>
2556
2557<div class="targetlang"><pre>
2558# Perl5 script for accessing nested member
2559$o = CreateObject(); # Create an object somehow
2560$o-&gt;{intRep}-&gt;{ivalue} = 7 # Change value of o.intRep.ivalue
2561</pre></div>
2562
2563<p>
2564If you have a lot nested structure declarations, it is
2565advisable to double-check them after running SWIG. Although,
2566there is a good chance that they will work, you may have to
2567modify the interface file in certain cases.
2568
2569<H3><a name="SWIG_nn39"></a>5.5.8 Other things to note about structure wrapping</H3>
2570
2571
2572<p>
2573SWIG doesn't care if the declaration of a structure in a <tt>.i</tt> file exactly matches
2574that used in the underlying C code (except in the case of nested
2575structures). For this reason, there are no problems omitting
2576problematic members or simply omitting the structure definition
2577altogether. If you are happy passing pointers around, this can
2578be done without ever giving SWIG a structure definition.</p>
2579
2580<p>
2581Starting with SWIG1.3, a number of improvements have been made to SWIG's
2582code generator. Specifically, even though structure access has been described
2583in terms of high-level accessor functions such as this,
2584</p>
2585
2586<div class="code">
2587<pre>
2588double Vector_x_get(Vector *v) {
2589 return v-&gt;x;
2590}
2591</pre>
2592</div>
2593
2594<p>
2595most of the generated code is actually inlined directly into wrapper
2596functions. Therefore, no function <tt>Vector_x_get()</tt> actually
2597exists in the generated wrapper file. For example, when creating a Tcl module,
2598the following function is generated instead:
2599</p>
2600
2601<div class="code">
2602<pre>
2603static int
2604_wrap_Vector_x_get(ClientData clientData, Tcl_Interp *interp,
2605 int objc, Tcl_Obj *CONST objv[]) {
2606 struct Vector *arg1 ;
2607 double result ;
2608
2609 if (SWIG_GetArgs(interp, objc, objv,"p:Vector_x_get self ",&amp;arg0,
2610 SWIGTYPE_p_Vector) == TCL_ERROR)
2611 return TCL_ERROR;
2612 result = (double ) (arg1-&gt;x);
2613 Tcl_SetObjResult(interp,Tcl_NewDoubleObj((double) result));
2614 return TCL_OK;
2615}
2616</pre>
2617</div>
2618
2619<p>
2620The only exception to this rule are methods defined with <tt>%extend</tt>. In this
2621case, the added code is contained in a separate function.
2622</p>
2623
2624<p>
2625Finally, it is important to note that most language modules may choose to
2626build a more advanced interface. Although you may never use the low-level
2627interface described here, most of SWIG's language modules use it in
2628some way or another.
2629</p>
2630
2631<H2><a name="SWIG_nn40"></a>5.6 Code Insertion</H2>
2632
2633
2634<p>
2635Sometimes it is necessary to insert special code into the resulting
2636wrapper file generated by SWIG. For example, you may want to include
2637additional C code to perform initialization or other operations.
2638There are four common ways to insert code, but it's useful to know how the
2639output of SWIG is structured first.</p>
2640
2641<H3><a name="SWIG_nn41"></a>5.6.1 The output of SWIG</H3>
2642
2643
2644<p>
2645When SWIG creates its output file, it is broken up into four sections
2646corresponding to runtime code, headers, wrapper functions, and module
2647initialization code (in that order).
2648</p>
2649
2650<ul>
2651<li><b>Runtime code</b>. <br>
2652This code is internal to SWIG and is used to include
2653type-checking and other support functions that are used by the rest of the module.
2654</li>
2655
2656<li><b>Header section</b>. <br>
2657This is user-defined support code that has been included by
2658the <tt>%{ ... %}</tt> directive. Usually this consists of header files and
2659other helper functions.
2660</li>
2661
2662<li><b>Wrapper code</b>. <br>
2663These are the wrappers generated automatically by SWIG.
2664</li>
2665
2666<li><b>Module initialization</b>. <br>
2667The function generated by SWIG to initialize
2668the module upon loading.
2669</li>
2670</ul>
2671
2672<H3><a name="SWIG_nn42"></a>5.6.2 Code insertion blocks</H3>
2673
2674
2675<p>
2676Code is inserted into the appropriate code section by using one
2677of the following code insertion directives:
2678</p>
2679
2680<div class="code">
2681<pre>
2682%runtime %{
2683 ... code in runtime section ...
2684%}
2685
2686%header %{
2687 ... code in header section ...
2688%}
2689
2690%wrapper %{
2691 ... code in wrapper section ...
2692%}
2693
2694%init %{
2695 ... code in init section ...
2696%}
2697</pre>
2698</div>
2699
2700<p>
2701The bare <tt>%{ ... %}</tt> directive is a shortcut that is the same as
2702<tt>%header %{ ... %}</tt>.
2703</p>
2704
2705<p>
2706Everything in a code insertion block is copied verbatim into the output file and is
2707not parsed by SWIG. Most SWIG input files have at least one such block to include header
2708files and support C code. Additional code blocks may be placed anywhere in a
2709SWIG file as needed. </p>
2710
2711<div class="code"><pre>
2712%module mymodule
2713%{
2714#include "my_header.h"
2715%}
2716... Declare functions here
2717%{
2718
2719void some_extra_function() {
2720 ...
2721}
2722%}
2723</pre></div>
2724
2725<p>
2726A common use for code blocks is to write "helper" functions. These
2727are functions that are used specifically for the purpose of building
2728an interface, but which are generally not visible to the normal C
2729program. For example :</p>
2730
2731<div class="code"><pre>
2732%{
2733/* Create a new vector */
2734static Vector *new_Vector() {
2735 return (Vector *) malloc(sizeof(Vector));
2736}
2737
2738%}
2739// Now wrap it
2740Vector *new_Vector();
2741</pre></div>
2742
2743<H3><a name="SWIG_nn43"></a>5.6.3 Inlined code blocks</H3>
2744
2745
2746<p>
2747Since the process of writing helper functions is fairly common,
2748there is a special inlined form of code block that is used as follows
2749:</p>
2750
2751<div class="code"><pre>
2752%inline %{
2753/* Create a new vector */
2754Vector *new_Vector() {
2755 return (Vector *) malloc(sizeof(Vector));
2756}
2757%}
2758
2759</pre></div>
2760
2761<p>
2762The <tt>%inline</tt> directive inserts all of the code that follows
2763verbatim into the header portion of an interface file. The code is
2764then parsed by both the SWIG preprocessor and parser.
2765Thus, the above example creates a new command <tt>new_Vector</tt> using only one
2766declaration. Since the code inside an <tt>%inline %{ ... %}</tt> block
2767is given to both the C compiler and SWIG, it is illegal to include any
2768SWIG directives inside a <tt>%{ ... %}</tt> block.</p>
2769
2770<H3><a name="SWIG_nn44"></a>5.6.4 Initialization blocks</H3>
2771
2772
2773<p>
2774When code is included in the <tt>%init</tt> section, it is copied directly into the
2775module initialization function. For example, if you needed to perform some extra
2776initialization on module loading, you could write this:
2777</p>
2778
2779<div class="code"><pre>
2780%init %{
2781 init_variables();
2782%}
2783</pre></div>
2784
2785<H2><a name="SWIG_nn45"></a>5.7 An Interface Building Strategy</H2>
2786
2787
2788<p>
2789This section describes the general approach for building interface
2790with SWIG. The specifics related to a particular scripting language
2791are found in later chapters.</p>
2792
2793<H3><a name="SWIG_nn46"></a>5.7.1 Preparing a C program for SWIG</H3>
2794
2795
2796<p>
2797SWIG doesn't require modifications to your C code, but if you feed it
2798a collection of raw C header files or source code, the results might
2799not be what you expect---in fact, they might be awful. Here's a series
2800of steps you can follow to make an interface for a C program :</p>
2801
2802<ul>
2803<li>Identify the functions that you want to wrap. It's probably not
2804necessary to access every single function in a C program--thus, a
2805little forethought can dramatically simplify the resulting scripting
2806language interface. C header files are particularly good source for
2807finding things to wrap.
2808
2809<li>Create a new interface file to describe the scripting language
2810interface to your program.
2811
2812<li>Copy the appropriate declarations into the interface file or use
2813SWIG's <tt>%include</tt> directive to process an entire C
2814source/header file.
2815
2816<li>Make sure everything in the interface file uses ANSI C/C++syntax.
2817
2818<li>Make sure all necessary `<tt>typedef</tt>' declarations and
2819type-information is available in the interface file.
2820
2821<li>If your program has a main() function, you may need to rename it
2822(read on).
2823
2824<li>Run SWIG and compile.
2825</ul>
2826
2827<p>
2828Although this may sound complicated, the process turns out to be
2829fairly easy once you get the hang of it.
2830</p>
2831
2832<p>
2833In the process of building an interface, SWIG may encounter syntax errors or
2834other problems. The best way to deal with this is to simply copy the offending
2835code into a separate interface file and edit it. However, the SWIG developers
2836have worked very hard to improve the SWIG parser--you should report parsing errors
2837to the <a href="http://www.swig.org/mail.html">swig-dev mailing list</a> or to the
2838<a href="http://www.swig.org/bugs.html">SWIG bug tracker</a>.
2839</p>
2840
2841<H3><a name="SWIG_nn47"></a>5.7.2 The SWIG interface file</H3>
2842
2843
2844<p>
2845The preferred method of using SWIG is to generate separate interface
2846file. Suppose you have the following C header file :</p>
2847
2848<div class="code"><pre>
2849/* File : header.h */
2850
2851#include &lt;stdio.h&gt;
2852#include &lt;math.h&gt;
2853
2854extern int foo(double);
2855extern double bar(int, int);
2856extern void dump(FILE *f);
2857
2858</pre></div>
2859
2860<p>
2861A typical SWIG interface file for this header file would look like the
2862following :</p>
2863
2864<div class="code"><pre>
2865/* File : interface.i */
2866%module mymodule
2867%{
2868#include "header.h"
2869%}
2870extern int foo(double);
2871extern double bar(int, int);
2872extern void dump(FILE *f);
2873
2874</pre></div>
2875
2876<p>
2877Of course, in this case, our header file is pretty simple so we could
2878have made an interface file like this as well:</p>
2879
2880<div class="code"><pre>
2881/* File : interface.i */
2882%module mymodule
2883%include header.h
2884</pre></div>
2885
2886<p>
2887Naturally, your mileage may vary.</p>
2888
2889<H3><a name="SWIG_nn48"></a>5.7.3 Why use separate interface files?</H3>
2890
2891
2892<p>
2893Although SWIG can parse many header files, it is more common to write a
2894special <tt>.i</tt> file defining the interface to a package. There
2895are several reasons why you might want to do this:
2896</p>
2897
2898<ul>
2899<li>It is rarely necessary to access every single function in a large
2900package. Many C functions might have little or no use in a scripted
2901environment. Therfore, why wrap them?
2902
2903<li>Separate interface files provide an opportunity to provide more
2904precise rules about how an interface is to be constructed.
2905
2906<li>Interface files can provide more structure and organization.
2907
2908<li>SWIG can't parse certain definitions that appear in header
2909files. Having a separate file allows you to eliminate or work around
2910these problems.
2911
2912<li>Interface files provide a more precise definition of what the interface
2913is. Users wanting to extend the system can go to the interface file
2914and immediately see what is available without having to dig it out of
2915header files.
2916</ul>
2917
2918<H3><a name="SWIG_nn49"></a>5.7.4 Getting the right header files</H3>
2919
2920
2921<p>
2922Sometimes, it is necessary to use certain header files in order for
2923the code generated by SWIG to compile properly. Make sure you
2924include certain header files by using a <tt>%{,%}</tt> block like this:
2925</p>
2926
2927<div class="code"><pre>
2928%module graphics
2929%{
2930#include &lt;GL/gl.h&gt;
2931#include &lt;GL/glu.h&gt;
2932%}
2933
2934// Put rest of declarations here
2935...
2936</pre></div>
2937
2938<H3><a name="SWIG_nn50"></a>5.7.5 What to do with main()</H3>
2939
2940
2941<p>
2942If your program defines a <tt>main()</tt> function, you may need to
2943get rid of it or rename it in order to use a scripting language. Most
2944scripting languages define their own <tt>main()</tt> procedure that
2945is called instead. <tt>main()</tt> also makes no sense when
2946working with dynamic loading. There are a few approaches to solving
2947the <tt>main()</tt> conflict :</p>
2948
2949<ul>
2950<li>Get rid of <tt>main()</tt> entirely.
2951
2952<li>Rename <tt>main()</tt> to something else. You can do this by
2953compiling your C program with an option like <tt>-Dmain=oldmain</tt>.
2954
2955<li>Use conditional compilation to only include <tt>main()</tt> when
2956not using a scripting language.
2957</ul>
2958
2959<p>
2960Getting rid of <tt>main()</tt> may cause potential initialization
2961problems of a program. To handle this problem, you may consider
2962writing a special function called <tt>program_init()</tt> that
2963initializes your program upon startup. This function could then be
2964called either from the scripting language as the first operation, or
2965when the SWIG generated module is loaded.</p>
2966
2967<p>
2968As a general note, many C programs only use the <tt>main()</tt>
2969function to parse command line options and to set parameters. However,
2970by using a scripting language, you are probably trying to create a
2971program that is more interactive. In many cases, the old
2972<tt>main()</tt> program can be completely replaced by a Perl, Python,
2973or Tcl script.</p>
2974
2975<p>
2976<b>Note:</b> If some cases, you might be inclined to create a
2977scripting language wrapper for <tt>main()</tt>. If you do this, the
2978compilation will probably work and your module might even load
2979correctly. The only trouble is that when you call your
2980<tt>main()</tt> wrapper, you will find that it actually invokes the
2981<tt>main()</tt> of the scripting language interpreter itself! This behavior
2982is a side effect of the symbol binding mechanism used in the dynamic linker.
2983The bottom line: don't do this.
2984</p>
2985
2986</body>
2987</html>