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

/trunk/Doc/Devel/wrapobj.html

#
HTML | 223 lines | 188 code | 35 blank | 0 comment | 0 complexity | 96853f591825f0eb8fd20caab4d0cd9a MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. <html>
  2. <head>
  3. <title>Wrapper Objects</title>
  4. </head>
  5. <body>
  6. <center>
  7. <h1>Wrapper Objects</h1>
  8. <p>
  9. David M. Beazley <br>
  10. dave-swig@dabeaz.com<br>
  11. January 15, 2007<br>
  12. </b>
  13. </center>
  14. <h2>Introduction</h2>
  15. This document describes the functions related to management of
  16. wrapper objects. A wrapper object is a low-level
  17. data structure used to contain the C/C++ code that is emitted during the
  18. wrapping process. It contains not only the emitted code, but information
  19. about local variables. These objects are a critical component of almost all
  20. SWIG target language modules.
  21. <p>
  22. The functions described here are declared
  23. in <tt>Source/Swig/swigwrap.h</tt>. This API is considered to be
  24. stable.
  25. <h2>Creating and Destroying Wrappers</h2>
  26. The following functions create and destroy wrapper objects.
  27. <p>
  28. <b><tt>Wrapper *NewWrapper()</tt></b>
  29. <blockquote>
  30. Creates a new wrapper object.
  31. </blockquote>
  32. <p>
  33. <b><tt>void DelWrapper(Wrapper *w)</tt></b>
  34. <blockquote>
  35. Destroys a wrapper object.
  36. </blockquote>
  37. <h2>Wrapper Objects</h2>
  38. The <tt>Wrapper</tt> object returned by <tt>NewWrapper()</tt> has
  39. three public attributes.
  40. <blockquote><pre>
  41. typedef struct Wrapper {
  42. String *def;
  43. String *locals;
  44. String *code;
  45. } Wrapper;
  46. </pre></blockquote>
  47. The <tt>def</tt> attribute is a string that holds the function
  48. definition line. This line declares the function name, return type,
  49. and parameters. Language modules create this declaration by simply printing
  50. the appropriate text into this attribute.
  51. <p>
  52. The <tt>locals</tt> attribute is a string that holds the code
  53. related to any local variables declaration. Normally, language modules
  54. do not emit code to this string directly. They use <tt>Wrapper_add_local()</tt> or <tt>Wrapper_new_local()</tt> to do this.
  55. <p>
  56. The <tt>code</tt> attribute is a string that holds code related to the body of the function. Almost all code emitted by SWIG language modules is printed into this attribute.
  57. <h2>Creating Local Variables</h2>
  58. Perhaps the most useful aspect of <tt>Wrapper</tt> objects is the
  59. management of local variables. When creating a wrapper, it is often
  60. necessary to emit local variables related to the API of the target
  61. language. In addition to this, typemaps and other aspects of SWIG
  62. rely upon their own local variables. The following functions are used
  63. to create local variables, but also provide support for renaming
  64. variables in order to avoid name clashes.
  65. <p>
  66. <b><tt>int Wrapper_add_local(Wrapper *w, const String_or_char *name, const String_or_char *decl)</tt></b>
  67. <blockquote>
  68. Adds a new local variable to the wrapper object. <tt>name</tt> is the
  69. name of the local variable. <tt>decl</tt> is a string containing the
  70. actual variable declaration code. For example, if you wanted to
  71. declare a variable "<tt>int x = 42;</tt>", you would set <tt>name</tt>
  72. to <tt>"x"</tt> and
  73. <tt>decl</tt> to <tt>"int x = 42;"</tt>. On success, the text in
  74. <tt>decl</tt> is added to the <tt>locals</tt> attribute of <tt>w</tt>
  75. and 0 is returned. -1 is returned if a variable with the given name
  76. has already been declared.
  77. </blockquote>
  78. <p>
  79. <b><tt>int Wrapper_add_localv(Wrapper *w, const String_or_char *name, ...)</tt></b>
  80. <blockquote>
  81. The same as <tt>Wrapper_add_local()</tt> except that instead of
  82. passing a single string for the declaration, a NULL-terminated list of
  83. strings can be passed. These strings are joined together when
  84. producing the output. This convention turns out to be fairly useful
  85. since language modules often create their output into pieces.
  86. </blockquote>
  87. <p>
  88. <b><tt>char * Wrapper_new_local(Wrapper *w, const String_or_char *name, const String_or_char *decl)</tt></b>
  89. <blockquote>
  90. The same as <tt>Wrapper_add_local()</tt> except that if a local variable
  91. with the given name already exists, this function picks a new name and adds
  92. the declaration using the new name. The actual name used for the variable
  93. is returned. This function is used when generating code originating from
  94. typemaps. For instance, if a typemap declares a local variable, that variable
  95. might have to be renamed if the same typemap is used more than once in the same function.
  96. </blockquote>
  97. <p>
  98. <b><tt>char * Wrapper_new_localv(Wrapper *w, const String_or_char *name,...)</tt></b>
  99. <blockquote>
  100. The same as <tt>Wrapper_new_localv()</tt>, but accepts a NULL-terminated list
  101. of strings as code output.
  102. </blockquote>
  103. <p>
  104. <b><tt>int Wrapper_check_local(Wrapper *w, const String_or_char *name)</tt></b>
  105. <blockquote>
  106. Checks to see if a local variable with name <tt>name</tt> has been declared. Returns 1 if the local is defined, 0 otherwise.
  107. </blockquote>
  108. <h2>Output</h2>
  109. <p>
  110. <b><tt>void Wrapper_print(Wrapper *w, File *f)</tt></b>
  111. <blockquote>
  112. This function is used to format a wrapper function for output. The
  113. formatted wrapper function is emitted to <tt>f</tt> which may be any
  114. file-like object including a <tt>FILE *</tt> object or a <tt>String
  115. *</tt> object. When emitting the wrapper, the code printed to the
  116. wrapper object is automatically formatted. By default, the formatting
  117. is done according to a "pretty printing" style in which lines are split onto
  118. multiple lines and indented according to reasonable C formatting rules. This produces code that is moderately readable should you want to look at the wrapper
  119. code output. An alternative output mode is "compact printing" in which
  120. lines are collected and compacted. This may result in multiple C statements
  121. appearing on the same line. This mode is sometimes used when the size of
  122. a wrapper file is too large for certain compilers. For example, some compilers
  123. might impose a limit of 65536 lines per source file.
  124. </blockquote>
  125. <p>
  126. <b><tt>void Wrapper_compact_print_mode_set(int flag)</tt></b>
  127. <blockquote>
  128. Sets the output mode of the <tt>Wrapper_print()</tt>
  129. function. If <tt>flag</tt> is set to 1, then wrapper code is formatted
  130. to be compact.
  131. </blockquote>
  132. <p>
  133. <b><tt>void Wrapper_pretty_print(String *str, File *f)</tt></b>
  134. <blockquote>
  135. Utility function that reformats a string containing C/C++ code and outputs
  136. it to the file-like object <tt>f</tt>. The formatting process indents the code
  137. and structures it according to reasonable C formatting rules.
  138. </blockquote>
  139. <p>
  140. <b><tt>void Wrapper_compact_print(String *str, File *f)</tt></b>
  141. <blockquote>
  142. Utility function that reformats a string containing C/C++ code and outputs
  143. it to the file-like object <tt>f</tt>. The formatting process tries to
  144. make the code as compact as possible, without going completely overboard. For
  145. example, multiple C statements may be combined onto a single line and braces may be aligned to not use up extra lines.
  146. </blockquote>
  147. <h2>An Example</h2>
  148. Here is a simple example of how these functions are used. Suppose
  149. you wanted to emit the following C function:
  150. <blockquote>
  151. <pre>
  152. void foo(int n) {
  153. int i;
  154. for (i = 0; i &lt; n; i++) {
  155. printf("%d\n", i);
  156. }
  157. }
  158. </pre>
  159. </blockquote>
  160. Here is code that generates the above function:
  161. <blockquote>
  162. <pre>
  163. Wrapper *w = NewWrapper();
  164. Printf(w-&gt;def,"void foo(int n) {");
  165. Wrapper_add_local(w,"n",""); /* parameter n */
  166. Wrapper_add_local(w,"i", "int i;"); /* local i */
  167. Printv(w-&gt;code,"for (i = 0; i &lt; n; i++) {",
  168. "printf(\"%d\n",i);",
  169. "}\n", NIL);
  170. Printf(w-&gt;code,"}\n");
  171. /* Emit wrapper code */
  172. Wrapper_print(w,outf);
  173. DelWrapper(w);
  174. </pre>
  175. </blockquote>
  176. Within different language modules, this process is obviously much more
  177. involved. However, this example shows the basic idea of how C/C++
  178. code is prepared for output.
  179. </body>
  180. </html>