PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/tcl/pointer/index.html

#
HTML | 171 lines | 137 code | 34 blank | 0 comment | 0 complexity | fe233725c39b5cefad23d82018f31b04 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. <html>
  2. <head>
  3. <title>SWIG:Examples:tcl:pointer</title>
  4. </head>
  5. <body bgcolor="#ffffff">
  6. <tt>SWIG/Examples/tcl/pointer/</tt>
  7. <hr>
  8. <H2>Simple Pointer Handling</H2>
  9. <p>
  10. This example illustrates a couple of techniques for handling
  11. simple pointers in SWIG. The prototypical example is a C function
  12. that operates on pointers such as this:
  13. <blockquote>
  14. <pre>
  15. void add(int *x, int *y, int *r) {
  16. *r = *x + *y;
  17. }
  18. </pre>
  19. </blockquote>
  20. By default, SWIG wraps this function exactly as specified and creates
  21. an interface that expects pointer objects for arguments. The only
  22. problem is how does one go about creating these objects from a script?
  23. <h2>Possible Solutions</h2>
  24. <ul>
  25. <li>Write some helper functions to explicitly create objects. For
  26. example:
  27. <blockquote>
  28. <pre>
  29. int *new_int(int ivalue) {
  30. int *i = (int *) malloc(sizeof(ivalue));
  31. *i = ivalue;
  32. return i;
  33. }
  34. int get_int(int *i) {
  35. return *i;
  36. }
  37. void delete_int(int *i) {
  38. free(i);
  39. }
  40. </pre>
  41. </blockquote>
  42. Now, in a script you would do this:
  43. <blockquote>
  44. <pre>
  45. set a [new_int 37]
  46. set b [new_int 42]
  47. set c [new_int 0]
  48. add $a $b $c
  49. set r [get_int $c]
  50. puts "Result = $r"
  51. delete_int $a
  52. delete_int $b
  53. delete_int $c
  54. </pre>
  55. </blockquote>
  56. <p>
  57. <li>Use the SWIG pointer library. For example, in the interface file
  58. you would do this:
  59. <blockquote>
  60. <pre>
  61. %include "pointer.i"
  62. </pre>
  63. </blockquote?
  64. and in a script you would do this:
  65. <blockquote>
  66. <pre>
  67. set a [ptrcreate int 37]
  68. set b [ptrcreate int 42]
  69. set c [ptrcreate int]
  70. add $a $b $c
  71. set r [ptrvalue $c]
  72. puts "Result = $r"
  73. ptrfree $a
  74. ptrfree $b
  75. ptrfree $c
  76. </pre>
  77. </blockquote>
  78. The advantage to using the pointer library is that it unifies some of the helper
  79. functions behind a common set of names. For example, the same set of functions work
  80. with int, double, float, and other fundamental types.
  81. <p>
  82. <li>Use the SWIG typemap library. This library allows you to completely
  83. change the way arguments are processed by SWIG. For example:
  84. <blockquote>
  85. <pre>
  86. %include "typemaps.i"
  87. void add(int *INPUT, int *INPUT, int *OUTPUT);
  88. </pre>
  89. </blockquote>
  90. And in a script:
  91. <blockquote>
  92. <pre>
  93. set r [add 37 42]
  94. puts "Result = $r"
  95. </pre>
  96. </blockquote>
  97. Needless to say, this is substantially easier.
  98. <p>
  99. <li>A final alternative is to use the typemaps library in combination
  100. with the %apply directive. This allows you to change the names of parameters
  101. that behave as input or output parameters. For example:
  102. <blockquote>
  103. <pre>
  104. %include "typemaps.i"
  105. %apply int *INPUT {int *x, int *y};
  106. %apply int *OUTPUT {int *r};
  107. void add(int *x, int *y, int *r);
  108. void sub(int *x, int *y, int *r);
  109. void mul(int *x, int *y, int *r);
  110. ... etc ...
  111. </pre>
  112. </blockquote>
  113. </ul>
  114. <h2>Example</h2>
  115. The following example illustrates the use of these features for pointer
  116. extraction.
  117. <ul>
  118. <li> <a href="example.c">example.c</a> (C Source)
  119. <li> <a href="example.i">example.i</a> (SWIG interface)
  120. <li> <a href="runme.tcl">runme.tcl</a> (Tcl Script)
  121. </ul>
  122. <h2>Notes</h2>
  123. <ul>
  124. <li>Since pointers are used for so many different things (arrays, output values,
  125. etc...) the complexity of pointer handling can be as complicated as you want to
  126. make it.
  127. <p>
  128. <li>More documentation on the typemaps.i and pointer.i library files can be
  129. found in the SWIG user manual. The files also contain documentation.
  130. <p>
  131. <li>The pointer.i library is designed primarily for convenience. If you
  132. are concerned about performance, you probably want to use a different
  133. approach.
  134. </ul>
  135. <hr>
  136. </body>
  137. </html>