PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/ttn-post-libtool-1-4-3-upgrade/SWIG/Doc/Manual/Preprocessor.html

#
HTML | 302 lines | 236 code | 64 blank | 2 comment | 0 complexity | 7c91844a81139094d92265c2e8428d17 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>SWIG Preprocessor</title>
  5. </head>
  6. <body bgcolor="#ffffff">
  7. <a name="n1"></a><H1>6 Preprocessing</H1>
  8. <!-- INDEX -->
  9. <ul>
  10. <li><a href="#n2">File inclusion</a>
  11. <li><a href="#n3">File imports</a>
  12. <li><a href="#n4">Conditional Compilation</a>
  13. <li><a href="#n5">Macro Expansion</a>
  14. <li><a href="#n6">SWIG Macros</a>
  15. <li><a href="#n7">C99 Extensions</a>
  16. <li><a href="#n8">Preprocessing and %{ ... %} blocks</a>
  17. <li><a href="#n9">Preprocessing and { ... }</a>
  18. </ul>
  19. <!-- INDEX -->
  20. SWIG includes its own enhanced version of the C preprocessor. The preprocessor
  21. supports the standard preprocessor directives and macro expansion rules.
  22. However, a number of modifications and enhancements have been made. This
  23. chapter describes some of these modifications.
  24. <a name="n2"></a><H2>6.1 File inclusion</H2>
  25. To include another file into a SWIG interface, use the <tt>%include</tt> directive
  26. like this:
  27. <blockquote>
  28. <pre>
  29. %include "pointer.i"
  30. </pre>
  31. </blockquote>
  32. Unlike, <tt>#include</tt>, <tt>%include</tt> includes each file once (and will not
  33. reload the file on subsequent <tt>%include</tt> declarations). Therefore, it
  34. is not necessary to use include-guards in SWIG interfaces.
  35. <p>
  36. By default, the <tt>#include</tt> is ignored unless you run SWIG with the
  37. <tt>-includeall</tt> option. The reason for ignoring traditional includes
  38. is that you often don't want SWIG to try and wrap everything included
  39. in standard header system headers and auxilliary files.
  40. <a name="n3"></a><H2>6.2 File imports</H2>
  41. SWIG provides another file inclusion directive with the <tt>%import</tt> directive.
  42. For example:
  43. <blockquote>
  44. <pre>
  45. %import "foo.i"
  46. </pre>
  47. </blockquote>
  48. The purpose of <tt>%import</tt> is to collect certain information from another
  49. SWIG interface file or a header file without actually generating any wrapper code.
  50. Such information generally includes type declarations (e.g., <tt>typedef</tt>) as well as
  51. C++ classes that might be used as base-classes for class declarations in the interface.
  52. The use of <tt>%import</tt> is also important when SWIG is used to generate
  53. extensions as a collection of related modules. This is advanced topic and is described
  54. in a later chapter.
  55. <P>
  56. The <tt>-importall</tt> directive tells SWIG to follow all <tt>#include</tt> statements
  57. as imports. This might be useful if you want to extract type definitions from system
  58. header files without generating any wrappers.
  59. <a name="n4"></a><H2>6.3 Conditional Compilation</H2>
  60. SWIG fully supports the use of <tt>#if</tt>, <tt>#ifdef</tt>,
  61. <tt>#ifndef</tt>, <tt>#else</tt>, <tt>#endif</tt> to conditionally
  62. include parts of an interface. The following symbols are predefined
  63. by SWIG when it is parsing the interface:
  64. <p>
  65. <blockquote><pre>
  66. SWIG Always defined when SWIG is processing a file
  67. SWIGTCL Defined when using Tcl
  68. SWIGTCL8 Defined when using Tcl8.0
  69. SWIGPERL Defined when using Perl
  70. SWIGPERL5 Defined when using Perl5
  71. SWIGPYTHON Defined when using Python
  72. SWIGGUILE Defined when using Guile
  73. SWIGRUBY Defined when using Ruby
  74. SWIGJAVA Defined when using Java
  75. SWIGMZSCHEME Defined when using Mzscheme
  76. SWIGWIN Defined when running SWIG under Windows
  77. SWIGMAC Defined when running SWIG on the Macintosh
  78. </pre></blockquote>
  79. In addition, SWIG defines the following set of standard C/C++ macros:
  80. <blockquote>
  81. <pre>
  82. __LINE__ Current line number
  83. __FILE__ Current file name
  84. __STDC__ Defined to indicate ANSI C
  85. __cplusplus Defined when -c++ option used
  86. </pre>
  87. </blockquote>
  88. Interface files can look at these symbols as necessary to change the
  89. way in which an interface is generated or to mix SWIG directives with
  90. C code. These symbols are also defined within the C code generated by
  91. SWIG (except for the symbol `<tt>SWIG</tt>' which is only defined
  92. within the SWIG compiler).<p>
  93. <a name="n5"></a><H2>6.4 Macro Expansion</H2>
  94. Traditional preprocessor macros can be used in SWIG interfaces. Be aware that the <tt>#define</tt> statement
  95. is also used to try and detect constants. Therefore, if you have something like this in your file,
  96. <blockquote>
  97. <pre>
  98. #ifndef _FOO_H 1
  99. #define _FOO_H 1
  100. ...
  101. #endif
  102. </pre>
  103. </blockquote>
  104. you may get some extra constants such as <tt>_FOO_H</tt> showing up in the scripting interface.
  105. <p>
  106. More complex macros can be defined in the standard way. For example:
  107. <blockquote>
  108. <pre>
  109. #define EXTERN extern
  110. #ifdef __STDC__
  111. #define _ANSI(args) (args)
  112. #else
  113. #define _ANSI(args) ()
  114. #endif
  115. </pre>
  116. </blockquote>
  117. The following operators can appear in macro definitions:
  118. <ul>
  119. <li><tt>#x</tt><br>
  120. Converts macro argument <tt>x</tt> to a string surrounded by double quotes ("x").
  121. <p>
  122. <li><tt>x ## y</tt><br>
  123. Concatenates x and y together to form <tt>xy</tt>.
  124. <p>
  125. <li><tt>`x`</tt><br>
  126. If <tt>x</tt> is a string surrounded by double quotes, do nothing. Otherwise, turn into a string
  127. like <tt>#x</tt>. This is a non-standard SWIG extension.
  128. </ul>
  129. <a name="n6"></a><H2>6.5 SWIG Macros</H2>
  130. SWIG provides an enhanced macro capability with the <tt>%define</tt> and <tt>%enddef</tt> directives.
  131. For example:
  132. <blockquote>
  133. <pre>
  134. %define ARRAYHELPER(type,name)
  135. %inline %{
  136. type *new_ ## name (int nitems) {
  137. return (type *) malloc(sizeof(type)*nitems);
  138. }
  139. void delete_ ## name(type *t) {
  140. free(t);
  141. }
  142. type name ## _get(type *t, int index) {
  143. return t[index];
  144. }
  145. void name ## _set(type *t, int index, type val) {
  146. t[index] = val;
  147. }
  148. %}
  149. %enddef
  150. ARRAYHELPER(int, IntArray)
  151. ARRAYHELPER(double, DoubleArray)
  152. </pre>
  153. </blockquote>
  154. The primary purpose of <tt>%define</tt> is to define large macros of code. Unlike normal C preprocessor
  155. macros, it is not necessary to terminate each line with a continuation character (\)--the macro definition
  156. extends to the first occurrence of <tt>%enddef</tt>. Furthermore, when such macros are expanded,
  157. they are reparsed through the C preprocessor. Thus, SWIG macros can contain all other preprocessor
  158. directives except for nested <tt>%define</tt> statements.
  159. <p>
  160. The SWIG macro capability is a very quick and easy way to generate large amounts of code. In fact,
  161. many of SWIG's advanced features and libraries are built using this mechanism (such as C++ template
  162. support).
  163. <a name="n7"></a><H2>6.6 C99 Extensions</H2>
  164. SWIG-1.3.12 and newer releases support variadic preprocessor macros. For example:
  165. <blockquote>
  166. <pre>
  167. #define DEBUGF(fmt,...) fprintf(stderr,fmt,__VA_ARGS__)
  168. </pre>
  169. </blockquote>
  170. When used, any extra arguments to <tt>...</tt> are placed into the
  171. special variable <tt>__VA_ARGS__</tt>. This also works with special SWIG
  172. macros defined using <tt>%define</tt>.
  173. <p>
  174. SWIG allows a variable number of arguments to be empty. However, this often results
  175. in an extra comma (,) and syntax error in the resulting expansion. For example:
  176. <blockquote>
  177. <pre>
  178. DEBUGF("hello"); --> fprintf(stderr,"hello",);
  179. </pre>
  180. </blockquote>
  181. To get rid of the extra comma, use <tt>##</tt> like this:
  182. <blockquote>
  183. <pre>
  184. #define DEBUGF(fmt,...) fprintf(stderr,fmt, ##__VA_ARGS__)
  185. </pre>
  186. </blockquote>
  187. <b>Comment:</b> It's not entirely clear how variadic macros might be useful to
  188. interface building. However, they are used internally to implement a number of
  189. SWIG directives and are provided to make SWIG more compatible with C99 code.
  190. <a name="n8"></a><H2>6.7 Preprocessing and %{ ... %} blocks</H2>
  191. The SWIG preprocessor does not process any text enclosed in a code block %{ ... %}. Therefore,
  192. if you write code like this,
  193. <blockquote>
  194. <pre>
  195. %{
  196. #ifdef NEED_BLAH
  197. int blah() {
  198. ...
  199. }
  200. #endif
  201. %}
  202. </pre>
  203. </blockquote>
  204. the contents of the <tt>%{ ... %}</tt> block are copied without
  205. modification to the output (including all preprocessor directives).
  206. <a name="n9"></a><H2>6.8 Preprocessing and { ... }</H2>
  207. SWIG always runs the preprocessor on text appearing inside <tt>{ ... }</tt>. However,
  208. sometimes it is desirable to make a preprocessor directive pass through to the output
  209. file. For example:
  210. <blockquote>
  211. <pre>
  212. %extend Foo {
  213. void bar() {
  214. #ifdef DEBUG
  215. printf("I'm in bar\n");
  216. #endif
  217. }
  218. }
  219. </pre>
  220. </blockquote>
  221. By default, SWIG will interpret the <tt>#ifdef DEBUG</tt> statement. However, if you really wanted that code
  222. to actually go into the wrapper file, prefix the preprocessor directives with <tt>%</tt> like this:
  223. <blockquote>
  224. <pre>
  225. %extend Foo {
  226. void bar() {
  227. %#ifdef DEBUG
  228. printf("I'm in bar\n");
  229. %#endif
  230. }
  231. }
  232. </pre>
  233. </blockquote>
  234. SWIG will strip the extra <tt>%</tt> and leave the preprocessor directive in the code.
  235. <p><hr>
  236. <address>SWIG 1.3 - Last Modified : May 25, 2002</address>
  237. </body>
  238. </html>