| 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> |
| 83 | This chapter describes the basic operation of SWIG, the structure of its |
| 84 | input files, and how it handles standard ANSI C declarations. C++ support is |
| 85 | described in the next chapter. However, C++ programmers should still read this |
| 86 | chapter to understand the basics. |
| 87 | Specific details about each target language are described in later |
| 88 | chapters. |
| 89 | </p> |
| 90 | |
| 91 | <H2><a name="SWIG_nn2"></a>5.1 Running SWIG</H2> |
| 92 | |
| 93 | |
| 94 | <p> |
| 95 | To run SWIG, use the <tt>swig</tt> command with one or more of the |
| 96 | following options and a filename like this: |
| 97 | </p> |
| 98 | |
| 99 | <div class="shell"><pre> |
| 100 | swig [ <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> |
| 131 | This is a subset of commandline options. |
| 132 | Additional options are also defined for each target language. A full list |
| 133 | can 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> |
| 141 | As input, SWIG expects a file containing ANSI C/C++ declarations and |
| 142 | special SWIG directives. More often than not, this is a special SWIG |
| 143 | interface 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 |
| 145 | raw header files or source files. However, this is not the most |
| 146 | typical case and there are several reasons why you might not want to |
| 147 | do this (described later). |
| 148 | </p> |
| 149 | |
| 150 | <p> |
| 151 | The 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 |
| 160 | int foo; |
| 161 | int bar(int x); |
| 162 | ... |
| 163 | </pre></div> |
| 164 | <p> |
| 165 | The name of the module is supplied using the special <tt>%module</tt> |
| 166 | directive (or the <tt>-module</tt> command line option). This |
| 167 | directive must appear at the beginning of the file and is used to name |
| 168 | the resulting extension module (in addition, this name often defines |
| 169 | a namespace in the target language). If the module name is supplied on the |
| 170 | command line, it overrides the name specified with the |
| 171 | <tt>%module</tt> directive. |
| 172 | </p> |
| 173 | |
| 174 | <p> |
| 175 | Everything in the <tt>%{ ... %}</tt> block is simply copied verbatim |
| 176 | to the resulting wrapper file created by SWIG. This section is almost |
| 177 | always used to include header files and other declarations that are |
| 178 | required to make the generated wrapper code compile. It is important |
| 179 | to emphasize that just because you include a declaration in a SWIG |
| 180 | input file, that declaration does <em>not</em> automatically appear in |
| 181 | the generated wrapper code---therefore you need to make sure you |
| 182 | include the proper header files in the <tt>%{ ... %}</tt> section. It |
| 183 | should be noted that the text enclosed in <tt>%{ ... %}</tt> is not |
| 184 | parsed or interpreted by SWIG. The <tt>%{...%}</tt> syntax and |
| 185 | semantics in SWIG is analogous to that of the declarations section |
| 186 | used 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> |
| 193 | The output of SWIG is a C/C++ file that contains all of the wrapper |
| 194 | code needed to build an extension module. SWIG may generate some |
| 195 | additional files depending on the target language. By default, an input file |
| 196 | with 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 |
| 198 | or not the <tt>-c++</tt> option has been used). The name of the |
| 199 | output file can be changed using the <tt>-o</tt> option. In certain |
| 200 | cases, file suffixes are used by the compiler to determine the source |
| 201 | language (C, C++, etc.). Therefore, you have to use the |
| 202 | <tt>-o</tt> option to change the suffix of the SWIG-generated wrapper |
| 203 | file 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> |
| 211 | The C/C++ output file created by SWIG often |
| 212 | contains everything that is needed to construct a extension module |
| 213 | for the target scripting language. SWIG is not a stub compiler nor is it |
| 214 | usually necessary to edit the output file (and if you look at the output, |
| 215 | you probably won't want to). To build the final extension module, the |
| 216 | SWIG output file is compiled and linked with the rest of your C/C++ |
| 217 | program to create a shared library. |
| 218 | </p> |
| 219 | |
| 220 | <p> |
| 221 | Many target languages will also generate proxy class files in the |
| 222 | target language. The default output directory for these language |
| 223 | specific files is the same directory as the generated C/C++ file. This can |
| 224 | can 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> |
| 231 | If the directories <tt>cppfiles</tt> and <tt>pyfiles</tt> exist, the following |
| 232 | will be generated:</p> |
| 233 | <div class="shell"><pre> |
| 234 | cppfiles/example_wrap.cpp |
| 235 | pyfiles/example.py |
| 236 | </pre></div> |
| 237 | |
| 238 | <H3><a name="SWIG_nn5"></a>5.1.3 Comments</H3> |
| 239 | |
| 240 | |
| 241 | <p> |
| 242 | C and C++ style comments may appear anywhere in interface files. In |
| 243 | previous versions of SWIG, comments were used to generate |
| 244 | documentation files. However, this feature is currently under repair |
| 245 | and 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> |
| 252 | Like C, SWIG preprocesses all input files through an enhanced version |
| 253 | of the C preprocessor. All standard preprocessor features are |
| 254 | supported including file inclusion, conditional compilation and |
| 255 | macros. However, <tt>#include</tt> statements are ignored unless the |
| 256 | <tt>-includeall</tt> command line option has been supplied. The |
| 257 | reason for disabling includes is that SWIG is sometimes used to |
| 258 | process raw C header files. In this case, you usually only want the |
| 259 | extension module to include functions in the supplied header file |
| 260 | rather than everything that might be included by that header file |
| 261 | (i.e., system headers, C library functions, etc.). |
| 262 | </p> |
| 263 | |
| 264 | <p> |
| 265 | It should also be noted that the SWIG preprocessor skips all text |
| 266 | enclosed inside a <tt>%{...%}</tt> block. In addition, the |
| 267 | preprocessor includes a number of macro handling enhancements that |
| 268 | make it more powerful than the normal C preprocessor. These |
| 269 | extensions 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> |
| 276 | Most of SWIG's operation is controlled by special directives that are |
| 277 | always preceded by a "<tt>%</tt>" to distinguish them from normal C |
| 278 | declarations. These directives are used to give SWIG hints or to alter |
| 279 | SWIG's parsing behavior in some manner. |
| 280 | </p> |
| 281 | |
| 282 | <p> |
| 283 | Since SWIG directives are not legal C syntax, it is generally not |
| 284 | possible to include them in header files. However, SWIG directives can be |
| 285 | included 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 |
| 300 | it 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> |
| 307 | Although SWIG can parse most C/C++ declarations, it does not |
| 308 | provide a complete C/C++ parser implementation. Most of these |
| 309 | limitations pertain to very complicated type declarations and certain |
| 310 | advanced C++ features. Specifically, the following features are not |
| 311 | currently supported: |
| 312 | </p> |
| 313 | |
| 314 | <ul> |
| 315 | <li>Non-conventional type declarations. |
| 316 | For 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) */ |
| 322 | const int extern Number; |
| 323 | |
| 324 | /* Extra declarator grouping */ |
| 325 | Matrix (foo); // A global variable |
| 326 | |
| 327 | /* Extra declarator grouping in parameters */ |
| 328 | void bar(Spam (Grok)(Doh)); |
| 329 | |
| 330 | </pre> |
| 331 | </div> |
| 332 | |
| 333 | <p> |
| 334 | In practice, few (if any) C programmers actually write code like |
| 335 | this since this style is never featured in programming books. However, |
| 336 | if 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) |
| 341 | is not recommended. Even though SWIG can parse C++ class declarations, |
| 342 | it ignores declarations that are decoupled from their |
| 343 | original class definition (the declarations are parsed, but a lot of warning |
| 344 | messages may be generated). For example: |
| 345 | |
| 346 | <div class="code"> |
| 347 | <pre> |
| 348 | /* Not supported by SWIG */ |
| 349 | int foo::bar(int) { |
| 350 | ... whatever ... |
| 351 | } |
| 352 | </pre> |
| 353 | </div> |
| 354 | </li> |
| 355 | |
| 356 | <li>Certain advanced features of C++ such as nested classes |
| 357 | are not yet supported. Please see the section on using SWIG |
| 358 | with C++ for more information. |
| 359 | </ul> |
| 360 | |
| 361 | <p> |
| 362 | In the event of a parsing error, conditional compilation can be used to skip |
| 363 | offending 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> |
| 374 | Alternatively, you can just delete the offending code from the interface file. |
| 375 | </p> |
| 376 | |
| 377 | <p> |
| 378 | One of the reasons why SWIG does not provide a full C++ parser |
| 379 | implementation is that it has been designed to work with incomplete |
| 380 | specifications and to be very permissive in its handling of C/C++ |
| 381 | datatypes (e.g., SWIG can generate interfaces even when there are |
| 382 | missing class declarations or opaque datatypes). Unfortunately, this |
| 383 | approach makes it extremely difficult to implement certain parts of a |
| 384 | C/C++ parser as most compilers use type information to assist in the |
| 385 | parsing of more complex declarations (for the truly curious, the |
| 386 | primary complication in the implementation is that the SWIG parser |
| 387 | does not utilize a separate <em>typedef-name</em> terminal symbol as |
| 388 | described on p. 234 of K&R). |
| 389 | </p> |
| 390 | |
| 391 | <H2><a name="SWIG_nn9"></a>5.2 Wrapping Simple C Declarations</H2> |
| 392 | |
| 393 | |
| 394 | <p> |
| 395 | SWIG wraps simple C declarations by creating an interface that closely matches |
| 396 | the way in which the declarations would be used in a C program. |
| 397 | For example, consider the following interface file: |
| 398 | </p> |
| 399 | |
| 400 | <div class="code"><pre> |
| 401 | %module example |
| 402 | |
| 403 | %inline %{ |
| 404 | extern double sin(double x); |
| 405 | extern int strcmp(const char *, const char *); |
| 406 | extern int Foo; |
| 407 | %} |
| 408 | #define STATUS 50 |
| 409 | #define VERSION "1.1" |
| 410 | </pre></div> |
| 411 | <p> |
| 412 | In this file, there are two functions <tt>sin()</tt> and <tt>strcmp()</tt>, |
| 413 | a global variable <tt>Foo</tt>, and two constants <tt>STATUS</tt> and |
| 414 | <tt>VERSION</tt>. When SWIG creates an extension module, these |
| 415 | declarations are accessible as scripting language functions, variables, and |
| 416 | constants respectively. For example, in Tcl: |
| 417 | </p> |
| 418 | |
| 419 | <div class="targetlang"><pre> |
| 420 | % sin 3 |
| 421 | 5.2335956 |
| 422 | % strcmp Dave Mike |
| 423 | -1 |
| 424 | % puts $Foo |
| 425 | 42 |
| 426 | % puts $STATUS |
| 427 | 50 |
| 428 | % puts $VERSION |
| 429 | 1.1 |
| 430 | </pre></div> |
| 431 | <p> |
| 432 | Or in Python: |
| 433 | </p> |
| 434 | |
| 435 | <div class="targetlang"><pre> |
| 436 | >>> example.sin(3) |
| 437 | 5.2335956 |
| 438 | >>> example.strcmp('Dave','Mike') |
| 439 | -1 |
| 440 | >>> print example.cvar.Foo |
| 441 | 42 |
| 442 | >>> print example.STATUS |
| 443 | 50 |
| 444 | >>> print example.VERSION |
| 445 | 1.1 |
| 446 | </pre></div> |
| 447 | <p> |
| 448 | Whenever possible, SWIG creates an interface that closely matches the underlying C/C++ |
| 449 | code. However, due to subtle differences between languages, run-time |
| 450 | environments, and semantics, it is not always possible to do so. The |
| 451 | next 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> |
| 458 | In order to build an interface, SWIG has to convert C/C++ datatypes to |
| 459 | equivalent types in the target language. Generally, |
| 460 | scripting languages provide a more limited set of primitive types than C. |
| 461 | Therefore, this conversion process involves a certain amount of type |
| 462 | coercion. |
| 463 | </p> |
| 464 | |
| 465 | <p> |
| 466 | Most scripting languages provide a single integer type that is implemented using |
| 467 | the <tt>int</tt> or <tt>long</tt> datatype in C. The following list shows |
| 468 | all of the C datatypes that SWIG will convert to and from integers in the target language: |
| 469 | </p> |
| 470 | |
| 471 | <div class="code"><pre> |
| 472 | int |
| 473 | short |
| 474 | long |
| 475 | unsigned |
| 476 | signed |
| 477 | unsigned short |
| 478 | unsigned long |
| 479 | unsigned char |
| 480 | signed char |
| 481 | bool |
| 482 | </pre></div> |
| 483 | |
| 484 | <p> |
| 485 | When an integral value is converted from C, a cast is used to convert it to |
| 486 | the representation in the target language. |
| 487 | Thus, a 16 bit short in C may be promoted to a 32 bit integer. When integers are |
| 488 | converted in the other direction, the value is cast back into the original C type. |
| 489 | If 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 |
| 495 | are handled as small 8-bit integers. Normally, the <tt>char</tt> |
| 496 | datatype is mapped as a one-character ASCII string. </p> |
| 497 | |
| 498 | <p> |
| 499 | The <tt>bool</tt> datatype is cast to and from an integer value of 0 |
| 500 | and 1 unless the target language provides a special boolean type.</p> |
| 501 | |
| 502 | <p> |
| 503 | Some care is required when working with large integer values. Most |
| 504 | scripting languages use 32-bit integers so mapping a 64-bit long |
| 505 | integer may lead to truncation errors. Similar problems may arise with |
| 506 | 32 bit unsigned integers (which may appear as large negative |
| 507 | numbers). As a rule of thumb, the <tt>int</tt> datatype and all |
| 508 | variations of <tt>char</tt> and <tt>short</tt> datatypes are safe to |
| 509 | use. For <tt>unsigned int</tt> and <tt>long</tt> datatypes, you will |
| 510 | need to carefully check the correct operation of your program after |
| 511 | it has been wrapped with SWIG. |
| 512 | </p> |
| 513 | |
| 514 | <p> |
| 515 | Although the SWIG parser supports the <tt>long long</tt> datatype, not |
| 516 | all language modules support it. This is because <tt>long long</tt> |
| 517 | usually exceeds the integer precision available in the target |
| 518 | language. In certain modules such as Tcl and Perl5, <tt>long |
| 519 | long</tt> integers are encoded as strings. This allows the full range |
| 520 | of these numbers to be represented. However, it does not allow |
| 521 | <tt>long long</tt> values to be used in arithmetic expressions. It |
| 522 | should also be noted that although <tt>long long</tt> is part |
| 523 | of the ISO C99 standard, it is not universally supported by all C |
| 524 | compilers. Make sure you are using a compiler that supports <tt>long |
| 525 | long</tt> before trying to use this type with SWIG. |
| 526 | </p> |
| 527 | |
| 528 | <p> |
| 529 | SWIG recognizes the following floating point types :</p> |
| 530 | |
| 531 | <div class="code"><pre> |
| 532 | float |
| 533 | double |
| 534 | </pre></div> |
| 535 | |
| 536 | <p> |
| 537 | Floating point numbers are mapped to and from the natural |
| 538 | representation of floats in the target language. This is almost always |
| 539 | a C <tt>double</tt>. The rarely used datatype of <tt>long double</tt> |
| 540 | is not supported by SWIG.</p> |
| 541 | |
| 542 | <p> |
| 543 | The <tt>char</tt> datatype is mapped into a NULL terminated ASCII |
| 544 | string with a single character. When used in a scripting language it |
| 545 | shows up as a tiny string containing the character value. When |
| 546 | converting the value back into C, SWIG takes a character string |
| 547 | from the scripting language and strips off the first character as the |
| 548 | char value. Thus if the value "foo" is assigned to a |
| 549 | <tt>char</tt> datatype, it gets the value `f'.</p> |
| 550 | |
| 551 | <p> |
| 552 | The <tt>char *</tt> datatype is handled as a NULL-terminated ASCII |
| 553 | string. SWIG maps this into a 8-bit character string in the target |
| 554 | scripting language. SWIG converts character strings in the target |
| 555 | language to NULL terminated strings before passing them into |
| 556 | C/C++. The default handling of these strings does not allow them to |
| 557 | have embedded NULL bytes. Therefore, the <tt>char *</tt> datatype is |
| 558 | not generally suitable for passing binary data. However, it is |
| 559 | possible to change this behavior by defining a SWIG typemap. See the chapter |
| 560 | on <a href="Typemaps.html#Typemaps">Typemaps</a> for details about this. |
| 561 | </p> |
| 562 | |
| 563 | <p> |
| 564 | At this time, SWIG does not provide any special support for Unicode or |
| 565 | wide-character strings (the C <tt>wchar_t</tt> type). This is a |
| 566 | delicate topic that is poorly understood by many programmers and not |
| 567 | implemented in a consistent manner across languages. For those |
| 568 | scripting languages that provide Unicode support, Unicode strings are |
| 569 | often available in an 8-bit representation such as UTF-8 that can be |
| 570 | mapped to the <tt>char *</tt> type (in which case the SWIG interface |
| 571 | will probably work). If the program you are wrapping uses Unicode, |
| 572 | there is no guarantee that Unicode characters in the target language |
| 573 | will use the same internal representation (e.g., UCS-2 vs. UCS-4). |
| 574 | You 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> |
| 581 | Whenever possible, SWIG maps C/C++ global variables into scripting language |
| 582 | variables. For example, |
| 583 | </p> |
| 584 | |
| 585 | <div class="code"><pre> |
| 586 | %module example |
| 587 | double foo; |
| 588 | |
| 589 | </pre></div> |
| 590 | <p> |
| 591 | results in a scripting language variable like this: |
| 592 | </p> |
| 593 | |
| 594 | <div class="code"><pre> |
| 595 | # Tcl |
| 596 | set foo [3.5] ;# Set foo to 3.5 |
| 597 | puts $foo ;# Print the value of foo |
| 598 | |
| 599 | # Python |
| 600 | cvar.foo = 3.5 # Set foo to 3.5 |
| 601 | print cvar.foo # Print value of foo |
| 602 | |
| 603 | # Perl |
| 604 | $foo = 3.5; # Set foo to 3.5 |
| 605 | print $foo,"\n"; # Print value of foo |
| 606 | |
| 607 | # Ruby |
| 608 | Module.foo = 3.5 # Set foo to 3.5 |
| 609 | print Module.foo, "\n" # Print value of foo |
| 610 | </pre></div> |
| 611 | <p> |
| 612 | Whenever the scripting language variable is used, the underlying C |
| 613 | global variable is accessed. Although SWIG makes every |
| 614 | attempt to make global variables work like scripting language |
| 615 | variables, it is not always possible to do so. For instance, in |
| 616 | Python, all global variables must be accessed through a special |
| 617 | variable object known as <tt>cvar</tt> (shown above). In Ruby, variables are |
| 618 | accessed as attributes of the module. Other languages may |
| 619 | convert variables to a pair of accessor functions. For example, the |
| 620 | Java module generates a pair of functions <tt>double get_foo()</tt> |
| 621 | and <tt>set_foo(double val)</tt> that are used to manipulate the |
| 622 | value. |
| 623 | </p> |
| 624 | |
| 625 | <p> |
| 626 | Finally, if a global variable has been declared as <tt>const</tt>, it |
| 627 | only supports read-only access. Note: this behavior is new to SWIG-1.3. |
| 628 | Earlier versions of SWIG incorrectly handled <tt>const</tt> and created |
| 629 | constants instead. |
| 630 | </p> |
| 631 | |
| 632 | <H3><a name="SWIG_nn12"></a>5.2.3 Constants</H3> |
| 633 | |
| 634 | |
| 635 | <p> |
| 636 | Constants can be created using <tt>#define</tt>, enumerations, |
| 637 | or a special <tt>%constant</tt> directive. The following |
| 638 | interface 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 | |
| 646 | enum boolean {NO=0, YES=1}; |
| 647 | enum 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> |
| 656 | In <tt>#define</tt> declarations, the type of a constant is inferred |
| 657 | by syntax. For example, a number with a decimal point is assumed to be |
| 658 | floating point. In addition, SWIG must be able to fully resolve all |
| 659 | of the symbols used in a <tt>#define</tt> in order for a constant to |
| 660 | actually be created. This restriction is necessary because |
| 661 | <tt>#define</tt> is also used to define preprocessor macros that are |
| 662 | definitely not meant to be part of the scripting language interface. |
| 663 | For example: |
| 664 | </p> |
| 665 | |
| 666 | <div class="code"> |
| 667 | <pre> |
| 668 | #define EXTERN extern |
| 669 | |
| 670 | EXTERN void foo(); |
| 671 | </pre> |
| 672 | </div> |
| 673 | <p> |
| 674 | In this case, you probably don't want to create a constant called |
| 675 | <tt>EXTERN</tt> (what would the value be?). In general, |
| 676 | SWIG will not create constants for macros unless the value can |
| 677 | be completely determined by the preprocessor. For instance, in the above example, |
| 678 | the declaration |
| 679 | </p> |
| 680 | |
| 681 | <div class="code"> |
| 682 | <pre> |
| 683 | #define PI_4 PI/4 |
| 684 | </pre> |
| 685 | </div> |
| 686 | <p> |
| 687 | defines a constant because <tt>PI</tt> was already defined as a |
| 688 | constant and the value is known. |
| 689 | </p> |
| 690 | |
| 691 | <p> |
| 692 | The use of constant expressions is allowed, but SWIG does not evaluate |
| 693 | them. Rather, it passes them through to the output file and lets the C |
| 694 | compiler perform the final evaluation (SWIG does perform a limited |
| 695 | form of type-checking however).</p> |
| 696 | |
| 697 | <p> |
| 698 | For enumerations, it is critical that the original enum definition be |
| 699 | included somewhere in the interface file (either in a header file or |
| 700 | in the <tt>%{,%}</tt> block). SWIG only translates the enumeration |
| 701 | into code needed to add the constants to a scripting language. It |
| 702 | needs the original enumeration declaration in order to get the correct |
| 703 | enum values as assigned by the C compiler. |
| 704 | </p> |
| 705 | |
| 706 | <p> |
| 707 | The <tt>%constant</tt> directive is used to more precisely create |
| 708 | constants corresponding to different C datatypes. Although it is not |
| 709 | usually not needed for simple values, it is more useful when working |
| 710 | with pointers and other more complex datatypes. Typically, <tt>%constant</tt> |
| 711 | is only used when you want to add constants to the scripting language |
| 712 | interface 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> |
| 719 | A common confusion with C programming is the semantic meaning of the |
| 720 | <tt>const</tt> qualifier in declarations--especially when it is mixed |
| 721 | with pointers and other type modifiers. In fact, previous versions of SWIG |
| 722 | handled <tt>const</tt> incorrectly--a situation that SWIG-1.3.7 and newer |
| 723 | releases have fixed. |
| 724 | </p> |
| 725 | |
| 726 | <p> |
| 727 | Starting with SWIG-1.3, all variable declarations, regardless of any |
| 728 | use of <tt>const</tt>, are wrapped as global variables. If a |
| 729 | declaration happens to be declared as <tt>const</tt>, it is wrapped as |
| 730 | a read-only variable. To tell if a variable is <tt>const</tt> or not, |
| 731 | you need to look at the right-most occurrence of the <tt>const</tt> |
| 732 | qualifier (that appears before the variable name). If the right-most |
| 733 | <tt>const</tt> occurs after all other type modifiers (such as |
| 734 | pointers), then the variable is <tt>const</tt>. Otherwise, it is not. |
| 735 | </p> |
| 736 | |
| 737 | <p> |
| 738 | Here are some examples of <tt>const</tt> declarations. |
| 739 | </p> |
| 740 | |
| 741 | <div class="code"> |
| 742 | <pre> |
| 743 | const char a; // A constant character |
| 744 | char const b; // A constant character (the same) |
| 745 | char *const c; // A constant pointer to a character |
| 746 | const char *const d; // A constant pointer to a constant character |
| 747 | </pre> |
| 748 | </div> |
| 749 | <p> |
| 750 | Here is an example of a declaration that is not <tt>const</tt>: |
| 751 | </p> |
| 752 | |
| 753 | <div class="code"> |
| 754 | <pre> |
| 755 | const char *e; // A pointer to a constant character. The pointer |
| 756 | // may be modified. |
| 757 | </pre> |
| 758 | </div> |
| 759 | <p> |
| 760 | In this case, the pointer <tt>e</tt> can change---it's only the value |
| 761 | being 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 |
| 767 | many situations where the value of a <tt>const</tt> variable might |
| 768 | change. For example, a library might export a symbol as |
| 769 | <tt>const</tt> in its public API to discourage modification, but still |
| 770 | allow the value to change through some other kind of internal |
| 771 | mechanism. Furthermore, programmers often overlook the fact that with |
| 772 | a constant declaration like <tt>char *const</tt>, the underlying data |
| 773 | being pointed to can be modified--it's only the pointer itself that is |
| 774 | constant. In an embedded system, a <tt>const</tt> declaration might |
| 775 | refer to a read-only memory address such as the location of a |
| 776 | memory-mapped I/O device port (where the value changes, but writing to |
| 777 | the port is not supported by the hardware). Rather than trying to |
| 778 | build a bunch of special cases into the <tt>const</tt> qualifier, the |
| 779 | new interpretation of <tt>const</tt> as "read-only" is simple and |
| 780 | exactly matches the actual semantics of <tt>const</tt> in C/C++. If |
| 781 | you really want to create a constant as in older versions of SWIG, use |
| 782 | the <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> |
| 792 | or |
| 793 | </p> |
| 794 | |
| 795 | <div class="code"> |
| 796 | <pre> |
| 797 | #ifdef SWIG |
| 798 | #define const %constant |
| 799 | #endif |
| 800 | const double foo = 3.4; |
| 801 | const double bar = 23.4; |
| 802 | const 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> |
| 815 | Before going any further, there is one bit of caution involving |
| 816 | <tt>char *</tt> that must now be mentioned. When strings are passed |
| 817 | from a scripting language to a C <tt>char *</tt>, the pointer usually |
| 818 | points to string data stored inside the interpreter. It is almost |
| 819 | always a really bad idea to modify this data. Furthermore, some |
| 820 | languages may explicitly disallow it. For instance, in Python, |
| 821 | strings are supposed be immutable. If you violate this, you will probably |
| 822 | receive a vast amount of wrath when you unleash your module on the world. |
| 823 | </p> |
| 824 | |
| 825 | <p> |
| 826 | The primary source of problems are functions that might modify string data in place. |
| 827 | A classic example would be a function like this: |
| 828 | </p> |
| 829 | |
| 830 | <div class="code"> |
| 831 | <pre> |
| 832 | char *strcat(char *s, const char *t) |
| 833 | </pre> |
| 834 | </div> |
| 835 | |
| 836 | <p> |
| 837 | Although SWIG will certainly generate a wrapper for this, its behavior |
| 838 | will be undefined. In fact, it will probably cause your application |
| 839 | to crash with a segmentation fault or other memory related problem. |
| 840 | This is because <tt>s</tt> refers to some internal data in the target |
| 841 | language---data that you shouldn't be touching. |
| 842 | </p> |
| 843 | |
| 844 | <p> |
| 845 | The bottom line: don't rely on <tt>char *</tt> for anything other than read-only |
| 846 | input values. However, it must be noted that you could change the behavior of SWIG |
| 847 | using <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> |
| 854 | Most C programs manipulate arrays, structures, and other types of objects. This section |
| 855 | discusses 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> |
| 862 | Pointers to primitive C datatypes such as </p> |
| 863 | |
| 864 | <div class="code"><pre> |
| 865 | int * |
| 866 | double *** |
| 867 | char ** |
| 868 | </pre></div> |
| 869 | <p> |
| 870 | are fully supported by SWIG. Rather than trying to convert the data being pointed to into a scripting |
| 871 | representation, SWIG simply encodes the pointer itself into a |
| 872 | representation that contains the actual value of the pointer and a type-tag. |
| 873 | Thus, the SWIG representation of the above |
| 874 | pointers (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> |
| 883 | A NULL pointer is represented by the string "NULL" or the value 0 |
| 884 | encoded with type information.</p> |
| 885 | |
| 886 | <p> |
| 887 | All pointers are treated as opaque objects by SWIG. Thus, a pointer |
| 888 | may be returned by a function and passed around to other C functions |
| 889 | as needed. For all practical purposes, the scripting language |
| 890 | interface works in exactly the same way as you would use the |
| 891 | pointer in a C program. The only difference is that there is no mechanism for |
| 892 | dereferencing the pointer since this would require the target language |
| 893 | to understand the memory layout of the underlying object. |
| 894 | </p> |
| 895 | |
| 896 | <p> |
| 897 | The scripting language representation of a pointer value should never be |
| 898 | manipulated directly. Even though the values shown look like hexadecimal |
| 899 | addresses, the numbers used may differ from the actual machine address (e.g., |
| 900 | on little-endian machines, the digits may appear in reverse order). |
| 901 | Furthermore, SWIG does not |
| 902 | normally map pointers into high-level objects such as associative |
| 903 | arrays or lists (for example, converting an |
| 904 | <tt>int *</tt> into an list of integers). There are several reasons |
| 905 | why SWIG does not do this:</p> |
| 906 | |
| 907 | <ul> |
| 908 | <li>There is not enough information in a C declaration to properly map |
| 909 | pointers into higher level constructs. For example, an <tt>int *</tt> |
| 910 | may indeed be an array of integers, but if it contains ten million |
| 911 | elements, 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 |
| 915 | by SWIG. For instance, an <tt>int *</tt> might not be an array at all--perhaps it |
| 916 | is an output value! |
| 917 | </li> |
| 918 | |
| 919 | <li>By handling all pointers in a consistent manner, the implementation of SWIG is greatly |
| 920 | simplified 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> |
| 928 | By allowing pointers to be manipulated from a scripting language, extension modules |
| 929 | effectively bypass compile-time type checking in the C/C++ |
| 930 | compiler. To prevent errors, a type signature is encoded into all |
| 931 | pointer values and is used to perform run-time type checking. This |
| 932 | type-checking process is an integral part of SWIG and can not be |
| 933 | disabled or modified without using typemaps (described in later |
| 934 | chapters). |
| 935 | </p> |
| 936 | |
| 937 | <p> |
| 938 | Like C, <tt>void *</tt> matches any kind of pointer. Furthermore, |
| 939 | <tt>NULL</tt> pointers can be passed to any function that expects to |
| 940 | receive a pointer. Although this has the potential to cause a crash, |
| 941 | <tt>NULL</tt> pointers are also sometimes used |
| 942 | as sentinel values or to denote a missing/empty value. Therefore, |
| 943 | SWIG 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> |
| 950 | For everything else (structs, classes, arrays, etc...) SWIG applies a |
| 951 | very simple rule :</p> |
| 952 | |
| 953 | <center> |
| 954 | <b>Everything else is a pointer</b> |
| 955 | </center> |
| 956 | |
| 957 | <p> |
| 958 | In other words, SWIG manipulates everything else by reference. This |
| 959 | model makes sense because most C/C++ programs make heavy use of |
| 960 | pointers and SWIG can use the type-checked pointer mechanism already |
| 961 | present for handling pointers to basic datatypes.</p> |
| 962 | |
| 963 | <p> |
| 964 | Although this probably sounds complicated, it's really quite |
| 965 | simple. Suppose you have an interface file like this :</p> |
| 966 | |
| 967 | <div class="code"><pre> |
| 968 | %module fileio |
| 969 | FILE *fopen(char *, char *); |
| 970 | int fclose(FILE *); |
| 971 | unsigned fread(void *ptr, unsigned size, unsigned nobj, FILE *); |
| 972 | unsigned fwrite(void *ptr, unsigned size, unsigned nobj, FILE *); |
| 973 | void *malloc(int nbytes); |
| 974 | void free(void *); |
| 975 | |
| 976 | </pre></div> |
| 977 | |
| 978 | <p> |
| 979 | In this file, SWIG doesn't know what a <tt>FILE</tt> is, but since it's used |
| 980 | as a pointer, so it doesn't really matter what it is. If you wrapped |
| 981 | this module into Python, you can use the functions just like you |
| 982 | expect :</p> |
| 983 | |
| 984 | <div class="targetlang"><pre> |
| 985 | # Copy a file |
| 986 | def 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 > 0): |
| 992 | fwrite(buffer,8192,1,f2) |
| 993 | nbytes = fread(buffer,8192,1,f1) |
| 994 | free(buffer) |
| 995 | |
| 996 | </pre></div> |
| 997 | |
| 998 | <p> |
| 999 | In this case <tt>f1</tt>,<tt> f2</tt>, and <tt>buffer</tt> are all |
| 1000 | opaque objects containing C pointers. It doesn't matter what value |
| 1001 | they 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> |
| 1007 | When SWIG encounters an undeclared datatype, it automatically assumes |
| 1008 | that it is a structure or class. For example, suppose the following |
| 1009 | function appeared in a SWIG input file:</p> |
| 1010 | |
| 1011 | <div class="code"><pre> |
| 1012 | void matrix_multiply(Matrix *a, Matrix *b, Matrix *c); |
| 1013 | </pre></div> |
| 1014 | |
| 1015 | <p> |
| 1016 | SWIG has no idea what a "<tt>Matrix</tt>" is. However, it is obviously |
| 1017 | a pointer to something so SWIG generates a wrapper using its generic pointer |
| 1018 | handling code. |
| 1019 | </p> |
| 1020 | |
| 1021 | <p> |
| 1022 | Unlike C or C++, SWIG does not actually care whether <tt>Matrix</tt> |
| 1023 | has been previously defined in the interface file or not. This |
| 1024 | allows SWIG to generate interfaces from |
| 1025 | only partial or limited information. In some cases, you may not care |
| 1026 | what a <tt>Matrix</tt> really is as long as you can pass an opaque reference to |
| 1027 | one around in the scripting language interface. |
| 1028 | </p> |
| 1029 | |
| 1030 | <p> |
| 1031 | An important detail to mention is that SWIG will gladly generate |
| 1032 | wrappers for an interface when there are unspecified type names. |
| 1033 | However, <b>all unspecified types are internally handled as pointers |
| 1034 | to structures or classes!</b> For example, consider the following declaration: |
| 1035 | </p> |
| 1036 | |
| 1037 | <div class="code"> |
| 1038 | <pre> |
| 1039 | void foo(size_t num); |
| 1040 | </pre> |
| 1041 | </div> |
| 1042 | |
| 1043 | <p> |
| 1044 | If <tt>size_t</tt> is undeclared, SWIG generates wrappers |
| 1045 | that expect to receive a type of <tt>size_t *</tt> (this mapping is described shortly). |
| 1046 | As a result, the scripting interface might behave strangely. For example: |
| 1047 | </p> |
| 1048 | |
| 1049 | <div class="code"> |
| 1050 | <pre> |
| 1051 | foo(40); |
| 1052 | TypeError: expected a _p_size_t. |
| 1053 | </pre> |
| 1054 | </div> |
| 1055 | |
| 1056 | <p> |
| 1057 | The 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> |
| 1067 | Like C, <tt>typedef</tt> can be used to define new type names in SWIG. For example: |
| 1068 | </p> |
| 1069 | |
| 1070 | <div class="code"><pre> |
| 1071 | typedef unsigned int size_t; |
| 1072 | </pre></div> |
| 1073 | |
| 1074 | <p> |
| 1075 | <tt>typedef</tt> definitions appearing in a SWIG interface |
| 1076 | are not propagated to the generated wrapper code. Therefore, they |
| 1077 | either need to be defined in an included header file or placed in the |
| 1078 | declarations section like this: |
| 1079 | </p> |
| 1080 | |
| 1081 | <div class="code"> |
| 1082 | <pre> |
| 1083 | %{ |
| 1084 | /* Include in the generated wrapper file */ |
| 1085 | typedef unsigned int size_t; |
| 1086 | %} |
| 1087 | /* Tell SWIG about it */ |
| 1088 | typedef unsigned int size_t; |
| 1089 | </pre> |
| 1090 | </div> |
| 1091 | |
| 1092 | <p> |
| 1093 | or |
| 1094 | </p> |
| 1095 | |
| 1096 | <div class="code"> |
| 1097 | <pre> |
| 1098 | %inline %{ |
| 1099 | typedef unsigned int size_t; |
| 1100 | %} |
| 1101 | </pre> |
| 1102 | </div> |
| 1103 | |
| 1104 | <p> |
| 1105 | In certain cases, you might be able to include other header files to collect type information. |
| 1106 | For 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> |
| 1117 | In 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> |
| 1127 | It should be noted that your mileage will vary greatly here. |
| 1128 | System headers are notoriously complicated and may rely upon a variety |
| 1129 | of non-standard C coding extensions (e.g., such as special directives |
| 1130 | to GCC). Unless you exactly specify the right include directories and |
| 1131 | preprocessor symbols, this may not work correctly (you will have to |
| 1132 | experiment). |
| 1133 | </p> |
| 1134 | |
| 1135 | <p> |
| 1136 | SWIG tracks <tt>typedef</tt> declarations and uses this information |
| 1137 | for run-time type checking. For instance, if you use the above <tt>typedef</tt> and |
| 1138 | had the following function declaration: |
| 1139 | </p> |
| 1140 | |
| 1141 | <div class="code"> |
| 1142 | <pre> |
| 1143 | void foo(unsigned int *ptr); |
| 1144 | </pre> |
| 1145 | </div> |
| 1146 | |
| 1147 | <p> |
| 1148 | The corresponding wrapper function will accept arguments of |
| 1149 | type <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> |
| 1156 | So far, this chapter has presented almost everything you need to know to use SWIG |
| 1157 | for simple interfaces. However, some C programs use idioms that are somewhat |
| 1158 | more difficult to map to a scripting language interface. This section describes |
| 1159 | some 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> |
| 1166 | Sometimes a C function takes structure parameters that are passed |
| 1167 | by value. For example, consider the following function: |
| 1168 | </p> |
| 1169 | |
| 1170 | <div class="code"><pre> |
| 1171 | double dot_product(Vector a, Vector b); |
| 1172 | </pre></div> |
| 1173 | |
| 1174 | <p> |
| 1175 | To deal with this, SWIG transforms the function to use pointers by |
| 1176 | creating a wrapper equivalent to the following: |
| 1177 | </p> |
| 1178 | |
| 1179 | <div class="code"><pre> |
| 1180 | double 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> |
| 1188 | In the target language, the <tt>dot_product()</tt> function now accepts pointers |
| 1189 | to Vectors instead of Vectors. For the most part, this transformation |
| 1190 | is 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> |
| 1197 | C functions that return structures or classes datatypes by value are more difficult |
| 1198 | to handle. Consider the following function:</p> |
| 1199 | |
| 1200 | <div class="code"><pre> |
| 1201 | Vector cross_product(Vector v1, Vector v2); |
| 1202 | </pre></div> |
| 1203 | |
| 1204 | <p> |
| 1205 | This function wants to return <tt>Vector</tt>, but SWIG only really supports |
| 1206 | pointers. As a result, SWIG creates a wrapper like this: |
| 1207 | </p> |
| 1208 | |
| 1209 | <div class="code"><pre> |
| 1210 | Vector *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> |
| 1221 | or if SWIG was run with the <tt>-c++</tt> option:</p> |
| 1222 | |
| 1223 | <div class="code"><pre> |
| 1224 | Vector *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> |
| 1233 | In both cases, SWIG allocates a new object and returns a reference to it. It |
| 1234 | is up to the user to delete the returned object when it is no longer |
| 1235 | in use. Clearly, this will leak memory if you are unaware of the implicit |
| 1236 | memory allocation and don't take steps to free the result. That said, it should be |
| 1237 | noted that some language modules can now automatically track newly created objects and |
| 1238 | reclaim memory for you. Consult the documentation for each language module for more details. |
| 1239 | </p> |
| 1240 | |
| 1241 | <p> |
| 1242 | It should also be noted that the handling of pass/return by value in |
| 1243 | C++ has some special cases. For example, the above code fragments |
| 1244 | don't work correctly if <tt>Vector</tt> doesn't define a default |
| 1245 | constructor. 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> |
| 1252 | When global variables or class members involving structures are |
| 1253 | encountered, SWIG handles them as pointers. For example, a global |
| 1254 | variable like this</p> |
| 1255 | |
| 1256 | <div class="code"><pre> |
| 1257 | Vector unit_i; |
| 1258 | </pre></div> |
| 1259 | |
| 1260 | <p> |
| 1261 | gets mapped to an underlying pair of set/get functions like this :</p> |
| 1262 | |
| 1263 | <div class="code"><pre> |
| 1264 | Vector *unit_i_get() { |
| 1265 | return &unit_i; |
| 1266 | } |
| 1267 | void unit_i_set(Vector *value) { |
| 1268 | unit_i = *value; |
| 1269 | } |
| 1270 | </pre></div> |
| 1271 | |
| 1272 | <p> |
| 1273 | Again some caution is in order. A global variable created in this |
| 1274 | manner will show up as a pointer in the target scripting language. It |
| 1275 | would be an extremely bad idea to free or destroy such a pointer. Also, |
| 1276 | C++ classes must supply a properly defined copy constructor in order for |
| 1277 | assignment 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> |
| 1284 | When 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 |
| 1286 | like this |
| 1287 | </p> |
| 1288 | |
| 1289 | <div class="code"> |
| 1290 | <pre> |
| 1291 | char *foo; |
| 1292 | </pre> |
| 1293 | </div> |
| 1294 | |
| 1295 | <p> |
| 1296 | SWIG generates the following code: |
| 1297 | </p> |
| 1298 | |
| 1299 | <div class="code"> |
| 1300 | <pre> |
| 1301 | /* C mode */ |
| 1302 | void 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 */ |
| 1309 | void 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> |
| 1318 | If 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 |
| 1320 | exactly 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> |
| 1334 | Note: If you write an assist function like this, you will have to call |
| 1335 | it as a function from the target scripting language (it does not work |
| 1336 | like a variable). For example, in Python you will have to write: |
| 1337 | </p> |
| 1338 | |
| 1339 | <div class="targetlang"> |
| 1340 | <pre> |
| 1341 | >>> set_foo("Hello World") |
| 1342 | </pre> |
| 1343 | </div> |
| 1344 | |
| 1345 | <p> |
| 1346 | A 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> |
| 1351 | char *VERSION = "1.0"; |
| 1352 | </pre> |
| 1353 | </div> |
| 1354 | |
| 1355 | <p> |
| 1356 | In this case, the variable will be readable, but any attempt to change |
| 1357 | the value results in a segmentation or general protection fault. This |
| 1358 | is 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 |
| 1360 | allocated using <tt>malloc()</tt> or <tt>new</tt>. |
| 1361 | To fix this behavior, you can |
| 1362 | either mark the variable as read-only, write a typemap (as described in Chapter 6), |
| 1363 | or write a special set function as shown. Another alternative is to declare the |
| 1364 | variable as an array: |
| 1365 | </p> |
| 1366 | |
| 1367 | <div class="code"> |
| 1368 | <pre> |
| 1369 | char VERSION[64] = "1.0"; |
| 1370 | </pre> |
| 1371 | </div> |
| 1372 | |
| 1373 | <p> |
| 1374 | When variables of type <tt>const char *</tt> are declared, SWIG still generates functions for setting and |
| 1375 | getting the value. However, the default behavior does <em>not</em> release the previous contents (resulting in |
| 1376 | a 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> |
| 1381 | example.i:20. Typemap warning. Setting const char * variable may leak memory |
| 1382 | </pre> |
| 1383 | </div> |
| 1384 | |
| 1385 | <p> |
| 1386 | The reason for this behavior is that <tt>const char *</tt> variables are often used to point to string literals. |
| 1387 | For example: |
| 1388 | </p> |
| 1389 | |
| 1390 | <div class="code"> |
| 1391 | <pre> |
| 1392 | const char *foo = "Hello World\n"; |
| 1393 | </pre> |
| 1394 | </div> |
| 1395 | |
| 1396 | <p> |
| 1397 | Therefore, it's a really bad idea to call <tt>free()</tt> on such a |
| 1398 | pointer. On the other hand, it <em>is</em> legal to change the |
| 1399 | pointer to point to some other value. When setting a variable of this |
| 1400 | type, SWIG allocates a new string (using malloc or new) and changes |
| 1401 | the pointer to point to the new value. However, repeated |
| 1402 | modifications of the value will result in a memory leak since the old |
| 1403 | value 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> |
| 1413 | Arrays are fully supported by SWIG, but they are always handled as pointers instead |
| 1414 | of mapping them to a special array object or list in the target language. Thus, the |
| 1415 | following declarations :</p> |
| 1416 | |
| 1417 | <div class="code"><pre> |
| 1418 | int foobar(int a[40]); |
| 1419 | void grok(char *argv[]); |
| 1420 | void transpose(double a[20][20]); |
| 1421 | </pre></div> |
| 1422 | |
| 1423 | <p> |
| 1424 | are processed as if they were really declared like this: |
| 1425 | </p> |
| 1426 | |
| 1427 | <div class="code"><pre> |
| 1428 | int foobar(int *a); |
| 1429 | void grok(char **argv); |
| 1430 | void transpose(double (*a)[20]); |
| 1431 | </pre></div> |
| 1432 | |
| 1433 | <p> |
| 1434 | Like C, SWIG does not perform array bounds checking. |
| 1435 | It is up to the |
| 1436 | user to make sure the pointer points a suitably allocated region of memory. |
| 1437 | </p> |
| 1438 | |
| 1439 | <p> |
| 1440 | Multi-dimensional arrays are transformed into a pointer to an array of one less |
| 1441 | dimension. For example: |
| 1442 | </p> |
| 1443 | |
| 1444 | <div class="code"> |
| 1445 | <pre> |
| 1446 | int [10]; // Maps to int * |
| 1447 | int [10][20]; // Maps to int (*)[20] |
| 1448 | int [10][20][30]; // Maps to int (*)[20][30] |
| 1449 | </pre> |
| 1450 | </div> |
| 1451 | |
| 1452 | <p> |
| 1453 | It is important to note that in the C type system, a multidimensional |
| 1454 | array <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 |
| 1456 | pointer to an array is used (as shown above) where the actual value of |
| 1457 | the pointer is the starting memory location of the array. The |
| 1458 | reader is strongly advised to dust off their C book and re-read the |
| 1459 | section on arrays before using them with SWIG. |
| 1460 | </p> |
| 1461 | |
| 1462 | <p> |
| 1463 | Array variables are supported, but are read-only by default. For example: |
| 1464 | </p> |
| 1465 | |
| 1466 | <div class="code"> |
| 1467 | <pre> |
| 1468 | int a[100][200]; |
| 1469 | </pre> |
| 1470 | </div> |
| 1471 | |
| 1472 | <p> |
| 1473 | In this case, reading the variable 'a' returns a pointer of type <tt>int (*)[200]</tt> |
| 1474 | that points to the first element of the array <tt>&a[0][0]</tt>. Trying to modify 'a' results |
| 1475 | in an error. This is because SWIG does not know how to copy data from the target |
| 1476 | language into the array. To work around this limitation, you may want to write |
| 1477 | a few simple assist functions like this: |
| 1478 | </p> |
| 1479 | |
| 1480 | <div class="code"> |
| 1481 | <pre> |
| 1482 | %inline %{ |
| 1483 | void a_set(int i, int j, int val) { |
| 1484 | a[i][j] = val; |
| 1485 | } |
| 1486 | int a_get(int i, int j) { |
| 1487 | return a[i][j]; |
| 1488 | } |
| 1489 | %} |
| 1490 | </pre> |
| 1491 | </div> |
| 1492 | |
| 1493 | <p> |
| 1494 | To dynamically create arrays of various sizes and shapes, it may be useful to write |
| 1495 | some 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> |
| 1515 | Arrays of <tt>char</tt> are handled as a special case by SWIG. In this case, strings in the |
| 1516 | target 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> |
| 1521 | char pathname[256]; |
| 1522 | </pre> |
| 1523 | </div> |
| 1524 | |
| 1525 | <p> |
| 1526 | SWIG generates functions for both getting and setting the value that are equivalent to the following |
| 1527 | code: |
| 1528 | </p> |
| 1529 | |
| 1530 | <div class="code"> |
| 1531 | <pre> |
| 1532 | char *pathname_get() { |
| 1533 | return pathname; |
| 1534 | } |
| 1535 | void pathname_set(char *value) { |
| 1536 | strncpy(pathname,value,256); |
| 1537 | } |
| 1538 | </pre> |
| 1539 | </div> |
| 1540 | |
| 1541 | <p> |
| 1542 | In 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> |
| 1549 | A read-only variable can be created by using the <tt>%immutable</tt> |
| 1550 | directive as shown :</p> |
| 1551 | |
| 1552 | <div class="code"><pre> |
| 1553 | // File : interface.i |
| 1554 | |
| 1555 | int a; // Can read/write |
| 1556 | %immutable; |
| 1557 | int b,c,d // Read only variables |
| 1558 | %mutable; |
| 1559 | double x,y // read/write |
| 1560 | </pre></div> |
| 1561 | |
| 1562 | <p> |
| 1563 | The <tt>%immutable</tt> directive enables read-only mode until it is |
| 1564 | explicitly disabled using the <tt>%mutable</tt> directive. As an alternative to turning |
| 1565 | read-only mode off and on like this, individual declarations can also be tagged as |
| 1566 | immutable. For example: |
| 1567 | </p> |
| 1568 | |
| 1569 | <div class="code"><pre> |
| 1570 | %immutable x; // Make x read-only |
| 1571 | ... |
| 1572 | double x; // Read-only (from earlier %immutable directive) |
| 1573 | double y; // Read-write |
| 1574 | ... |
| 1575 | </pre></div> |
| 1576 | |
| 1577 | <p> |
| 1578 | The <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> |
| 1588 | If 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 | ... |
| 1595 | double x; |
| 1596 | double y; |
| 1597 | double z; |
| 1598 | ... |
| 1599 | </pre></div> |
| 1600 | |
| 1601 | <p> |
| 1602 | Read-only variables are also created when declarations are declared as <tt>const</tt>. |
| 1603 | For example: |
| 1604 | </p> |
| 1605 | |
| 1606 | <div class="code"> |
| 1607 | <pre> |
| 1608 | const int foo; /* Read only variable */ |
| 1609 | char * 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 |
| 1615 | generate 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> |
| 1623 | Normally, the name of a C declaration is used when that declaration is |
| 1624 | wrapped into the target language. However, this may generate a |
| 1625 | conflict with a keyword or already existing function in the scripting |
| 1626 | language. To resolve a name conflict, you can use the <tt>%rename</tt> |
| 1627 | directive as shown :</p> |
| 1628 | |
| 1629 | <div class="code"><pre> |
| 1630 | // interface.i |
| 1631 | |
| 1632 | %rename(my_print) print; |
| 1633 | extern void print(char *); |
| 1634 | |
| 1635 | %rename(foo) a_really_long_and_annoying_name; |
| 1636 | extern int a_really_long_and_annoying_name; |
| 1637 | |
| 1638 | </pre></div> |
| 1639 | |
| 1640 | <p> |
| 1641 | SWIG still calls the correct C function, but in this case the |
| 1642 | function <tt>print()</tt> will really be called "<tt>my_print()</tt>" |
| 1643 | in the target language. </p> |
| 1644 | |
| 1645 | <p> |
| 1646 | The placement of the <tt>%rename</tt> directive is arbitrary as long as it appears |
| 1647 | before the declarations to be renamed. A common technique is to write code for |
| 1648 | wrapping 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 |
| 1662 | occurrences of a name. The renaming applies to functions, variables, |
| 1663 | class and structure names, member functions, and member data. For |
| 1664 | example, if you had two-dozen C++ classes, all with a member function |
| 1665 | named `print' (which is a keyword in Python), you could rename them |
| 1666 | all 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> |
| 1673 | SWIG does not normally perform any checks to see if the functions it wraps are |
| 1674 | already defined in the target scripting language. However, if you are |
| 1675 | careful about namespaces and your use of modules, you can usually |
| 1676 | avoid these problems.</p> |
| 1677 | |
| 1678 | <p> |
| 1679 | Closely related to <tt>%rename</tt> is the <tt>%ignore</tt> directive. <tt>%ignore</tt> instructs SWIG |
| 1680 | to 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> |
| 1694 | One use of <tt>%ignore</tt> is to selectively remove certain declarations from a header file without having |
| 1695 | to add conditional compilation to the header. However, it should be stressed that this only works for simple |
| 1696 | declarations. If you need to remove a whole section of problematic code, the SWIG preprocessor should be used instead. |
| 1697 | </p> |
| 1698 | |
| 1699 | <p> |
| 1700 | More powerful variants of <tt>%rename</tt> and <tt>%ignore</tt> directives can be used to help |
| 1701 | wrap 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. |
| 1707 | For example: |
| 1708 | </p> |
| 1709 | |
| 1710 | <div class="code"> |
| 1711 | <pre> |
| 1712 | %name(output) extern void print(char *); |
| 1713 | </pre> |
| 1714 | </div> |
| 1715 | |
| 1716 | <p> |
| 1717 | This directive is still supported, but it is deprecated and should probably be avoided. The <tt>%rename</tt> |
| 1718 | directive 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> |
| 1725 | SWIG supports default arguments in both C and C++ code. For example: |
| 1726 | </p> |
| 1727 | |
| 1728 | <div class="code"><pre> |
| 1729 | int plot(double x, double y, int color=WHITE); |
| 1730 | </pre></div> |
| 1731 | |
| 1732 | <p> |
| 1733 | In this case, SWIG generates wrapper code where the |
| 1734 | default arguments are optional in the target language. For example, this function could be |
| 1735 | used 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> |
| 1744 | Although the ANSI C standard does not allow default arguments, default |
| 1745 | arguments 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 |
| 1750 | of default arguments and the SWIG generated wrapper code. When default |
| 1751 | arguments are used in C code, the default values are emitted into the wrappers and the |
| 1752 | function is invoked with a full set of arguments. This is different to when wrapping C++ |
| 1753 | where an overloaded wrapper method is generated for each defaulted argument. |
| 1754 | Please refer to the section on <a href="SWIGPlus.html#SWIGPlus_default_args">default arguments</a> |
| 1755 | in 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> |
| 1762 | Occasionally, a C library may include functions that expect to receive |
| 1763 | pointers to functions--possibly to serve as callbacks. SWIG |
| 1764 | provides full support for function pointers provided that the callback |
| 1765 | functions are defined in C and not in the target language. For example, |
| 1766 | consider a function like this: |
| 1767 | </p> |
| 1768 | |
| 1769 | <div class="code"><pre> |
| 1770 | int binary_op(int a, int b, int (*op)(int,int)); |
| 1771 | </pre></div> |
| 1772 | |
| 1773 | <p> |
| 1774 | When you first wrap something like this into an extension module, you |
| 1775 | may find the function to be impossible to use. For instance, in Python: |
| 1776 | </p> |
| 1777 | |
| 1778 | <div class="targetlang"><pre> |
| 1779 | >>> def add(x,y): |
| 1780 | ... return x+y |
| 1781 | ... |
| 1782 | >>> binary_op(3,4,add) |
| 1783 | Traceback (most recent call last): |
| 1784 | File "<stdin>", line 1, in ? |
| 1785 | TypeError: Type error. Expected _p_f_int_int__int |
| 1786 | >>> |
| 1787 | </pre> |
| 1788 | </div> |
| 1789 | |
| 1790 | <p> |
| 1791 | The reason for this error is that SWIG doesn't know how to map a scripting |
| 1792 | language function into a C callback. However, existing C functions can |
| 1793 | be used as arguments provided you install them as constants. |
| 1794 | One 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 */ |
| 1799 | int 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> |
| 1808 | In this case, <tt>add</tt>, <tt>sub</tt>, and <tt>mul</tt> become function pointer |
| 1809 | constants in the target scripting language. This allows you to use them as follows: |
| 1810 | </p> |
| 1811 | |
| 1812 | <div class="targetlang"> |
| 1813 | <pre> |
| 1814 | >>> binary_op(3,4,add) |
| 1815 | 7 |
| 1816 | >>> binary_op(3,4,mul) |
| 1817 | 12 |
| 1818 | >>> |
| 1819 | </pre> |
| 1820 | </div> |
| 1821 | |
| 1822 | <p> |
| 1823 | Unfortunately, by declaring the callback functions as constants, they are no longer accesible |
| 1824 | as functions. For example: |
| 1825 | </p> |
| 1826 | |
| 1827 | <div class="targetlang"> |
| 1828 | <pre> |
| 1829 | >>> add(3,4) |
| 1830 | Traceback (most recent call last): |
| 1831 | File "<stdin>", line 1, in ? |
| 1832 | TypeError: object is not callable: '_ff020efc_p_f_int_int__int' |
| 1833 | >>> |
| 1834 | </pre> |
| 1835 | </div> |
| 1836 | |
| 1837 | <p> |
| 1838 | If you want to make a function available as both a callback function and a function, you |
| 1839 | can 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 */ |
| 1845 | int binary_op(int a, int b, int (*op)(int,int)); |
| 1846 | |
| 1847 | /* Some callback functions */ |
| 1848 | %callback("%s_cb") |
| 1849 | int add(int,int); |
| 1850 | int sub(int,int); |
| 1851 | int mul(int,int); |
| 1852 | %nocallback |
| 1853 | </pre></div> |
| 1854 | |
| 1855 | <p> |
| 1856 | The argument to <tt>%callback</tt> is a printf-style format string that |
| 1857 | specifies the naming convention for the callback constants (<tt>%s</tt> gets replaced |
| 1858 | by the function name). The callback mode remains in effect until it is explicitly |
| 1859 | disabled using <tt>%nocallback</tt>. When you do this, the interface now works as follows: |
| 1860 | </p> |
| 1861 | |
| 1862 | <div class="targetlang"> |
| 1863 | <pre> |
| 1864 | >>> binary_op(3,4,add_cb) |
| 1865 | 7 |
| 1866 | >>> binary_op(3,4,mul_cb) |
| 1867 | 12 |
| 1868 | >>> add(3,4) |
| 1869 | 7 |
| 1870 | >>> mul(3,4) |
| 1871 | 12 |
| 1872 | </pre> |
| 1873 | </div> |
| 1874 | |
| 1875 | <p> |
| 1876 | Notice that when the function is used as a callback, special names |
| 1877 | such as <tt>add_cb</tt> is used instead. To call the function |
| 1878 | normally, just use the original function name such as <tt>add()</tt>. |
| 1879 | </p> |
| 1880 | |
| 1881 | <p> |
| 1882 | SWIG provides a number of extensions to standard C printf formatting |
| 1883 | that may be useful in this context. For instance, the following |
| 1884 | variation 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") |
| 1891 | int add(int,int); |
| 1892 | int sub(int,int); |
| 1893 | int mul(int,int); |
| 1894 | %nocallback |
| 1895 | </pre></div> |
| 1896 | |
| 1897 | <p> |
| 1898 | A format string of <tt>"%(lower)s"</tt> converts all characters to lower-case. |
| 1899 | A string of <tt>"%(title)s"</tt> capitalizes the first character and converts the |
| 1900 | rest to lower case. |
| 1901 | </p> |
| 1902 | |
| 1903 | <p> |
| 1904 | And now, a final note about function pointer support. Although SWIG |
| 1905 | does not normally allow callback functions to be written in the target language, this |
| 1906 | can be accomplished with the use of typemaps and other advanced SWIG features. |
| 1907 | This 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> |
| 1914 | This section describes the behavior of SWIG when processing ANSI C structures and union declarations. Extensions to |
| 1915 | handle C++ are described in the next section. |
| 1916 | </p> |
| 1917 | |
| 1918 | <p> |
| 1919 | If SWIG encounters the definition of a structure or union, it |
| 1920 | creates a set of accessor functions. Although SWIG does not need |
| 1921 | structure definitions to build an interface, providing definitions |
| 1922 | make it possible to access structure members. The accessor functions |
| 1923 | generated by SWIG simply take a pointer to an object and allow access |
| 1924 | to an individual member. For example, the declaration :</p> |
| 1925 | |
| 1926 | <div class="code"><pre> |
| 1927 | struct Vector { |
| 1928 | double x,y,z; |
| 1929 | } |
| 1930 | |
| 1931 | </pre></div> |
| 1932 | |
| 1933 | <p> |
| 1934 | gets transformed into the following set of accessor functions :</p> |
| 1935 | |
| 1936 | <div class="code"><pre> |
| 1937 | double Vector_x_get(struct Vector *obj) { |
| 1938 | return obj->x; |
| 1939 | } |
| 1940 | double Vector_y_get(struct Vector *obj) { |
| 1941 | return obj->y; |
| 1942 | } |
| 1943 | double Vector_z_get(struct Vector *obj) { |
| 1944 | return obj->z; |
| 1945 | } |
| 1946 | void Vector_x_set(struct Vector *obj, double value) { |
| 1947 | obj->x = value; |
| 1948 | } |
| 1949 | void Vector_y_set(struct Vector *obj, double value) { |
| 1950 | obj->y = value; |
| 1951 | } |
| 1952 | void Vector_z_set(struct Vector *obj, double value) { |
| 1953 | obj->z = value; |
| 1954 | } |
| 1955 | </pre></div> |
| 1956 | |
| 1957 | <p> |
| 1958 | In addition, SWIG creates default constructor and destructor functions if none are |
| 1959 | defined in the interface. For example: |
| 1960 | </p> |
| 1961 | |
| 1962 | <div class="code"><pre> |
| 1963 | struct Vector *new_Vector() { |
| 1964 | return (Vector *) calloc(1,sizeof(struct Vector)); |
| 1965 | } |
| 1966 | void delete_Vector(struct Vector *obj) { |
| 1967 | free(obj); |
| 1968 | } |
| 1969 | </pre> |
| 1970 | </div> |
| 1971 | |
| 1972 | <p> |
| 1973 | Using these low-level accessor functions, an object can be minimally manipulated from the target |
| 1974 | language using code like this: |
| 1975 | </p> |
| 1976 | |
| 1977 | <div class="code"> |
| 1978 | <pre> |
| 1979 | v = new_Vector() |
| 1980 | Vector_x_set(v,2) |
| 1981 | Vector_y_set(v,10) |
| 1982 | Vector_z_set(v,-5) |
| 1983 | ... |
| 1984 | delete_Vector(v) |
| 1985 | </pre> |
| 1986 | </div> |
| 1987 | |
| 1988 | <p> |
| 1989 | However, 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> |
| 1996 | SWIG supports the following construct which is quite common in C |
| 1997 | programs :</p> |
| 1998 | |
| 1999 | <div class="code"><pre> |
| 2000 | typedef struct { |
| 2001 | double x,y,z; |
| 2002 | } Vector; |
| 2003 | |
| 2004 | </pre></div> |
| 2005 | |
| 2006 | <p> |
| 2007 | When encountered, SWIG assumes that the name of the object is `Vector' |
| 2008 | and creates accessor functions like before. The only difference is |
| 2009 | that 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> |
| 2015 | double Vector_x_get(Vector *obj) { |
| 2016 | return obj->x; |
| 2017 | } |
| 2018 | </pre> |
| 2019 | </div> |
| 2020 | |
| 2021 | <p> |
| 2022 | If two different names are used like this :</p> |
| 2023 | |
| 2024 | <div class="code"><pre> |
| 2025 | typedef struct vector_struct { |
| 2026 | double x,y,z; |
| 2027 | } Vector; |
| 2028 | |
| 2029 | </pre></div> |
| 2030 | |
| 2031 | <p> |
| 2032 | the name <tt>Vector</tt> is used instead of <tt>vector_struct</tt> since |
| 2033 | this is more typical C programming style. If declarations defined later in the interface use the type <tt>struct |
| 2034 | vector_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> |
| 2042 | Structures involving character strings require some care. SWIG assumes |
| 2043 | that all members of type <tt>char *</tt> have been dynamically |
| 2044 | allocated using <tt>malloc()</tt> and that they are NULL-terminated |
| 2045 | ASCII strings. When such a member is modified, the previously contents |
| 2046 | will be released, and the new contents allocated. For example :</p> |
| 2047 | |
| 2048 | <div class="code"><pre> |
| 2049 | %module mymodule |
| 2050 | ... |
| 2051 | struct Foo { |
| 2052 | char *name; |
| 2053 | ... |
| 2054 | } |
| 2055 | |
| 2056 | </pre></div> |
| 2057 | |
| 2058 | <p> |
| 2059 | This results in the following accessor functions :</p> |
| 2060 | |
| 2061 | <div class="code"><pre> |
| 2062 | char *Foo_name_get(Foo *obj) { |
| 2063 | return Foo->name; |
| 2064 | } |
| 2065 | |
| 2066 | char *Foo_name_set(Foo *obj, char *c) { |
| 2067 | if (obj->name) free(obj->name); |
| 2068 | obj->name = (char *) malloc(strlen(c)+1); |
| 2069 | strcpy(obj->name,c); |
| 2070 | return obj->name; |
| 2071 | } |
| 2072 | </pre></div> |
| 2073 | |
| 2074 | <p> |
| 2075 | If this behavior differs from what you need in your applications, |
| 2076 | the SWIG "memberin" typemap can be used to change it. See the |
| 2077 | typemaps chapter for further details. |
| 2078 | </p> |
| 2079 | |
| 2080 | <p> |
| 2081 | Note: If the <tt>-c++</tt> option is used, <tt>new</tt> and <tt>delete</tt> are used to |
| 2082 | perform memory allocation. |
| 2083 | </p> |
| 2084 | |
| 2085 | <H3><a name="SWIG_nn34"></a>5.5.3 Array members</H3> |
| 2086 | |
| 2087 | |
| 2088 | <p> |
| 2089 | Arrays may appear as the members of structures, but they will be |
| 2090 | read-only. SWIG will write an accessor function that returns the |
| 2091 | pointer to the first element of the array, but will not write a |
| 2092 | function to change the contents of the array itself. |
| 2093 | When this |
| 2094 | situation is detected, SWIG may generate a warning message such as the |
| 2095 | following :</p> |
| 2096 | |
| 2097 | <div class="shell"><pre> |
| 2098 | interface.i:116. Warning. Array member will be read-only |
| 2099 | </pre></div> |
| 2100 | |
| 2101 | <p> |
| 2102 | To eliminate the warning message, typemaps can be used, but this is |
| 2103 | discussed in a later chapter. In many cases, the warning message is |
| 2104 | harmless. |
| 2105 | </p> |
| 2106 | |
| 2107 | <H3><a name="SWIG_nn35"></a>5.5.4 Structure data members</H3> |
| 2108 | |
| 2109 | |
| 2110 | <p> |
| 2111 | Occasionally, a structure will contain data members that are themselves structures. For example: |
| 2112 | </p> |
| 2113 | |
| 2114 | <div class="code"> |
| 2115 | <pre> |
| 2116 | typedef struct Foo { |
| 2117 | int x; |
| 2118 | } Foo; |
| 2119 | |
| 2120 | typedef struct Bar { |
| 2121 | int y; |
| 2122 | Foo f; /* struct member */ |
| 2123 | } Bar; |
| 2124 | </pre> |
| 2125 | </div> |
| 2126 | |
| 2127 | <p> |
| 2128 | When a structure member is wrapped, it is always handled as a pointer. |
| 2129 | For example: |
| 2130 | </p> |
| 2131 | |
| 2132 | <div class="code"> |
| 2133 | <pre> |
| 2134 | Foo *Bar_f_get(Bar *b) { |
| 2135 | return &b->f; |
| 2136 | } |
| 2137 | void Bar_f_set(Bar *b, Foo *value) { |
| 2138 | b->f = *value; |
| 2139 | } |
| 2140 | </pre> |
| 2141 | </div> |
| 2142 | |
| 2143 | <p> |
| 2144 | The reasons for this are somewhat subtle but have to do with the |
| 2145 | problem of modifying and accessing data inside the data member. For |
| 2146 | example, suppose you wanted to modify the value of <tt>f.x</tt> |
| 2147 | of a <tt>Bar</tt> object like this: |
| 2148 | </p> |
| 2149 | |
| 2150 | <div class="code"> |
| 2151 | <pre> |
| 2152 | Bar *b; |
| 2153 | b->f.x = 37; |
| 2154 | </pre> |
| 2155 | </div> |
| 2156 | |
| 2157 | <p> |
| 2158 | Translating this assignment to function calls (as would be used inside the scripting |
| 2159 | language interface) results in the following code: |
| 2160 | </p> |
| 2161 | |
| 2162 | <div class="code"> |
| 2163 | <pre> |
| 2164 | Bar *b; |
| 2165 | Foo_x_set(Bar_f_get(b),37); |
| 2166 | </pre> |
| 2167 | </div> |
| 2168 | |
| 2169 | <p> |
| 2170 | In 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 |
| 2172 | the data member <tt>f</tt> itself. Clearly that's not what you want! |
| 2173 | </p> |
| 2174 | |
| 2175 | <p> |
| 2176 | It should be noted that this transformation to pointers only occurs if SWIG knows that a data member |
| 2177 | is a structure or class. For instance, if you had a structure like this, |
| 2178 | </p> |
| 2179 | |
| 2180 | <div class="code"> |
| 2181 | <pre> |
| 2182 | struct Foo { |
| 2183 | WORD w; |
| 2184 | }; |
| 2185 | </pre> |
| 2186 | </div> |
| 2187 | |
| 2188 | <p> |
| 2189 | and nothing was known about <tt>WORD</tt>, then SWIG will generate more normal accessor functions |
| 2190 | like this: |
| 2191 | </p> |
| 2192 | |
| 2193 | <div class="code"> |
| 2194 | <pre> |
| 2195 | WORD Foo_w_get(Foo *f) { |
| 2196 | return f->w; |
| 2197 | } |
| 2198 | void Foo_w_set(FOO *f, WORD value) { |
| 2199 | f->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 |
| 2206 | to pointers. Starting in SWIG-1.3.12, this transformation <em>only</em> occurs if a datatype is known to be a structure, |
| 2207 | class, or union. This is unlikely to break existing code. However, if you need to tell SWIG that an undeclared |
| 2208 | datatype 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> |
| 2215 | When wrapping structures, it is generally useful to have a mechanism |
| 2216 | for creating and destroying objects. If you don't do anything, SWIG |
| 2217 | will automatically generate functions for creating and destroying |
| 2218 | objects 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 |
| 2221 | differently. |
| 2222 | </p> |
| 2223 | |
| 2224 | <p> |
| 2225 | If you don't want SWIG to generate constructors and destructors, you |
| 2226 | can use the <tt>%nodefault</tt> directive or the <tt>-no_default</tt> |
| 2227 | command line option. For example: |
| 2228 | </p> |
| 2229 | |
| 2230 | <div class="shell"><pre> |
| 2231 | swig -no_default example.i |
| 2232 | </pre></div> |
| 2233 | |
| 2234 | <p> |
| 2235 | or |
| 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> |
| 2247 | If you need more precise control, <tt>%nodefault</tt> can selectively target individual structure |
| 2248 | definitions. For example: |
| 2249 | </p> |
| 2250 | |
| 2251 | <div class="code"> |
| 2252 | <pre> |
| 2253 | %nodefault Foo; // No default constructor/destructors for Foo |
| 2254 | ... |
| 2255 | struct Foo { // No default generated. |
| 2256 | }; |
| 2257 | |
| 2258 | struct 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 |
| 2265 | or destructors unless you explicitly turned them on using <tt>-make_default</tt>. |
| 2266 | However, it appears that most users want to have constructor and destructor functions so it |
| 2267 | has 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> |
| 2274 | Most languages provide a mechanism for creating classes and |
| 2275 | supporting object oriented programming. From a C standpoint, object |
| 2276 | oriented programming really just boils down to the process of |
| 2277 | attaching functions to structures. These functions normally operate |
| 2278 | on an instance of the structure (or object). Although there is a |
| 2279 | natural mapping of C++ to such a scheme, there is no direct mechanism |
| 2280 | for utilizing it with C code. However, SWIG provides a special |
| 2281 | <tt>%extend</tt> directive that makes it possible to attach |
| 2282 | methods to C structures for purposes of building an object oriented |
| 2283 | interface. Suppose you have a C header file with |
| 2284 | the following declaration :</p> |
| 2285 | |
| 2286 | <div class="code"><pre> |
| 2287 | /* file : vector.h */ |
| 2288 | ... |
| 2289 | typedef struct { |
| 2290 | double x,y,z; |
| 2291 | } Vector; |
| 2292 | |
| 2293 | </pre></div> |
| 2294 | |
| 2295 | <p> |
| 2296 | You 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->x = x; |
| 2312 | v->y = y; |
| 2313 | v->z = z; |
| 2314 | return v; |
| 2315 | } |
| 2316 | ~Vector() { |
| 2317 | free(self); |
| 2318 | } |
| 2319 | double magnitude() { |
| 2320 | return sqrt(self->x*self->x+self->y*self->y+self->z*self->z); |
| 2321 | } |
| 2322 | void print() { |
| 2323 | printf("Vector [%g, %g, %g]\n", self->x,self->y,self->z); |
| 2324 | } |
| 2325 | }; |
| 2326 | |
| 2327 | </pre></div> |
| 2328 | |
| 2329 | <p> |
| 2330 | Now, when used with proxy classes in Python, you can do things like |
| 2331 | this :</p> |
| 2332 | |
| 2333 | <div class="targetlang"><pre> |
| 2334 | >>> v = Vector(3,4,0) # Create a new vector |
| 2335 | >>> print v.magnitude() # Print magnitude |
| 2336 | 5.0 |
| 2337 | >>> v.print() # Print it out |
| 2338 | [ 3, 4, 0 ] |
| 2339 | >>> del v # Destroy it |
| 2340 | </pre></div> |
| 2341 | |
| 2342 | <p> |
| 2343 | The <tt>%extend</tt> directive can also be used inside the definition |
| 2344 | of 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 | |
| 2353 | typedef 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> |
| 2364 | Finally, <tt>%extend</tt> can be used to access externally written |
| 2365 | functions provided they follow the naming convention used in this |
| 2366 | example :</p> |
| 2367 | |
| 2368 | <div class="code"><pre> |
| 2369 | /* File : vector.c */ |
| 2370 | /* Vector methods */ |
| 2371 | #include "vector.h" |
| 2372 | Vector *new_Vector(double x, double y, double z) { |
| 2373 | Vector *v; |
| 2374 | v = (Vector *) malloc(sizeof(Vector)); |
| 2375 | v->x = x; |
| 2376 | v->y = y; |
| 2377 | v->z = z; |
| 2378 | return v; |
| 2379 | } |
| 2380 | void delete_Vector(Vector *v) { |
| 2381 | free(v); |
| 2382 | } |
| 2383 | |
| 2384 | double Vector_magnitude(Vector *v) { |
| 2385 | return sqrt(v->x*v->x+v->y*v->y+v->z*v->z); |
| 2386 | } |
| 2387 | |
| 2388 | // File : vector.i |
| 2389 | // Interface file |
| 2390 | %module mymodule |
| 2391 | %{ |
| 2392 | #include "vector.h" |
| 2393 | %} |
| 2394 | |
| 2395 | typedef 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> |
| 2408 | A little known feature of the <tt>%extend</tt> directive is that |
| 2409 | it can also be used to add synthesized attributes or to modify the |
| 2410 | behavior of existing data attributes. For example, suppose you wanted |
| 2411 | to make <tt>magnitude</tt> a read-only attribute of <tt>Vector</tt> |
| 2412 | instead 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 | %{ |
| 2423 | const double Vector_magnitude_get(Vector *v) { |
| 2424 | return (const double) return sqrt(v->x*v->x+v->y*v->y+v->z*v->z); |
| 2425 | } |
| 2426 | %} |
| 2427 | |
| 2428 | </pre> |
| 2429 | </div> |
| 2430 | |
| 2431 | <p> |
| 2432 | Now, for all practial purposes, <tt>magnitude</tt> will appear like an attribute |
| 2433 | of the object. |
| 2434 | </p> |
| 2435 | |
| 2436 | <p> |
| 2437 | A similar technique can also be used to work with problematic data members. |
| 2438 | For example, consider this interface: |
| 2439 | </p> |
| 2440 | |
| 2441 | <div class="code"> |
| 2442 | <pre> |
| 2443 | struct Person { |
| 2444 | char name[50]; |
| 2445 | ... |
| 2446 | } |
| 2447 | </pre> |
| 2448 | </div> |
| 2449 | |
| 2450 | <p> |
| 2451 | By default, the <tt>name</tt> attribute is read-only because SWIG does not |
| 2452 | normally know how to modify arrays. However, you can rewrite the interface |
| 2453 | as follows to change this: |
| 2454 | </p> |
| 2455 | |
| 2456 | <div class="code"> |
| 2457 | <pre> |
| 2458 | struct Person { |
| 2459 | %extend { |
| 2460 | char *name; |
| 2461 | } |
| 2462 | ... |
| 2463 | } |
| 2464 | |
| 2465 | // Specific implementation of set/get functions |
| 2466 | %{ |
| 2467 | char *Person_name_get(Person *p) { |
| 2468 | return p->name; |
| 2469 | } |
| 2470 | void Person_name_set(Person *p, char *val) { |
| 2471 | strncpy(p->name,val,50); |
| 2472 | } |
| 2473 | %} |
| 2474 | </pre> |
| 2475 | </div> |
| 2476 | |
| 2477 | <p> |
| 2478 | Finally, it should be stressed that even though <tt>%extend</tt> |
| 2479 | can be used to add new data members, these new members can not require |
| 2480 | the allocation of additional storage in the object (e.g., their values must |
| 2481 | be 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 |
| 2486 | name for the <tt>%addmethods</tt> directive. Since <tt>%addmethods</tt> could |
| 2487 | be used to extend a structure with more than just methods, a more suitable |
| 2488 | directive name has been chosen. |
| 2489 | </p> |
| 2490 | |
| 2491 | <H3><a name="SWIG_nn38"></a>5.5.7 Nested structures</H3> |
| 2492 | |
| 2493 | |
| 2494 | <p> |
| 2495 | Occasionally, a C program will involve structures like this :</p> |
| 2496 | |
| 2497 | <div class="code"><pre> |
| 2498 | typedef 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> |
| 2511 | When SWIG encounters this, it performs a structure splitting operation |
| 2512 | that transforms the declaration into the equivalent of the |
| 2513 | following:</p> |
| 2514 | |
| 2515 | <div class="code"><pre> |
| 2516 | typedef union { |
| 2517 | int ivalue; |
| 2518 | double dvalue; |
| 2519 | char *strvalue; |
| 2520 | void *ptrvalue; |
| 2521 | } Object_intRep; |
| 2522 | |
| 2523 | typedef struct Object { |
| 2524 | int objType; |
| 2525 | Object_intRep intRep; |
| 2526 | } Object; |
| 2527 | |
| 2528 | </pre></div> |
| 2529 | |
| 2530 | <p> |
| 2531 | SWIG will then create an <tt>Object_intRep</tt> structure for use inside |
| 2532 | the interface file. Accessor functions will be created for both |
| 2533 | structures. In this case, functions like this would be created :</p> |
| 2534 | |
| 2535 | <div class="code"><pre> |
| 2536 | Object_intRep *Object_intRep_get(Object *o) { |
| 2537 | return (Object_intRep *) &o->intRep; |
| 2538 | } |
| 2539 | int Object_intRep_ivalue_get(Object_intRep *o) { |
| 2540 | return o->ivalue; |
| 2541 | } |
| 2542 | int Object_intRep_ivalue_set(Object_intRep *o, int value) { |
| 2543 | return (o->ivalue = value); |
| 2544 | } |
| 2545 | double Object_intRep_dvalue_get(Object_intRep *o) { |
| 2546 | return o->dvalue; |
| 2547 | } |
| 2548 | ... etc ... |
| 2549 | |
| 2550 | </pre></div> |
| 2551 | |
| 2552 | <p> |
| 2553 | Although this process is a little hairy, it works like you would expect in the |
| 2554 | target 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->{intRep}->{ivalue} = 7 # Change value of o.intRep.ivalue |
| 2561 | </pre></div> |
| 2562 | |
| 2563 | <p> |
| 2564 | If you have a lot nested structure declarations, it is |
| 2565 | advisable to double-check them after running SWIG. Although, |
| 2566 | there is a good chance that they will work, you may have to |
| 2567 | modify 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> |
| 2573 | SWIG doesn't care if the declaration of a structure in a <tt>.i</tt> file exactly matches |
| 2574 | that used in the underlying C code (except in the case of nested |
| 2575 | structures). For this reason, there are no problems omitting |
| 2576 | problematic members or simply omitting the structure definition |
| 2577 | altogether. If you are happy passing pointers around, this can |
| 2578 | be done without ever giving SWIG a structure definition.</p> |
| 2579 | |
| 2580 | <p> |
| 2581 | Starting with SWIG1.3, a number of improvements have been made to SWIG's |
| 2582 | code generator. Specifically, even though structure access has been described |
| 2583 | in terms of high-level accessor functions such as this, |
| 2584 | </p> |
| 2585 | |
| 2586 | <div class="code"> |
| 2587 | <pre> |
| 2588 | double Vector_x_get(Vector *v) { |
| 2589 | return v->x; |
| 2590 | } |
| 2591 | </pre> |
| 2592 | </div> |
| 2593 | |
| 2594 | <p> |
| 2595 | most of the generated code is actually inlined directly into wrapper |
| 2596 | functions. Therefore, no function <tt>Vector_x_get()</tt> actually |
| 2597 | exists in the generated wrapper file. For example, when creating a Tcl module, |
| 2598 | the following function is generated instead: |
| 2599 | </p> |
| 2600 | |
| 2601 | <div class="code"> |
| 2602 | <pre> |
| 2603 | static 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 ",&arg0, |
| 2610 | SWIGTYPE_p_Vector) == TCL_ERROR) |
| 2611 | return TCL_ERROR; |
| 2612 | result = (double ) (arg1->x); |
| 2613 | Tcl_SetObjResult(interp,Tcl_NewDoubleObj((double) result)); |
| 2614 | return TCL_OK; |
| 2615 | } |
| 2616 | </pre> |
| 2617 | </div> |
| 2618 | |
| 2619 | <p> |
| 2620 | The only exception to this rule are methods defined with <tt>%extend</tt>. In this |
| 2621 | case, the added code is contained in a separate function. |
| 2622 | </p> |
| 2623 | |
| 2624 | <p> |
| 2625 | Finally, it is important to note that most language modules may choose to |
| 2626 | build a more advanced interface. Although you may never use the low-level |
| 2627 | interface described here, most of SWIG's language modules use it in |
| 2628 | some way or another. |
| 2629 | </p> |
| 2630 | |
| 2631 | <H2><a name="SWIG_nn40"></a>5.6 Code Insertion</H2> |
| 2632 | |
| 2633 | |
| 2634 | <p> |
| 2635 | Sometimes it is necessary to insert special code into the resulting |
| 2636 | wrapper file generated by SWIG. For example, you may want to include |
| 2637 | additional C code to perform initialization or other operations. |
| 2638 | There are four common ways to insert code, but it's useful to know how the |
| 2639 | output 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> |
| 2645 | When SWIG creates its output file, it is broken up into four sections |
| 2646 | corresponding to runtime code, headers, wrapper functions, and module |
| 2647 | initialization code (in that order). |
| 2648 | </p> |
| 2649 | |
| 2650 | <ul> |
| 2651 | <li><b>Runtime code</b>. <br> |
| 2652 | This code is internal to SWIG and is used to include |
| 2653 | type-checking and other support functions that are used by the rest of the module. |
| 2654 | </li> |
| 2655 | |
| 2656 | <li><b>Header section</b>. <br> |
| 2657 | This is user-defined support code that has been included by |
| 2658 | the <tt>%{ ... %}</tt> directive. Usually this consists of header files and |
| 2659 | other helper functions. |
| 2660 | </li> |
| 2661 | |
| 2662 | <li><b>Wrapper code</b>. <br> |
| 2663 | These are the wrappers generated automatically by SWIG. |
| 2664 | </li> |
| 2665 | |
| 2666 | <li><b>Module initialization</b>. <br> |
| 2667 | The function generated by SWIG to initialize |
| 2668 | the 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> |
| 2676 | Code is inserted into the appropriate code section by using one |
| 2677 | of 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> |
| 2701 | The bare <tt>%{ ... %}</tt> directive is a shortcut that is the same as |
| 2702 | <tt>%header %{ ... %}</tt>. |
| 2703 | </p> |
| 2704 | |
| 2705 | <p> |
| 2706 | Everything in a code insertion block is copied verbatim into the output file and is |
| 2707 | not parsed by SWIG. Most SWIG input files have at least one such block to include header |
| 2708 | files and support C code. Additional code blocks may be placed anywhere in a |
| 2709 | SWIG 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 | |
| 2719 | void some_extra_function() { |
| 2720 | ... |
| 2721 | } |
| 2722 | %} |
| 2723 | </pre></div> |
| 2724 | |
| 2725 | <p> |
| 2726 | A common use for code blocks is to write "helper" functions. These |
| 2727 | are functions that are used specifically for the purpose of building |
| 2728 | an interface, but which are generally not visible to the normal C |
| 2729 | program. For example :</p> |
| 2730 | |
| 2731 | <div class="code"><pre> |
| 2732 | %{ |
| 2733 | /* Create a new vector */ |
| 2734 | static Vector *new_Vector() { |
| 2735 | return (Vector *) malloc(sizeof(Vector)); |
| 2736 | } |
| 2737 | |
| 2738 | %} |
| 2739 | // Now wrap it |
| 2740 | Vector *new_Vector(); |
| 2741 | </pre></div> |
| 2742 | |
| 2743 | <H3><a name="SWIG_nn43"></a>5.6.3 Inlined code blocks</H3> |
| 2744 | |
| 2745 | |
| 2746 | <p> |
| 2747 | Since the process of writing helper functions is fairly common, |
| 2748 | there 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 */ |
| 2754 | Vector *new_Vector() { |
| 2755 | return (Vector *) malloc(sizeof(Vector)); |
| 2756 | } |
| 2757 | %} |
| 2758 | |
| 2759 | </pre></div> |
| 2760 | |
| 2761 | <p> |
| 2762 | The <tt>%inline</tt> directive inserts all of the code that follows |
| 2763 | verbatim into the header portion of an interface file. The code is |
| 2764 | then parsed by both the SWIG preprocessor and parser. |
| 2765 | Thus, the above example creates a new command <tt>new_Vector</tt> using only one |
| 2766 | declaration. Since the code inside an <tt>%inline %{ ... %}</tt> block |
| 2767 | is given to both the C compiler and SWIG, it is illegal to include any |
| 2768 | SWIG 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> |
| 2774 | When code is included in the <tt>%init</tt> section, it is copied directly into the |
| 2775 | module initialization function. For example, if you needed to perform some extra |
| 2776 | initialization 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> |
| 2789 | This section describes the general approach for building interface |
| 2790 | with SWIG. The specifics related to a particular scripting language |
| 2791 | are 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> |
| 2797 | SWIG doesn't require modifications to your C code, but if you feed it |
| 2798 | a collection of raw C header files or source code, the results might |
| 2799 | not be what you expect---in fact, they might be awful. Here's a series |
| 2800 | of 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 |
| 2804 | necessary to access every single function in a C program--thus, a |
| 2805 | little forethought can dramatically simplify the resulting scripting |
| 2806 | language interface. C header files are particularly good source for |
| 2807 | finding things to wrap. |
| 2808 | |
| 2809 | <li>Create a new interface file to describe the scripting language |
| 2810 | interface to your program. |
| 2811 | |
| 2812 | <li>Copy the appropriate declarations into the interface file or use |
| 2813 | SWIG's <tt>%include</tt> directive to process an entire C |
| 2814 | source/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 |
| 2819 | type-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> |
| 2828 | Although this may sound complicated, the process turns out to be |
| 2829 | fairly easy once you get the hang of it. |
| 2830 | </p> |
| 2831 | |
| 2832 | <p> |
| 2833 | In the process of building an interface, SWIG may encounter syntax errors or |
| 2834 | other problems. The best way to deal with this is to simply copy the offending |
| 2835 | code into a separate interface file and edit it. However, the SWIG developers |
| 2836 | have worked very hard to improve the SWIG parser--you should report parsing errors |
| 2837 | to 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> |
| 2845 | The preferred method of using SWIG is to generate separate interface |
| 2846 | file. Suppose you have the following C header file :</p> |
| 2847 | |
| 2848 | <div class="code"><pre> |
| 2849 | /* File : header.h */ |
| 2850 | |
| 2851 | #include <stdio.h> |
| 2852 | #include <math.h> |
| 2853 | |
| 2854 | extern int foo(double); |
| 2855 | extern double bar(int, int); |
| 2856 | extern void dump(FILE *f); |
| 2857 | |
| 2858 | </pre></div> |
| 2859 | |
| 2860 | <p> |
| 2861 | A typical SWIG interface file for this header file would look like the |
| 2862 | following :</p> |
| 2863 | |
| 2864 | <div class="code"><pre> |
| 2865 | /* File : interface.i */ |
| 2866 | %module mymodule |
| 2867 | %{ |
| 2868 | #include "header.h" |
| 2869 | %} |
| 2870 | extern int foo(double); |
| 2871 | extern double bar(int, int); |
| 2872 | extern void dump(FILE *f); |
| 2873 | |
| 2874 | </pre></div> |
| 2875 | |
| 2876 | <p> |
| 2877 | Of course, in this case, our header file is pretty simple so we could |
| 2878 | have 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> |
| 2887 | Naturally, 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> |
| 2893 | Although SWIG can parse many header files, it is more common to write a |
| 2894 | special <tt>.i</tt> file defining the interface to a package. There |
| 2895 | are 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 |
| 2900 | package. Many C functions might have little or no use in a scripted |
| 2901 | environment. Therfore, why wrap them? |
| 2902 | |
| 2903 | <li>Separate interface files provide an opportunity to provide more |
| 2904 | precise 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 |
| 2909 | files. Having a separate file allows you to eliminate or work around |
| 2910 | these problems. |
| 2911 | |
| 2912 | <li>Interface files provide a more precise definition of what the interface |
| 2913 | is. Users wanting to extend the system can go to the interface file |
| 2914 | and immediately see what is available without having to dig it out of |
| 2915 | header files. |
| 2916 | </ul> |
| 2917 | |
| 2918 | <H3><a name="SWIG_nn49"></a>5.7.4 Getting the right header files</H3> |
| 2919 | |
| 2920 | |
| 2921 | <p> |
| 2922 | Sometimes, it is necessary to use certain header files in order for |
| 2923 | the code generated by SWIG to compile properly. Make sure you |
| 2924 | include 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 <GL/gl.h> |
| 2931 | #include <GL/glu.h> |
| 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> |
| 2942 | If your program defines a <tt>main()</tt> function, you may need to |
| 2943 | get rid of it or rename it in order to use a scripting language. Most |
| 2944 | scripting languages define their own <tt>main()</tt> procedure that |
| 2945 | is called instead. <tt>main()</tt> also makes no sense when |
| 2946 | working with dynamic loading. There are a few approaches to solving |
| 2947 | the <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 |
| 2953 | compiling your C program with an option like <tt>-Dmain=oldmain</tt>. |
| 2954 | |
| 2955 | <li>Use conditional compilation to only include <tt>main()</tt> when |
| 2956 | not using a scripting language. |
| 2957 | </ul> |
| 2958 | |
| 2959 | <p> |
| 2960 | Getting rid of <tt>main()</tt> may cause potential initialization |
| 2961 | problems of a program. To handle this problem, you may consider |
| 2962 | writing a special function called <tt>program_init()</tt> that |
| 2963 | initializes your program upon startup. This function could then be |
| 2964 | called either from the scripting language as the first operation, or |
| 2965 | when the SWIG generated module is loaded.</p> |
| 2966 | |
| 2967 | <p> |
| 2968 | As a general note, many C programs only use the <tt>main()</tt> |
| 2969 | function to parse command line options and to set parameters. However, |
| 2970 | by using a scripting language, you are probably trying to create a |
| 2971 | program that is more interactive. In many cases, the old |
| 2972 | <tt>main()</tt> program can be completely replaced by a Perl, Python, |
| 2973 | or Tcl script.</p> |
| 2974 | |
| 2975 | <p> |
| 2976 | <b>Note:</b> If some cases, you might be inclined to create a |
| 2977 | scripting language wrapper for <tt>main()</tt>. If you do this, the |
| 2978 | compilation will probably work and your module might even load |
| 2979 | correctly. 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 |
| 2982 | is a side effect of the symbol binding mechanism used in the dynamic linker. |
| 2983 | The bottom line: don't do this. |
| 2984 | </p> |
| 2985 | |
| 2986 | </body> |
| 2987 | </html> |