PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/go/pointer/index.html

#
HTML | 143 lines | 112 code | 31 blank | 0 comment | 0 complexity | e6bbb02a469d1d711a8ec49f8c9ac952 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:go:pointer</title>
  4. </head>
  5. <body bgcolor="#ffffff">
  6. <tt>SWIG/Examples/go/pointer/</tt>
  7. <hr>
  8. <H2>Simple Pointer Handling</H2>
  9. <p>
  10. This example illustrates a couple of techniques for handling simple
  11. pointers in SWIG. The prototypical example is a C function that
  12. 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. This only
  22. works when there is a precise correspondence between the C type and
  23. some Go type.
  24. <p>
  25. <h2>Other approaches</h2>
  26. <p>
  27. <li>The SWIG pointer library provides a different, safer, way to
  28. handle pointers. For example, in the interface file you would do
  29. this:
  30. <blockquote>
  31. <pre>
  32. %include cpointer.i
  33. %pointer_functions(int, intp);
  34. </pre>
  35. </blockquote>
  36. and from Go you would use pointers like this:
  37. <blockquote>
  38. <pre>
  39. a := example.New_intp()
  40. b := example.New_intp()
  41. c := example.New_intp()
  42. Intp_Assign(a, 37)
  43. Intp_Assign(b, 42)
  44. fmt.Println(" a =", a)
  45. fmt.Println(" b =", b)
  46. fmt.Println(" c =", c)
  47. // Call the add() function with some pointers
  48. example.Add(a,b,c)
  49. // Now get the result
  50. res := example.Intp_value(c)
  51. fmt.Println(" 37 + 42 =", res)
  52. // Clean up the pointers
  53. example.Delete_intp(a)
  54. example.Delete_intp(b)
  55. example.Delete_intp(c)
  56. </pre>
  57. </blockquote>
  58. <p>
  59. <li>Use the SWIG typemap library. This library allows you to
  60. completely change the way arguments are processed by SWIG. For
  61. example:
  62. <blockquote>
  63. <pre>
  64. %include "typemaps.i"
  65. void add(int *INPUT, int *INPUT, int *OUTPUT);
  66. </pre>
  67. </blockquote>
  68. And in a Go program:
  69. <blockquote>
  70. <pre>
  71. r := []int{0}
  72. example.Sub(37,42,r)
  73. fmt.Println("Result =", r[0])
  74. </pre>
  75. </blockquote>
  76. Needless to say, this is substantially easier although a bit unusual.
  77. <p>
  78. <li>A final alternative is to use the typemaps library in combination
  79. with the %apply directive. This allows you to change the names of parameters
  80. that behave as input or output parameters. For example:
  81. <blockquote>
  82. <pre>
  83. %include "typemaps.i"
  84. %apply int *INPUT {int *x, int *y};
  85. %apply int *OUTPUT {int *r};
  86. void add(int *x, int *y, int *r);
  87. void sub(int *x, int *y, int *r);
  88. void mul(int *x, int *y, int *r);
  89. ... etc ...
  90. </pre>
  91. </blockquote>
  92. </ul>
  93. <h2>Example</h2>
  94. The following example illustrates the use of these features for pointer
  95. extraction.
  96. <ul>
  97. <li> <a href="example.c">example.c</a> (C Source)
  98. <li> <a href="example.i">example.i</a> (SWIG interface)
  99. <li> <a href="runme.go">runme.go</a> (Go program)
  100. </ul>
  101. <h2>Notes</h2>
  102. <ul>
  103. <li>Since pointers are used for so many different things (arrays, output values,
  104. etc...) the complexity of pointer handling can be as complicated as you want to
  105. make it.
  106. <p>
  107. <li>More documentation on the typemaps.i and cpointer.i library files can be
  108. found in the SWIG user manual. The files also contain documentation.
  109. </ul>
  110. <hr>
  111. </body>
  112. </html>