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