/tags/rel-2.0.1/Examples/perl5/value/index.html

# · HTML · 126 lines · 96 code · 30 blank · 0 comment · 0 complexity · c59fbc1ad8b8d7d4055015464dc65f88 MD5 · raw file

  1. <html>
  2. <head>
  3. <title>SWIG:Examples:perl5:value</title>
  4. </head>
  5. <body bgcolor="#ffffff">
  6. <tt>SWIG/Examples/perl5/value/</tt>
  7. <hr>
  8. <H2>Passing and Returning Structures by Value</H2>
  9. <p>
  10. Occasionally, a C program will manipulate structures by value such as shown in the
  11. following code:
  12. <blockquote>
  13. <pre>
  14. /* File : example.c */
  15. typedef struct Vector {
  16. double x, y, z;
  17. } Vector;
  18. double dot_product(Vector a, Vector b) {
  19. return (a.x*b.x + a.y*b.y + a.z*b.z);
  20. }
  21. Vector vector_add(Vector a, Vector b) {
  22. Vector r;
  23. r.x = a.x + b.x;
  24. r.y = a.y + b.y;
  25. r.z = a.z + b.z;
  26. return r;
  27. }
  28. </pre>
  29. </blockquote>
  30. Since SWIG only knows how to manage pointers to structures (not their internal
  31. representation), the following translations are made when wrappers are
  32. created:
  33. <blockquote>
  34. <pre>
  35. double wrap_dot_product(Vector *a, Vector *b) {
  36. return dot_product(*a,*b);
  37. }
  38. Vector *wrap_vector_add(Vector *a, Vector *b) {
  39. Vector *r = (Vector *) malloc(sizeof(Vector));
  40. *r = vector_add(*a,*b);
  41. return r;
  42. }
  43. </pre>
  44. </blockquote>
  45. The functions are then called using pointers from the scripting language interface.
  46. It should also be noted that any function that returns a structure by value results
  47. in an implicit memory allocation. This will be a memory leak unless you take steps
  48. to free the result (see below).
  49. <h2>The SWIG interface</h2>
  50. Click <a href="example.i">here</a> to see a SWIG interface file that
  51. wraps these two functions. In this file, there are a few essential features:
  52. <ul>
  53. <li>A wrapper for the <tt>free()</tt> function is created so that we
  54. can clean up the return result created by <tt>vector_add()</tt>
  55. function.
  56. <p>
  57. <li>The %inline directive is used to create a few helper functions for creating new Vector
  58. objects and to print out the value (for debugging purposes).
  59. </ul>
  60. <h2>A Perl Script</h2>
  61. Click <a href="runme.pl">here</a> to see a script that uses these functions from Perl.
  62. <h2>Notes</h2>
  63. <ul>
  64. <li>When the '<tt>-c++</tt>' option is used, the resulting wrapper code for the return value
  65. changes to the following:
  66. <blockquote>
  67. <pre>
  68. Vector *wrap_vector_add(Vector *a, Vector *b) {
  69. Vector *r = new Vector(vector_add(*a,*b));
  70. return r;
  71. }
  72. </pre>
  73. </blockquote>
  74. Similarly, it would be a mistake to use the <tt>free()</tt> function from C++. A safer
  75. approach would be to write a helper function like this:
  76. <blockquote>
  77. <pre>
  78. %inline %{
  79. void delete_Vector(Vector *v) {
  80. delete v;
  81. }
  82. %}
  83. </pre>
  84. </blockquote>
  85. <p>
  86. <li>If you use proxy classes and are careful, the SWIG generated wrappers can automatically
  87. clean up the result of return-by-reference when the scripting variable goes out of scope.
  88. <p>
  89. <li>Passing parameters by value like this really isn't the best C programming style.
  90. If possible, you might change your application to use pointers.
  91. <p>
  92. <li>Similar translations are made when C++ references are used.
  93. </ul>
  94. <hr>
  95. </body>
  96. </html>