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

/trunk/Examples/ruby/value/index.html

#
HTML | 114 lines | 86 code | 28 blank | 0 comment | 0 complexity | 8ecafedc3756985850293e5bb5dff257 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:ruby:value</title>
  4. </head>
  5. <body bgcolor="#ffffff">
  6. <tt>SWIG/Examples/ruby/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 Ruby Script</h2>
  61. Click <a href="runme.rb">here</a> to see a script that uses these functions from Ruby.
  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. <p>
  75. <li>If you define C structure (or C++ class with '<tt>-c++</tt>' option)
  76. in the interface file, the SWIG generated wrappers can automaticallyclean
  77. up the result of return-by-reference by GC.
  78. <p>
  79. <li>Passing parameters by value like this really isn't the best C programming style.
  80. If possible, you might change your application to use pointers.
  81. <p>
  82. <li>Similar translations are made when C++ references are used.
  83. </ul>
  84. <hr>
  85. </body>
  86. </html>