PageRenderTime 64ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-2.0.4/Doc/Manual/SWIG.html

#
HTML | 2036 lines | 1737 code | 295 blank | 4 comment | 0 complexity | 31dc4ae9bbd484a39b2e2bc326506eb9 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  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. <body bgcolor="#ffffff">
  8. <H1><a name="SWIG"></a>5 SWIG Basics</H1>
  9. <!-- INDEX -->
  10. <div class="sectiontoc">
  11. <ul>
  12. <li><a href="#SWIG_nn2">Running SWIG</a>
  13. <ul>
  14. <li><a href="#SWIG_nn3">Input format</a>
  15. <li><a href="#SWIG_output">SWIG Output</a>
  16. <li><a href="#SWIG_nn5">Comments</a>
  17. <li><a href="#SWIG_nn6">C Preprocessor</a>
  18. <li><a href="#SWIG_nn7">SWIG Directives</a>
  19. <li><a href="#SWIG_nn8">Parser Limitations</a>
  20. </ul>
  21. <li><a href="#SWIG_nn9">Wrapping Simple C Declarations</a>
  22. <ul>
  23. <li><a href="#SWIG_nn10">Basic Type Handling</a>
  24. <li><a href="#SWIG_nn11">Global Variables</a>
  25. <li><a href="#SWIG_nn12">Constants</a>
  26. <li><a href="#SWIG_nn13">A brief word about <tt>const</tt></a>
  27. <li><a href="#SWIG_nn14">A cautionary tale of <tt>char *</tt></a>
  28. </ul>
  29. <li><a href="#SWIG_nn15">Pointers and complex objects</a>
  30. <ul>
  31. <li><a href="#SWIG_nn16">Simple pointers</a>
  32. <li><a href="#SWIG_nn17">Run time pointer type checking</a>
  33. <li><a href="#SWIG_nn18">Derived types, structs, and classes</a>
  34. <li><a href="#SWIG_nn19">Undefined datatypes</a>
  35. <li><a href="#SWIG_nn20">Typedef</a>
  36. </ul>
  37. <li><a href="#SWIG_nn21">Other Practicalities</a>
  38. <ul>
  39. <li><a href="#SWIG_nn22">Passing structures by value</a>
  40. <li><a href="#SWIG_nn23">Return by value</a>
  41. <li><a href="#SWIG_nn24">Linking to structure variables</a>
  42. <li><a href="#SWIG_nn25">Linking to <tt>char *</tt></a>
  43. <li><a href="#SWIG_nn26">Arrays</a>
  44. <li><a href="#SWIG_readonly_variables">Creating read-only variables</a>
  45. <li><a href="#SWIG_rename_ignore">Renaming and ignoring declarations</a>
  46. <ul>
  47. <li><a href="#SWIG_nn29">Simple renaming of specific identifiers</a>
  48. <li><a href="#SWIG_advanced_renaming">Advanced renaming support</a>
  49. <li><a href="#SWIG_limiting_renaming">Limiting global renaming rules</a>
  50. </ul>
  51. <li><a href="#SWIG_default_args">Default/optional arguments</a>
  52. <li><a href="#SWIG_nn30">Pointers to functions and callbacks</a>
  53. </ul>
  54. <li><a href="#SWIG_nn31">Structures and unions</a>
  55. <ul>
  56. <li><a href="#SWIG_nn32">Typedef and structures</a>
  57. <li><a href="#SWIG_nn33">Character strings and structures</a>
  58. <li><a href="#SWIG_nn34">Array members</a>
  59. <li><a href="#SWIG_structure_data_members">Structure data members</a>
  60. <li><a href="#SWIG_nn36">C constructors and destructors</a>
  61. <li><a href="#SWIG_adding_member_functions">Adding member functions to C structures</a>
  62. <li><a href="#SWIG_nested_structs">Nested structures</a>
  63. <li><a href="#SWIG_nn39">Other things to note about structure wrapping</a>
  64. </ul>
  65. <li><a href="#SWIG_nn40">Code Insertion</a>
  66. <ul>
  67. <li><a href="#SWIG_nn41">The output of SWIG</a>
  68. <li><a href="#SWIG_nn42">Code insertion blocks</a>
  69. <li><a href="#SWIG_nn43">Inlined code blocks</a>
  70. <li><a href="#SWIG_nn44">Initialization blocks</a>
  71. </ul>
  72. <li><a href="#SWIG_nn45">An Interface Building Strategy</a>
  73. <ul>
  74. <li><a href="#SWIG_nn46">Preparing a C program for SWIG</a>
  75. <li><a href="#SWIG_nn47">The SWIG interface file</a>
  76. <li><a href="#SWIG_nn48">Why use separate interface files?</a>
  77. <li><a href="#SWIG_nn49">Getting the right header files</a>
  78. <li><a href="#SWIG_nn50">What to do with main()</a>
  79. </ul>
  80. </ul>
  81. </div>
  82. <!-- INDEX -->
  83. <p>
  84. This chapter describes the basic operation of SWIG, the structure of its
  85. input files, and how it handles standard ANSI C declarations. C++ support is
  86. described in the next chapter. However, C++ programmers should still read this
  87. chapter to understand the basics.
  88. Specific details about each target language are described in later
  89. chapters.
  90. </p>
  91. <H2><a name="SWIG_nn2"></a>5.1 Running SWIG</H2>
  92. <p>
  93. To run SWIG, use the <tt>swig</tt> command with options and a filename like this:
  94. </p>
  95. <div class="shell"><pre>
  96. swig [ <em>options</em> ] filename
  97. </pre></div>
  98. <p>
  99. where <tt>filename</tt> is a SWIG interface file or a C/C++ header file.
  100. Below is a subset of <em>options</em> that can be used.
  101. Additional options are also defined for each target language. A full list
  102. can be obtained by typing <tt>swig -help</tt> or <tt>swig
  103. -<em>lang</em> -help</tt>.
  104. </p>
  105. <div class="shell"><pre>
  106. -allegrocl Generate ALLEGROCL wrappers
  107. -chicken Generate CHICKEN wrappers
  108. -clisp Generate CLISP wrappers
  109. -cffi Generate CFFI wrappers
  110. -csharp Generate C# wrappers
  111. -go Generate Go wrappers
  112. -guile Generate Guile wrappers
  113. -java Generate Java wrappers
  114. -lua Generate Lua wrappers
  115. -modula3 Generate Modula 3 wrappers
  116. -mzscheme Generate Mzscheme wrappers
  117. -ocaml Generate Ocaml wrappers
  118. -perl Generate Perl wrappers
  119. -php Generate PHP wrappers
  120. -pike Generate Pike wrappers
  121. -python Generate Python wrappers
  122. -r Generate R (aka GNU S) wrappers
  123. -ruby Generate Ruby wrappers
  124. -sexp Generate Lisp S-Expressions wrappers
  125. -tcl Generate Tcl wrappers
  126. -uffi Generate Common Lisp / UFFI wrappers
  127. -xml Generate XML wrappers
  128. -c++ Enable C++ parsing
  129. -D<em>symbol</em> Define a preprocessor symbol
  130. -Fstandard Display error/warning messages in commonly used format
  131. -Fmicrosoft Display error/warning messages in Microsoft format
  132. -help Display all options
  133. -I<em>dir</em> Add a directory to the file include path
  134. -l<em>file</em> Include a SWIG library file.
  135. -module <em>name</em> Set the name of the SWIG module
  136. -o <em>outfile</em> Name of output file
  137. -outcurrentdir Set default output dir to current dir instead of input file's path
  138. -outdir <em>dir</em> Set language specific files output directory
  139. -pcreversion Display PCRE version information
  140. -swiglib Show location of SWIG library
  141. -version Show SWIG version number
  142. </pre></div>
  143. <H3><a name="SWIG_nn3"></a>5.1.1 Input format</H3>
  144. <p>
  145. As input, SWIG expects a file containing ANSI C/C++ declarations and
  146. special SWIG directives. More often than not, this is a special SWIG
  147. interface file which is usually denoted with a special <tt>.i</tt> or
  148. <tt>.swg</tt> suffix. In certain cases, SWIG can be used directly on
  149. raw header files or source files. However, this is not the most
  150. typical case and there are several reasons why you might not want to
  151. do this (described later).
  152. </p>
  153. <p>
  154. The most common format of a SWIG interface is as follows:
  155. </p>
  156. <div class="code"><pre>
  157. %module mymodule
  158. %{
  159. #include "myheader.h"
  160. %}
  161. // Now list ANSI C/C++ declarations
  162. int foo;
  163. int bar(int x);
  164. ...
  165. </pre></div>
  166. <p>
  167. The module name is supplied using the special <tt>%module</tt>
  168. directive. Modules are described further in the <a href="Modules.html#Modules_introduction">Modules Introduction</a> section.
  169. </p>
  170. <p>
  171. Everything in the <tt>%{ ... %}</tt> block is simply copied verbatim
  172. to the resulting wrapper file created by SWIG. This section is almost
  173. always used to include header files and other declarations that are
  174. required to make the generated wrapper code compile. It is important
  175. to emphasize that just because you include a declaration in a SWIG
  176. input file, that declaration does <em>not</em> automatically appear in
  177. the generated wrapper code---therefore you need to make sure you
  178. include the proper header files in the <tt>%{ ... %}</tt> section. It
  179. should be noted that the text enclosed in <tt>%{ ... %}</tt> is not
  180. parsed or interpreted by SWIG. The <tt>%{...%}</tt> syntax and
  181. semantics in SWIG is analogous to that of the declarations section
  182. used in input files to parser generation tools such as yacc or bison.
  183. </p>
  184. <H3><a name="SWIG_output"></a>5.1.2 SWIG Output</H3>
  185. <p>
  186. The output of SWIG is a C/C++ file that contains all of the wrapper
  187. code needed to build an extension module. SWIG may generate some
  188. additional files depending on the target language. By default, an input file
  189. with the name <tt>file.i</tt> is transformed into a file
  190. <tt>file_wrap.c</tt> or <tt>file_wrap.cxx</tt> (depending on whether
  191. or not the <tt>-c++</tt> option has been used). The name of the
  192. output file can be changed using the <tt>-o</tt> option. In certain
  193. cases, file suffixes are used by the compiler to determine the source
  194. language (C, C++, etc.). Therefore, you have to use the
  195. <tt>-o</tt> option to change the suffix of the SWIG-generated wrapper
  196. file if you want something different than the default. For example:
  197. </p>
  198. <div class="shell"><pre>
  199. $ swig -c++ -python -o example_wrap.cpp example.i
  200. </pre></div>
  201. <p>
  202. The C/C++ output file created by SWIG often
  203. contains everything that is needed to construct a extension module
  204. for the target scripting language. SWIG is not a stub compiler nor is it
  205. usually necessary to edit the output file (and if you look at the output,
  206. you probably won't want to). To build the final extension module, the
  207. SWIG output file is compiled and linked with the rest of your C/C++
  208. program to create a shared library.
  209. </p>
  210. <p>
  211. Many target languages will also generate proxy class files in the
  212. target language. The default output directory for these language
  213. specific files is the same directory as the generated C/C++ file. This
  214. can be modified using the <tt>-outdir</tt> option. For example:
  215. </p>
  216. <div class="shell"><pre>
  217. $ swig -c++ -python -outdir pyfiles -o cppfiles/example_wrap.cpp example.i
  218. </pre></div>
  219. <p>
  220. If the directories <tt>cppfiles</tt> and <tt>pyfiles</tt> exist, the following
  221. will be generated:</p>
  222. <div class="shell"><pre>
  223. cppfiles/example_wrap.cpp
  224. pyfiles/example.py
  225. </pre></div>
  226. <p>
  227. If the <tt>-outcurrentdir</tt> option is used (without <tt>-o</tt>)
  228. then SWIG behaves like a typical C/C++
  229. compiler and the default output directory is then the current directory. Without
  230. this option the default output directory is the path to the input file.
  231. If <tt>-o</tt> and
  232. <tt>-outcurrentdir</tt> are used together, <tt>-outcurrentdir</tt> is effectively ignored
  233. as the output directory for the language files is the same directory as the
  234. generated C/C++ file if not overidden with <tt>-outdir</tt>.
  235. </p>
  236. <H3><a name="SWIG_nn5"></a>5.1.3 Comments</H3>
  237. <p>
  238. C and C++ style comments may appear anywhere in interface files. In
  239. previous versions of SWIG, comments were used to generate
  240. documentation files. However, this feature is currently under repair
  241. and will reappear in a later SWIG release.
  242. </p>
  243. <H3><a name="SWIG_nn6"></a>5.1.4 C Preprocessor</H3>
  244. <p>
  245. Like C, SWIG preprocesses all input files through an enhanced version
  246. of the C preprocessor. All standard preprocessor features are
  247. supported including file inclusion, conditional compilation and
  248. macros. However, <tt>#include</tt> statements are ignored unless the
  249. <tt>-includeall</tt> command line option has been supplied. The
  250. reason for disabling includes is that SWIG is sometimes used to
  251. process raw C header files. In this case, you usually only want the
  252. extension module to include functions in the supplied header file
  253. rather than everything that might be included by that header file
  254. (i.e., system headers, C library functions, etc.).
  255. </p>
  256. <p>
  257. It should also be noted that the SWIG preprocessor skips all text
  258. enclosed inside a <tt>%{...%}</tt> block. In addition, the
  259. preprocessor includes a number of macro handling enhancements that
  260. make it more powerful than the normal C preprocessor. These
  261. extensions are described in the "<a href="Preprocessor.html#Preprocessor">Preprocessor</a>" chapter.
  262. </p>
  263. <H3><a name="SWIG_nn7"></a>5.1.5 SWIG Directives</H3>
  264. <p>
  265. Most of SWIG's operation is controlled by special directives that are
  266. always preceded by a "<tt>%</tt>" to distinguish them from normal C
  267. declarations. These directives are used to give SWIG hints or to alter
  268. SWIG's parsing behavior in some manner.
  269. </p>
  270. <p>
  271. Since SWIG directives are not legal C syntax, it is generally not
  272. possible to include them in header files. However, SWIG directives can be
  273. included in C header files using conditional compilation like this:
  274. </p>
  275. <div class="code"><pre>
  276. /* header.h --- Some header file */
  277. /* SWIG directives -- only seen if SWIG is running */
  278. #ifdef SWIG
  279. %module foo
  280. #endif
  281. </pre>
  282. </div>
  283. <p>
  284. <tt>SWIG</tt> is a special preprocessing symbol defined by SWIG when
  285. it is parsing an input file.
  286. </p>
  287. <H3><a name="SWIG_nn8"></a>5.1.6 Parser Limitations</H3>
  288. <p>
  289. Although SWIG can parse most C/C++ declarations, it does not
  290. provide a complete C/C++ parser implementation. Most of these
  291. limitations pertain to very complicated type declarations and certain
  292. advanced C++ features. Specifically, the following features are not
  293. currently supported:
  294. </p>
  295. <ul>
  296. <li>
  297. <p>
  298. Non-conventional type declarations.
  299. For example, SWIG does not support declarations such as the following
  300. (even though this is legal C):
  301. </p>
  302. <div class="code">
  303. <pre>
  304. /* Non-conventional placement of storage specifier (extern) */
  305. const int extern Number;
  306. /* Extra declarator grouping */
  307. Matrix (foo); // A global variable
  308. /* Extra declarator grouping in parameters */
  309. void bar(Spam (Grok)(Doh));
  310. </pre>
  311. </div>
  312. <p>
  313. In practice, few (if any) C programmers actually write code like
  314. this since this style is never featured in programming books. However,
  315. if you're feeling particularly obfuscated, you can certainly break SWIG (although why would you want to?).
  316. </p>
  317. </li>
  318. <li>
  319. <p>
  320. Running SWIG on C++ source files (the code in a .C, .cpp or .cxx file) is not recommended.
  321. The usual approach is to feed SWIG header files for parsing C++ definitions and declarations.
  322. The main reason is if SWIG parses a scoped definition or declaration (as is normal for C++ source files),
  323. it is ignored, unless a declaration for the symbol was parsed earlier.
  324. For example
  325. </p>
  326. <div class="code">
  327. <pre>
  328. /* bar not wrapped unless foo has been defined and
  329. the declaration of bar within foo has already been parsed */
  330. int foo::bar(int) {
  331. ... whatever ...
  332. }
  333. </pre>
  334. </div>
  335. </li>
  336. <li>
  337. <p>
  338. Certain advanced features of C++ such as nested classes
  339. are not yet fully supported. Please see the C++ <a href="SWIGPlus.html#SWIGPlus_nested_classes">Nested classes</a> section
  340. for more information.
  341. </p>
  342. </ul>
  343. <p>
  344. In the event of a parsing error, conditional compilation can be used to skip
  345. offending code. For example:
  346. </p>
  347. <div class="code">
  348. <pre>
  349. #ifndef SWIG
  350. ... some bad declarations ...
  351. #endif
  352. </pre>
  353. </div>
  354. <p>
  355. Alternatively, you can just delete the offending code from the interface file.
  356. </p>
  357. <p>
  358. One of the reasons why SWIG does not provide a full C++ parser
  359. implementation is that it has been designed to work with incomplete
  360. specifications and to be very permissive in its handling of C/C++
  361. datatypes (e.g., SWIG can generate interfaces even when there are
  362. missing class declarations or opaque datatypes). Unfortunately, this
  363. approach makes it extremely difficult to implement certain parts of a
  364. C/C++ parser as most compilers use type information to assist in the
  365. parsing of more complex declarations (for the truly curious, the
  366. primary complication in the implementation is that the SWIG parser
  367. does not utilize a separate <em>typedef-name</em> terminal symbol as
  368. described on p. 234 of K&amp;R).
  369. </p>
  370. <H2><a name="SWIG_nn9"></a>5.2 Wrapping Simple C Declarations</H2>
  371. <p>
  372. SWIG wraps simple C declarations by creating an interface that closely matches
  373. the way in which the declarations would be used in a C program.
  374. For example, consider the following interface file:
  375. </p>
  376. <div class="code"><pre>
  377. %module example
  378. %inline %{
  379. extern double sin(double x);
  380. extern int strcmp(const char *, const char *);
  381. extern int Foo;
  382. %}
  383. #define STATUS 50
  384. #define VERSION "1.1"
  385. </pre></div>
  386. <p>
  387. In this file, there are two functions <tt>sin()</tt> and <tt>strcmp()</tt>,
  388. a global variable <tt>Foo</tt>, and two constants <tt>STATUS</tt> and
  389. <tt>VERSION</tt>. When SWIG creates an extension module, these
  390. declarations are accessible as scripting language functions, variables, and
  391. constants respectively. For example, in Tcl:
  392. </p>
  393. <div class="targetlang"><pre>
  394. % sin 3
  395. 5.2335956
  396. % strcmp Dave Mike
  397. -1
  398. % puts $Foo
  399. 42
  400. % puts $STATUS
  401. 50
  402. % puts $VERSION
  403. 1.1
  404. </pre></div>
  405. <p>
  406. Or in Python:
  407. </p>
  408. <div class="targetlang"><pre>
  409. &gt;&gt;&gt; example.sin(3)
  410. 5.2335956
  411. &gt;&gt;&gt; example.strcmp('Dave','Mike')
  412. -1
  413. &gt;&gt;&gt; print example.cvar.Foo
  414. 42
  415. &gt;&gt;&gt; print example.STATUS
  416. 50
  417. &gt;&gt;&gt; print example.VERSION
  418. 1.1
  419. </pre></div>
  420. <p>
  421. Whenever possible, SWIG creates an interface that closely matches the underlying C/C++
  422. code. However, due to subtle differences between languages, run-time
  423. environments, and semantics, it is not always possible to do so. The
  424. next few sections describes various aspects of this mapping.
  425. </p>
  426. <H3><a name="SWIG_nn10"></a>5.2.1 Basic Type Handling</H3>
  427. <p>
  428. In order to build an interface, SWIG has to convert C/C++ datatypes to
  429. equivalent types in the target language. Generally,
  430. scripting languages provide a more limited set of primitive types than C.
  431. Therefore, this conversion process involves a certain amount of type
  432. coercion.
  433. </p>
  434. <p>
  435. Most scripting languages provide a single integer type that is implemented using
  436. the <tt>int</tt> or <tt>long</tt> datatype in C. The following list shows
  437. all of the C datatypes that SWIG will convert to and from integers in the target language:
  438. </p>
  439. <div class="code"><pre>
  440. int
  441. short
  442. long
  443. unsigned
  444. signed
  445. unsigned short
  446. unsigned long
  447. unsigned char
  448. signed char
  449. bool
  450. </pre></div>
  451. <p>
  452. When an integral value is converted from C, a cast is used to convert it to
  453. the representation in the target language.
  454. Thus, a 16 bit short in C may be promoted to a 32 bit integer. When integers are
  455. converted in the other direction, the value is cast back into the original C type.
  456. If the value is too large to fit, it is silently truncated.
  457. <!-- Dave: Maybe we should fix this -->
  458. </p>
  459. <p>
  460. <tt>unsigned char</tt> and <tt>signed char</tt> are special cases that
  461. are handled as small 8-bit integers. Normally, the <tt>char</tt>
  462. datatype is mapped as a one-character ASCII string. </p>
  463. <p>
  464. The <tt>bool</tt> datatype is cast to and from an integer value of 0
  465. and 1 unless the target language provides a special boolean type.</p>
  466. <p>
  467. Some care is required when working with large integer values. Most
  468. scripting languages use 32-bit integers so mapping a 64-bit long
  469. integer may lead to truncation errors. Similar problems may arise with
  470. 32 bit unsigned integers (which may appear as large negative
  471. numbers). As a rule of thumb, the <tt>int</tt> datatype and all
  472. variations of <tt>char</tt> and <tt>short</tt> datatypes are safe to
  473. use. For <tt>unsigned int</tt> and <tt>long</tt> datatypes, you will
  474. need to carefully check the correct operation of your program after
  475. it has been wrapped with SWIG.
  476. </p>
  477. <p>
  478. Although the SWIG parser supports the <tt>long long</tt> datatype, not
  479. all language modules support it. This is because <tt>long long</tt>
  480. usually exceeds the integer precision available in the target
  481. language. In certain modules such as Tcl and Perl5, <tt>long
  482. long</tt> integers are encoded as strings. This allows the full range
  483. of these numbers to be represented. However, it does not allow
  484. <tt>long long</tt> values to be used in arithmetic expressions. It
  485. should also be noted that although <tt>long long</tt> is part
  486. of the ISO C99 standard, it is not universally supported by all C
  487. compilers. Make sure you are using a compiler that supports <tt>long
  488. long</tt> before trying to use this type with SWIG.
  489. </p>
  490. <p>
  491. SWIG recognizes the following floating point types :</p>
  492. <div class="code"><pre>
  493. float
  494. double
  495. </pre></div>
  496. <p>
  497. Floating point numbers are mapped to and from the natural
  498. representation of floats in the target language. This is almost always
  499. a C <tt>double</tt>. The rarely used datatype of <tt>long double</tt>
  500. is not supported by SWIG.</p>
  501. <p>
  502. The <tt>char</tt> datatype is mapped into a NULL terminated ASCII
  503. string with a single character. When used in a scripting language it
  504. shows up as a tiny string containing the character value. When
  505. converting the value back into C, SWIG takes a character string
  506. from the scripting language and strips off the first character as the
  507. char value. Thus if the value "foo" is assigned to a
  508. <tt>char</tt> datatype, it gets the value `f'.</p>
  509. <p>
  510. The <tt>char *</tt> datatype is handled as a NULL-terminated ASCII
  511. string. SWIG maps this into a 8-bit character string in the target
  512. scripting language. SWIG converts character strings in the target
  513. language to NULL terminated strings before passing them into
  514. C/C++. The default handling of these strings does not allow them to
  515. have embedded NULL bytes. Therefore, the <tt>char *</tt> datatype is
  516. not generally suitable for passing binary data. However, it is
  517. possible to change this behavior by defining a SWIG typemap. See the chapter
  518. on <a href="Typemaps.html#Typemaps">Typemaps</a> for details about this.
  519. </p>
  520. <p>
  521. At this time, SWIG provides limited support for Unicode and
  522. wide-character strings (the C <tt>wchar_t</tt> type).
  523. Some languages provide typemaps for wchar_t, but bear in mind these
  524. might not be portable across different operating systems. This is a
  525. delicate topic that is poorly understood by many programmers and not
  526. implemented in a consistent manner across languages. For those
  527. scripting languages that provide Unicode support, Unicode strings are
  528. often available in an 8-bit representation such as UTF-8 that can be
  529. mapped to the <tt>char *</tt> type (in which case the SWIG interface
  530. will probably work). If the program you are wrapping uses Unicode,
  531. there is no guarantee that Unicode characters in the target language
  532. will use the same internal representation (e.g., UCS-2 vs. UCS-4).
  533. You may need to write some special conversion functions.
  534. </p>
  535. <H3><a name="SWIG_nn11"></a>5.2.2 Global Variables</H3>
  536. <p>
  537. Whenever possible, SWIG maps C/C++ global variables into scripting language
  538. variables. For example,
  539. </p>
  540. <div class="code"><pre>
  541. %module example
  542. double foo;
  543. </pre></div>
  544. <p>
  545. results in a scripting language variable like this:
  546. </p>
  547. <div class="code"><pre>
  548. # Tcl
  549. set foo [3.5] ;# Set foo to 3.5
  550. puts $foo ;# Print the value of foo
  551. # Python
  552. cvar.foo = 3.5 # Set foo to 3.5
  553. print cvar.foo # Print value of foo
  554. # Perl
  555. $foo = 3.5; # Set foo to 3.5
  556. print $foo,"\n"; # Print value of foo
  557. # Ruby
  558. Module.foo = 3.5 # Set foo to 3.5
  559. print Module.foo, "\n" # Print value of foo
  560. </pre></div>
  561. <p>
  562. Whenever the scripting language variable is used, the underlying C
  563. global variable is accessed. Although SWIG makes every
  564. attempt to make global variables work like scripting language
  565. variables, it is not always possible to do so. For instance, in
  566. Python, all global variables must be accessed through a special
  567. variable object known as <tt>cvar</tt> (shown above). In Ruby, variables are
  568. accessed as attributes of the module. Other languages may
  569. convert variables to a pair of accessor functions. For example, the
  570. Java module generates a pair of functions <tt>double get_foo()</tt>
  571. and <tt>set_foo(double val)</tt> that are used to manipulate the
  572. value.
  573. </p>
  574. <p>
  575. Finally, if a global variable has been declared as <tt>const</tt>, it
  576. only supports read-only access. Note: this behavior is new to SWIG-1.3.
  577. Earlier versions of SWIG incorrectly handled <tt>const</tt> and created
  578. constants instead.
  579. </p>
  580. <H3><a name="SWIG_nn12"></a>5.2.3 Constants</H3>
  581. <p>
  582. Constants can be created using <tt>#define</tt>, enumerations,
  583. or a special <tt>%constant</tt> directive. The following
  584. interface file shows a few valid constant declarations :</p>
  585. <div class="code"><pre>
  586. #define I_CONST 5 // An integer constant
  587. #define PI 3.14159 // A Floating point constant
  588. #define S_CONST "hello world" // A string constant
  589. #define NEWLINE '\n' // Character constant
  590. enum boolean {NO=0, YES=1};
  591. enum months {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG,
  592. SEP, OCT, NOV, DEC};
  593. %constant double BLAH = 42.37;
  594. #define PI_4 PI/4
  595. #define FLAGS 0x04 | 0x08 | 0x40
  596. </pre></div>
  597. <p>
  598. In <tt>#define</tt> declarations, the type of a constant is inferred
  599. by syntax. For example, a number with a decimal point is assumed to be
  600. floating point. In addition, SWIG must be able to fully resolve all
  601. of the symbols used in a <tt>#define</tt> in order for a constant to
  602. actually be created. This restriction is necessary because
  603. <tt>#define</tt> is also used to define preprocessor macros that are
  604. definitely not meant to be part of the scripting language interface.
  605. For example:
  606. </p>
  607. <div class="code">
  608. <pre>
  609. #define EXTERN extern
  610. EXTERN void foo();
  611. </pre>
  612. </div>
  613. <p>
  614. In this case, you probably don't want to create a constant called
  615. <tt>EXTERN</tt> (what would the value be?). In general,
  616. SWIG will not create constants for macros unless the value can
  617. be completely determined by the preprocessor. For instance, in the above example,
  618. the declaration
  619. </p>
  620. <div class="code">
  621. <pre>
  622. #define PI_4 PI/4
  623. </pre>
  624. </div>
  625. <p>
  626. defines a constant because <tt>PI</tt> was already defined as a
  627. constant and the value is known.
  628. However, for the same conservative reasons even a constant with a simple cast will be ignored, such as
  629. </p>
  630. <div class="code">
  631. <pre>
  632. #define F_CONST (double) 5 // A floating pointer constant with cast
  633. </pre>
  634. </div>
  635. <p>
  636. The use of constant expressions is allowed, but SWIG does not evaluate
  637. them. Rather, it passes them through to the output file and lets the C
  638. compiler perform the final evaluation (SWIG does perform a limited
  639. form of type-checking however).</p>
  640. <p>
  641. For enumerations, it is critical that the original enum definition be
  642. included somewhere in the interface file (either in a header file or
  643. in the <tt>%{,%}</tt> block). SWIG only translates the enumeration
  644. into code needed to add the constants to a scripting language. It
  645. needs the original enumeration declaration in order to get the correct
  646. enum values as assigned by the C compiler.
  647. </p>
  648. <p>
  649. The <tt>%constant</tt> directive is used to more precisely create
  650. constants corresponding to different C datatypes. Although it is not
  651. usually not needed for simple values, it is more useful when working
  652. with pointers and other more complex datatypes. Typically, <tt>%constant</tt>
  653. is only used when you want to add constants to the scripting language
  654. interface that are not defined in the original header file.
  655. </p>
  656. <H3><a name="SWIG_nn13"></a>5.2.4 A brief word about <tt>const</tt></H3>
  657. <p>
  658. A common confusion with C programming is the semantic meaning of the
  659. <tt>const</tt> qualifier in declarations--especially when it is mixed
  660. with pointers and other type modifiers. In fact, previous versions of SWIG
  661. handled <tt>const</tt> incorrectly--a situation that SWIG-1.3.7 and newer
  662. releases have fixed.
  663. </p>
  664. <p>
  665. Starting with SWIG-1.3, all variable declarations, regardless of any
  666. use of <tt>const</tt>, are wrapped as global variables. If a
  667. declaration happens to be declared as <tt>const</tt>, it is wrapped as
  668. a read-only variable. To tell if a variable is <tt>const</tt> or not,
  669. you need to look at the right-most occurrence of the <tt>const</tt>
  670. qualifier (that appears before the variable name). If the right-most
  671. <tt>const</tt> occurs after all other type modifiers (such as
  672. pointers), then the variable is <tt>const</tt>. Otherwise, it is not.
  673. </p>
  674. <p>
  675. Here are some examples of <tt>const</tt> declarations.
  676. </p>
  677. <div class="code">
  678. <pre>
  679. const char a; // A constant character
  680. char const b; // A constant character (the same)
  681. char *const c; // A constant pointer to a character
  682. const char *const d; // A constant pointer to a constant character
  683. </pre>
  684. </div>
  685. <p>
  686. Here is an example of a declaration that is not <tt>const</tt>:
  687. </p>
  688. <div class="code">
  689. <pre>
  690. const char *e; // A pointer to a constant character. The pointer
  691. // may be modified.
  692. </pre>
  693. </div>
  694. <p>
  695. In this case, the pointer <tt>e</tt> can change---it's only the value
  696. being pointed to that is read-only.
  697. </p>
  698. <p>
  699. Please note that for const parameters or return types used in a function, SWIG pretty much ignores
  700. the fact that these are const, see the section on <a href="SWIGPlus.html#SWIGPlus_const">const-correctness</a>
  701. for more information.
  702. </p>
  703. <p>
  704. <b>Compatibility Note:</b> One reason for changing SWIG to handle
  705. <tt>const</tt> declarations as read-only variables is that there are
  706. many situations where the value of a <tt>const</tt> variable might
  707. change. For example, a library might export a symbol as
  708. <tt>const</tt> in its public API to discourage modification, but still
  709. allow the value to change through some other kind of internal
  710. mechanism. Furthermore, programmers often overlook the fact that with
  711. a constant declaration like <tt>char *const</tt>, the underlying data
  712. being pointed to can be modified--it's only the pointer itself that is
  713. constant. In an embedded system, a <tt>const</tt> declaration might
  714. refer to a read-only memory address such as the location of a
  715. memory-mapped I/O device port (where the value changes, but writing to
  716. the port is not supported by the hardware). Rather than trying to
  717. build a bunch of special cases into the <tt>const</tt> qualifier, the
  718. new interpretation of <tt>const</tt> as "read-only" is simple and
  719. exactly matches the actual semantics of <tt>const</tt> in C/C++. If
  720. you really want to create a constant as in older versions of SWIG, use
  721. the <tt>%constant</tt> directive instead. For example:
  722. </p>
  723. <div class="code">
  724. <pre>
  725. %constant double PI = 3.14159;
  726. </pre>
  727. </div>
  728. <p>
  729. or
  730. </p>
  731. <div class="code">
  732. <pre>
  733. #ifdef SWIG
  734. #define const %constant
  735. #endif
  736. const double foo = 3.4;
  737. const double bar = 23.4;
  738. const int spam = 42;
  739. #ifdef SWIG
  740. #undef const
  741. #endif
  742. ...
  743. </pre>
  744. </div>
  745. <H3><a name="SWIG_nn14"></a>5.2.5 A cautionary tale of <tt>char *</tt></H3>
  746. <p>
  747. Before going any further, there is one bit of caution involving
  748. <tt>char *</tt> that must now be mentioned. When strings are passed
  749. from a scripting language to a C <tt>char *</tt>, the pointer usually
  750. points to string data stored inside the interpreter. It is almost
  751. always a really bad idea to modify this data. Furthermore, some
  752. languages may explicitly disallow it. For instance, in Python,
  753. strings are supposed be immutable. If you violate this, you will probably
  754. receive a vast amount of wrath when you unleash your module on the world.
  755. </p>
  756. <p>
  757. The primary source of problems are functions that might modify string data in place.
  758. A classic example would be a function like this:
  759. </p>
  760. <div class="code">
  761. <pre>
  762. char *strcat(char *s, const char *t)
  763. </pre>
  764. </div>
  765. <p>
  766. Although SWIG will certainly generate a wrapper for this, its behavior
  767. will be undefined. In fact, it will probably cause your application
  768. to crash with a segmentation fault or other memory related problem.
  769. This is because <tt>s</tt> refers to some internal data in the target
  770. language---data that you shouldn't be touching.
  771. </p>
  772. <p>
  773. The bottom line: don't rely on <tt>char *</tt> for anything other than read-only
  774. input values. However, it must be noted that you could change the behavior of SWIG
  775. using <a href="Typemaps.html#Typemaps">typemaps</a>.
  776. </p>
  777. <H2><a name="SWIG_nn15"></a>5.3 Pointers and complex objects</H2>
  778. <p>
  779. Most C programs manipulate arrays, structures, and other types of objects. This section
  780. discusses the handling of these datatypes.
  781. </p>
  782. <H3><a name="SWIG_nn16"></a>5.3.1 Simple pointers</H3>
  783. <p>
  784. Pointers to primitive C datatypes such as </p>
  785. <div class="code"><pre>
  786. int *
  787. double ***
  788. char **
  789. </pre></div>
  790. <p>
  791. are fully supported by SWIG. Rather than trying to convert the data being pointed to into a scripting
  792. representation, SWIG simply encodes the pointer itself into a
  793. representation that contains the actual value of the pointer and a type-tag.
  794. Thus, the SWIG representation of the above
  795. pointers (in Tcl), might look like this:</p>
  796. <div class="targetlang"><pre>
  797. _10081012_p_int
  798. _1008e124_ppp_double
  799. _f8ac_pp_char
  800. </pre></div>
  801. <p>
  802. A NULL pointer is represented by the string "NULL" or the value 0
  803. encoded with type information.</p>
  804. <p>
  805. All pointers are treated as opaque objects by SWIG. Thus, a pointer
  806. may be returned by a function and passed around to other C functions
  807. as needed. For all practical purposes, the scripting language
  808. interface works in exactly the same way as you would use the
  809. pointer in a C program. The only difference is that there is no mechanism for
  810. dereferencing the pointer since this would require the target language
  811. to understand the memory layout of the underlying object.
  812. </p>
  813. <p>
  814. The scripting language representation of a pointer value should never be
  815. manipulated directly. Even though the values shown look like hexadecimal
  816. addresses, the numbers used may differ from the actual machine address (e.g.,
  817. on little-endian machines, the digits may appear in reverse order).
  818. Furthermore, SWIG does not
  819. normally map pointers into high-level objects such as associative
  820. arrays or lists (for example, converting an
  821. <tt>int *</tt> into an list of integers). There are several reasons
  822. why SWIG does not do this:</p>
  823. <ul>
  824. <li>There is not enough information in a C declaration to properly map
  825. pointers into higher level constructs. For example, an <tt>int *</tt>
  826. may indeed be an array of integers, but if it contains ten million
  827. elements, converting it into a list object is probably a bad idea.
  828. </li>
  829. <li>The underlying semantics associated with a pointer is not known
  830. by SWIG. For instance, an <tt>int *</tt> might not be an array at all--perhaps it
  831. is an output value!
  832. </li>
  833. <li>By handling all pointers in a consistent manner, the implementation of SWIG is greatly
  834. simplified and less prone to error.
  835. </li>
  836. </ul>
  837. <H3><a name="SWIG_nn17"></a>5.3.2 Run time pointer type checking</H3>
  838. <p>
  839. By allowing pointers to be manipulated from a scripting language, extension modules
  840. effectively bypass compile-time type checking in the C/C++
  841. compiler. To prevent errors, a type signature is encoded into all
  842. pointer values and is used to perform run-time type checking. This
  843. type-checking process is an integral part of SWIG and can not be
  844. disabled or modified without using typemaps (described in later
  845. chapters).
  846. </p>
  847. <p>
  848. Like C, <tt>void *</tt> matches any kind of pointer. Furthermore,
  849. <tt>NULL</tt> pointers can be passed to any function that expects to
  850. receive a pointer. Although this has the potential to cause a crash,
  851. <tt>NULL</tt> pointers are also sometimes used
  852. as sentinel values or to denote a missing/empty value. Therefore,
  853. SWIG leaves NULL pointer checking up to the application.
  854. </p>
  855. <H3><a name="SWIG_nn18"></a>5.3.3 Derived types, structs, and classes</H3>
  856. <p>
  857. For everything else (structs, classes, arrays, etc...) SWIG applies a
  858. very simple rule :</p>
  859. <center>
  860. <b>Everything else is a pointer</b>
  861. </center>
  862. <p>
  863. In other words, SWIG manipulates everything else by reference. This
  864. model makes sense because most C/C++ programs make heavy use of
  865. pointers and SWIG can use the type-checked pointer mechanism already
  866. present for handling pointers to basic datatypes.</p>
  867. <p>
  868. Although this probably sounds complicated, it's really quite
  869. simple. Suppose you have an interface file like this :</p>
  870. <div class="code"><pre>
  871. %module fileio
  872. FILE *fopen(char *, char *);
  873. int fclose(FILE *);
  874. unsigned fread(void *ptr, unsigned size, unsigned nobj, FILE *);
  875. unsigned fwrite(void *ptr, unsigned size, unsigned nobj, FILE *);
  876. void *malloc(int nbytes);
  877. void free(void *);
  878. </pre></div>
  879. <p>
  880. In this file, SWIG doesn't know what a <tt>FILE</tt> is, but since it's used
  881. as a pointer, so it doesn't really matter what it is. If you wrapped
  882. this module into Python, you can use the functions just like you
  883. expect :</p>
  884. <div class="targetlang"><pre>
  885. # Copy a file
  886. def filecopy(source,target):
  887. f1 = fopen(source,"r")
  888. f2 = fopen(target,"w")
  889. buffer = malloc(8192)
  890. nbytes = fread(buffer,8192,1,f1)
  891. while (nbytes &gt; 0):
  892. fwrite(buffer,8192,1,f2)
  893. nbytes = fread(buffer,8192,1,f1)
  894. free(buffer)
  895. </pre></div>
  896. <p>
  897. In this case <tt>f1</tt>,<tt> f2</tt>, and <tt>buffer</tt> are all
  898. opaque objects containing C pointers. It doesn't matter what value
  899. they contain--our program works just fine without this knowledge.</p>
  900. <H3><a name="SWIG_nn19"></a>5.3.4 Undefined datatypes</H3>
  901. <p>
  902. When SWIG encounters an undeclared datatype, it automatically assumes
  903. that it is a structure or class. For example, suppose the following
  904. function appeared in a SWIG input file:</p>
  905. <div class="code"><pre>
  906. void matrix_multiply(Matrix *a, Matrix *b, Matrix *c);
  907. </pre></div>
  908. <p>
  909. SWIG has no idea what a "<tt>Matrix</tt>" is. However, it is obviously
  910. a pointer to something so SWIG generates a wrapper using its generic pointer
  911. handling code.
  912. </p>
  913. <p>
  914. Unlike C or C++, SWIG does not actually care whether <tt>Matrix</tt>
  915. has been previously defined in the interface file or not. This
  916. allows SWIG to generate interfaces from
  917. only partial or limited information. In some cases, you may not care
  918. what a <tt>Matrix</tt> really is as long as you can pass an opaque reference to
  919. one around in the scripting language interface.
  920. </p>
  921. <p>
  922. An important detail to mention is that SWIG will gladly generate
  923. wrappers for an interface when there are unspecified type names.
  924. However, <b>all unspecified types are internally handled as pointers
  925. to structures or classes!</b> For example, consider the following declaration:
  926. </p>
  927. <div class="code">
  928. <pre>
  929. void foo(size_t num);
  930. </pre>
  931. </div>
  932. <p>
  933. If <tt>size_t</tt> is undeclared, SWIG generates wrappers
  934. that expect to receive a type of <tt>size_t *</tt> (this mapping is described shortly).
  935. As a result, the scripting interface might behave strangely. For example:
  936. </p>
  937. <div class="code">
  938. <pre>
  939. foo(40);
  940. TypeError: expected a _p_size_t.
  941. </pre>
  942. </div>
  943. <p>
  944. The only way to fix this problem is to make sure you properly declare type names using
  945. <tt>typedef</tt>.
  946. </p>
  947. <!-- We might want to add an error reporting flag to swig -->
  948. <H3><a name="SWIG_nn20"></a>5.3.5 Typedef</H3>
  949. <p>
  950. Like C, <tt>typedef</tt> can be used to define new type names in SWIG. For example:
  951. </p>
  952. <div class="code"><pre>
  953. typedef unsigned int size_t;
  954. </pre></div>
  955. <p>
  956. <tt>typedef</tt> definitions appearing in a SWIG interface
  957. are not propagated to the generated wrapper code. Therefore, they
  958. either need to be defined in an included header file or placed in the
  959. declarations section like this:
  960. </p>
  961. <div class="code">
  962. <pre>
  963. %{
  964. /* Include in the generated wrapper file */
  965. typedef unsigned int size_t;
  966. %}
  967. /* Tell SWIG about it */
  968. typedef unsigned int size_t;
  969. </pre>
  970. </div>
  971. <p>
  972. or
  973. </p>
  974. <div class="code">
  975. <pre>
  976. %inline %{
  977. typedef unsigned int size_t;
  978. %}
  979. </pre>
  980. </div>
  981. <p>
  982. In certain cases, you might be able to include other header files to collect type information.
  983. For example:
  984. </p>
  985. <div class="code">
  986. <pre>
  987. %module example
  988. %import "sys/types.h"
  989. </pre>
  990. </div>
  991. <p>
  992. In this case, you might run SWIG as follows:
  993. </p>
  994. <div class="shell">
  995. <pre>
  996. $ swig -I/usr/include -includeall example.i
  997. </pre>
  998. </div>
  999. <p>
  1000. It should be noted that your mileage will vary greatly here.
  1001. System headers are notoriously complicated and may rely upon a variety
  1002. of non-standard C coding extensions (e.g., such as special directives
  1003. to GCC). Unless you exactly specify the right include directories and
  1004. preprocessor symbols, this may not work correctly (you will have to
  1005. experiment).
  1006. </p>
  1007. <p>
  1008. SWIG tracks <tt>typedef</tt> declarations and uses this information
  1009. for run-time type checking. For instance, if you use the above <tt>typedef</tt> and
  1010. had the following function declaration:
  1011. </p>
  1012. <div class="code">
  1013. <pre>
  1014. void foo(unsigned int *ptr);
  1015. </pre>
  1016. </div>
  1017. <p>
  1018. The corresponding wrapper function will accept arguments of
  1019. type <tt>unsigned int *</tt> or <tt>size_t *</tt>.
  1020. </p>
  1021. <H2><a name="SWIG_nn21"></a>5.4 Other Practicalities</H2>
  1022. <p>
  1023. So far, this chapter has presented almost everything you need to know to use SWIG
  1024. for simple interfaces. However, some C programs use idioms that are somewhat
  1025. more difficult to map to a scripting language interface. This section describes
  1026. some of these issues.
  1027. </p>
  1028. <H3><a name="SWIG_nn22"></a>5.4.1 Passing structures by value</H3>
  1029. <p>
  1030. Sometimes a C function takes structure parameters that are passed
  1031. by value. For example, consider the following function:
  1032. </p>
  1033. <div class="code"><pre>
  1034. double dot_product(Vector a, Vector b);
  1035. </pre></div>
  1036. <p>
  1037. To deal with this, SWIG transforms the function to use pointers by
  1038. creating a wrapper equivalent to the following:
  1039. </p>
  1040. <div class="code"><pre>
  1041. double wrap_dot_product(Vector *a, Vector *b) {
  1042. Vector x = *a;
  1043. Vector y = *b;
  1044. return dot_product(x,y);
  1045. }
  1046. </pre></div>
  1047. <p>
  1048. In the target language, the <tt>dot_product()</tt> function now accepts pointers
  1049. to Vectors instead of Vectors. For the most part, this transformation
  1050. is transparent so you might not notice.
  1051. </p>
  1052. <H3><a name="SWIG_nn23"></a>5.4.2 Return by value</H3>
  1053. <p>
  1054. C functions that return structures or classes datatypes by value are more difficult
  1055. to handle. Consider the following function:</p>
  1056. <div class="code"><pre>
  1057. Vector cross_product(Vector v1, Vector v2);
  1058. </pre></div>
  1059. <p>
  1060. This function wants to return <tt>Vector</tt>, but SWIG only really supports
  1061. pointers. As a result, SWIG creates a wrapper like this:
  1062. </p>
  1063. <div class="code"><pre>
  1064. Vector *wrap_cross_product(Vector *v1, Vector *v2) {
  1065. Vector x = *v1;
  1066. Vector y = *v2;
  1067. Vector *result;
  1068. result = (Vector *) malloc(sizeof(Vector));
  1069. *(result) = cross(x,y);
  1070. return result;
  1071. }
  1072. </pre></div>
  1073. <p>
  1074. or if SWIG was run with the <tt>-c++</tt> option:</p>
  1075. <div class="code"><pre>
  1076. Vector *wrap_cross(Vector *v1, Vector *v2) {
  1077. Vector x = *v1;
  1078. Vector y = *v2;
  1079. Vector *result = new Vector(cross(x,y)); // Uses default copy constructor
  1080. return result;
  1081. }
  1082. </pre></div>
  1083. <p>
  1084. In both cases, SWIG allocates a new object and returns a reference to it. It
  1085. is up to the user to delete the returned object when it is no longer
  1086. in use. Clearly, this will leak memory if you are unaware of the implicit
  1087. memory allocation and don't take steps to free the result. That said, it should be
  1088. noted that some language modules can now automatically track newly created objects and
  1089. reclaim memory for you. Consult the documentation for each language module for more details.
  1090. </p>
  1091. <p>
  1092. It should also be noted that the handling of pass/return by value in
  1093. C++ has some special cases. For example, the above code fragments
  1094. don't work correctly if <tt>Vector</tt> doesn't define a default
  1095. constructor. The section on SWIG and C++ has more information about this case.
  1096. </p>
  1097. <H3><a name="SWIG_nn24"></a>5.4.3 Linking to structure variables</H3>
  1098. <p>
  1099. When global variables or class members involving structures are
  1100. encountered, SWIG handles them as pointers. For example, a global
  1101. variable like this</p>
  1102. <div class="code"><pre>
  1103. Vector unit_i;
  1104. </pre></div>
  1105. <p>
  1106. gets mapped to an underlying pair of set/get functions like this :</p>
  1107. <div class="code"><pre>
  1108. Vector *unit_i_get() {
  1109. return &amp;unit_i;
  1110. }
  1111. void unit_i_set(Vector *value) {
  1112. unit_i = *value;
  1113. }
  1114. </pre></div>
  1115. <p>
  1116. Again some caution is in order. A global variable created in this
  1117. manner will show up as a pointer in the target scripting language. It
  1118. would be an extremely bad idea to free or destroy such a pointer. Also,
  1119. C++ classes must supply a properly defined copy constructor in order for
  1120. assignment to work correctly.
  1121. </p>
  1122. <H3><a name="SWIG_nn25"></a>5.4.4 Linking to <tt>char *</tt></H3>
  1123. <p>
  1124. When a global variable of type <tt>char *</tt> appears, SWIG uses <tt>malloc()</tt> or
  1125. <tt>new</tt> to allocate memory for the new value. Specifically, if you have a variable
  1126. like this
  1127. </p>
  1128. <div class="code">
  1129. <pre>
  1130. char *foo;
  1131. </pre>
  1132. </div>
  1133. <p>
  1134. SWIG generates the following code:
  1135. </p>
  1136. <div class="code">
  1137. <pre>
  1138. /* C mode */
  1139. void foo_set(char *value) {
  1140. if (foo) free(foo);
  1141. foo = (char *) malloc(strlen(value)+1);
  1142. strcpy(foo,value);
  1143. }
  1144. /* C++ mode. When -c++ option is used */
  1145. void foo_set(char *value) {
  1146. if (foo) delete [] foo;
  1147. foo = new char[strlen(value)+1];
  1148. strcpy(foo,value);
  1149. }
  1150. </pre>
  1151. </div>
  1152. <p>
  1153. If this is not the behavior that you want, consider making the variable read-only using the
  1154. <tt>%immutable</tt> directive. Alternatively, you might write a short assist-function to set the value
  1155. exactly like you want. For example:
  1156. </p>
  1157. <div class="code">
  1158. <pre>
  1159. %inline %{
  1160. void set_foo(char *value) {
  1161. strncpy(foo,value, 50);
  1162. }
  1163. %}
  1164. </pre>
  1165. </div>
  1166. <p>
  1167. Note: If you write an assist function like this, you will have to call
  1168. it as a function from the target scripting language (it does not work
  1169. like a variable). For example, in Python you will have to write:
  1170. </p>
  1171. <div class="targetlang">
  1172. <pre>
  1173. &gt;&gt;&gt; set_foo("Hello World")
  1174. </pre>
  1175. </div>
  1176. <p>
  1177. A common mistake with <tt>char *</tt> variables is to link to a variable declared like this:
  1178. </p>
  1179. <div class="code">
  1180. <pre>
  1181. char *VERSION = "1.0";
  1182. </pre>
  1183. </div>
  1184. <p>
  1185. In this case, the variable will be readable, but any attempt to change
  1186. the value results in a segmentation or general protection fault. This
  1187. is due to the fact that SWIG is trying to release the old value using
  1188. <tt>free</tt> or <tt>delete</tt> when the string literal value currently assigned to the variable wasn't
  1189. allocated using <tt>malloc()</tt> or <tt>new</tt>.
  1190. To fix this behavior, you can
  1191. either mark the variable as read-only, write a typemap (as described in Chapter 6),
  1192. or write a special set function as shown. Another alternative is to declare the
  1193. variable as an array:
  1194. </p>
  1195. <div class="code">
  1196. <pre>
  1197. char VERSION[64] = "1.0";
  1198. </pre>
  1199. </div>
  1200. <p>
  1201. When variables of type <tt>const char *</tt> are declared, SWIG still generates functions for setting and
  1202. getting the value. However, the default behavior does <em>not</em> release the previous contents (resulting in
  1203. a possible memory leak). In fact, you may get a warning message such as this when wrapping such a variable:
  1204. </p>
  1205. <div class="shell">
  1206. <pre>
  1207. example.i:20. Typemap warning. Setting const char * variable may leak memory
  1208. </pre>
  1209. </div>
  1210. <p>
  1211. The reason for this behavior is that <tt>const char *</tt> variables are often used to point to string literals.
  1212. For example:
  1213. </p>
  1214. <div class="code">
  1215. <pre>
  1216. const char *foo = "Hello World\n";
  1217. </pre>
  1218. </div>
  1219. <p>
  1220. Therefore, it's a really bad idea to call <tt>free()</tt> on such a
  1221. pointer. On the other hand, it <em>is</em> legal to change the
  1222. pointer to point to some other value. When setting a variable of this
  1223. type, SWIG allocates a new string (using malloc or new) and changes
  1224. the pointer to point to the new value. However, repeated
  1225. modifications of the value will result in a memory leak since the old
  1226. value is not released.
  1227. </p>
  1228. <H3><a name="SWIG_nn26"></a>5.4.5 Arrays</H3>
  1229. <p>
  1230. Arrays are fully supported by SWIG, but they are always handled as pointers instead
  1231. of mapping them to a special array object or list in the target language. Thus, the
  1232. following declarations :</p>
  1233. <div class="code"><pre>
  1234. int foobar(int a[40]);
  1235. void grok(char *argv[]);
  1236. void transpose(double a[20][20]);
  1237. </pre></div>
  1238. <p>
  1239. are processed as if they were really declared like this:
  1240. </p>
  1241. <div class="code"><pre>
  1242. int foobar(int *a);
  1243. void grok(char **argv);
  1244. void transpose(double (*a)[20]);
  1245. </pre></div>
  1246. <p>
  1247. Like C, SWIG does not perform array bounds checking.
  1248. It is up to the
  1249. user to make sure the pointer points a suitably allocated region of memory.
  1250. </p>
  1251. <p>
  1252. Multi-dimensional arrays are transformed into a pointer to an array of one less
  1253. dimension. For example:
  1254. </p>
  1255. <div class="code">
  1256. <pre>
  1257. int [10]; // Maps to int *
  1258. int [10][20]; // Maps to int (*)[20]
  1259. int [10][20][30]; // Maps to int (*)[20][30]
  1260. </pre>
  1261. </div>
  1262. <p>
  1263. It is important to note that in the C type system, a multidimensional
  1264. array <tt>a[][]</tt> is <b>NOT</b> equivalent to a single pointer
  1265. <tt>*a</tt> or a double pointer such as <tt>**a</tt>. Instead, a
  1266. pointer to an array is used (as shown above) where the actual value of
  1267. the pointer is the starting memory location of the array. The
  1268. reader is strongly advised to dust off their C book and re-read the
  1269. section on arrays before using them with SWIG.
  1270. </p>
  1271. <p>
  1272. Array variables are supported, but are read-only by default. For example:
  1273. </p>
  1274. <div class="code">
  1275. <pre>
  1276. int a[100][200];
  1277. </pre>
  1278. </div>
  1279. <p>
  1280. In this case, reading the variable 'a' returns a pointer of type <tt>int (*)[200]</tt>
  1281. that points to the first element of the array <tt>&amp;a[0][0]</tt>. Trying to modify 'a' results
  1282. in an error. This is because SWIG does not know how to copy data from the target
  1283. language into the array. To work around this limitation, you may want to write
  1284. a few simple assist functions like this:
  1285. </p>
  1286. <div class="code">
  1287. <pre>
  1288. %inline %{
  1289. void a_set(int i, int j, int val) {
  1290. a[i][j] = val;
  1291. }
  1292. int a_get(int i, int j) {
  1293. return a[i][j];
  1294. }
  1295. %}
  1296. </pre>
  1297. </div>
  1298. <p>
  1299. To dynamically create arrays of various sizes and shapes, it may be useful to write
  1300. some helper functions in your interface. For example:
  1301. </p>
  1302. <div class="code">
  1303. <pre>
  1304. // Some array helpers
  1305. %inline %{
  1306. /* Create any sort of [size] array */
  1307. int *int_array(int size) {
  1308. return (int *) malloc(size*sizeof(int));
  1309. }
  1310. /* Create a two-dimension array [size][10] */
  1311. int (*int_array_10(int size))[10] {
  1312. return (int (*)[10]) malloc(size*10*sizeof(int));
  1313. }
  1314. %}
  1315. </pre>
  1316. </div>
  1317. <p>
  1318. Arrays of <tt>char</tt> are handled as a special case by SWIG. In this case, strings in the
  1319. target language can be stored in the array. For example, if you have a declaration like this,
  1320. </p>
  1321. <div class="code">
  1322. <pre>
  1323. char pathname[256];
  1324. </pre>
  1325. </div>
  1326. <p>
  1327. SWIG generates functions for both getting and setting the value that are equivalent to the following
  1328. code:…

Large files files are truncated, but you can click here to view the full file