PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/tcl/reference/index.html

#
HTML | 147 lines | 116 code | 31 blank | 0 comment | 0 complexity | 3a017fc16868f82064799608db22aad8 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:reference</title>
  4. </head>
  5. <body bgcolor="#ffffff">
  6. <tt>SWIG/Examples/tcl/reference/</tt>
  7. <hr>
  8. <H2>C++ Reference Handling</H2>
  9. <p>
  10. This example tests SWIG's handling of C++ references. Since C++
  11. references are closely related to pointers (as both refer to a
  12. location in memory), SWIG simply collapses all references into
  13. pointers when creating wrappers.
  14. <h2>Some examples</h2>
  15. References are most commonly used as function parameter. For example,
  16. you might have an operator like this:
  17. <blockquote>
  18. <pre>
  19. Vector operator+(const Vector &amp;a, const Vector &amp;b) {
  20. Vector result;
  21. result.x = a.x + b.x;
  22. result.y = a.y + b.y;
  23. result.z = a.z + b.z;
  24. return result;
  25. }
  26. </pre>
  27. </blockquote>
  28. or a function:
  29. <blockquote>
  30. <pre>
  31. Vector addv(const Vector &amp;a, const Vector &amp;b) {
  32. Vector result;
  33. result.x = a.x + b.x;
  34. result.y = a.y + b.y;
  35. result.z = a.z + b.z;
  36. return result;
  37. }
  38. </pre>
  39. </blockquote>
  40. In these cases, SWIG transforms everything into a pointer and creates a wrapper
  41. that looks like this:
  42. <blockquote>
  43. <pre>
  44. Vector wrap_addv(Vector *a, Vector *b) {
  45. return addv(*a,*b);
  46. }
  47. </pre>
  48. </blockquote>
  49. Occasionally, a reference is used as a return value of a function
  50. when the return result is to be used as an lvalue in an expression.
  51. The prototypical example is an operator like this:
  52. <blockquote>
  53. <pre>
  54. Vector &amp;operator[](int index);
  55. </pre>
  56. </blockquote>
  57. or a method:
  58. <blockquote>
  59. <pre>
  60. Vector &amp;get(int index);
  61. </pre>
  62. </blockquote>
  63. For functions returning references, a wrapper like this is created:
  64. <blockquote>
  65. <pre>
  66. Vector *wrap_Object_get(Object *self, int index) {
  67. Vector &amp;result = self-&gt;get(index);
  68. return &amp;result;
  69. }
  70. </pre>
  71. </blockquote>
  72. The following <a href="example.h">header file</a> contains some class
  73. definitions with some operators and use of references.
  74. <h2>SWIG Interface</h2>
  75. SWIG does NOT support overloaded operators so it can not directly build
  76. an interface to the classes in the above file. However, a number of workarounds
  77. can be made. For example, an overloaded operator can be stuck behind a function
  78. call such as the <tt>addv()</tt> function above. Array access can be handled
  79. with a pair of set/get functions like this:
  80. <blockquote>
  81. <pre>
  82. class VectorArray {
  83. public:
  84. ...
  85. %addmethods {
  86. Vector &amp;get(int index) {
  87. return (*self)[index];
  88. }
  89. void set(int index, Vector &amp;a) {
  90. (*self)[index] = a;
  91. }
  92. }
  93. ...
  94. }
  95. </pre>
  96. </blockquote>
  97. Click <a href="example.i">here</a> to see a SWIG interface file with these additions.
  98. <h2>Sample Tcl scripts</h2>
  99. Click <a href="runme.tcl">here</a> to see a script that manipulates some C++ references.
  100. <h2>Notes:</h2>
  101. <ul>
  102. <li>C++ references primarily provide notational convenience for C++
  103. source code. However, Tcl has neither the 'x.a' or 'x-&gt;a'
  104. notation so it doesn't much matter.
  105. <p>
  106. <li>When a program returns a reference, a pointer is returned.
  107. Unlike return by value, memory is not allocated to hold the
  108. return result.
  109. <p>
  110. <li>SWIG has particular trouble handling various combinations of references
  111. and pointers. This is side effect of an old parsing scheme and
  112. type representation that will be replaced in future versions.
  113. </ul>
  114. <hr>
  115. </body>
  116. </html>