Add support for the Go programming language.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12108 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
09f14197ac
commit
5af2978f77
259 changed files with 16159 additions and 14 deletions
18
Examples/go/pointer/Makefile
Normal file
18
Examples/go/pointer/Makefile
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT =
|
||||
|
||||
all:: go
|
||||
|
||||
go::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile go_clean
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_run
|
||||
16
Examples/go/pointer/example.c
Normal file
16
Examples/go/pointer/example.c
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/* File : example.c */
|
||||
|
||||
void add(int *x, int *y, int *result) {
|
||||
*result = *x + *y;
|
||||
}
|
||||
|
||||
void sub(int *x, int *y, int *result) {
|
||||
*result = *x - *y;
|
||||
}
|
||||
|
||||
int divide(int n, int d, int *r) {
|
||||
int q;
|
||||
q = n/d;
|
||||
*r = n - q*d;
|
||||
return q;
|
||||
}
|
||||
68
Examples/go/pointer/example.go
Normal file
68
Examples/go/pointer/example.go
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/* ----------------------------------------------------------------------------
|
||||
* This file was automatically generated by SWIG (http://www.swig.org).
|
||||
* Version 2.0.1
|
||||
*
|
||||
* This file is not intended to be easily readable and contains a number of
|
||||
* coding conventions designed to improve portability and efficiency. Do not make
|
||||
* changes to this file unless you know what you are doing--modify the SWIG
|
||||
* interface file instead.
|
||||
* ----------------------------------------------------------------------------- */
|
||||
|
||||
package example
|
||||
|
||||
|
||||
type _swig_fnptr *byte
|
||||
type _swig_memberptr *byte
|
||||
|
||||
|
||||
func _swig_allocatememory(int) *byte
|
||||
func _swig_internal_allocate(len int) *byte {
|
||||
return _swig_allocatememory(len)
|
||||
}
|
||||
|
||||
func _swig_allocatestring(*byte, int) string
|
||||
func _swig_internal_makegostring(p *byte, l int) string {
|
||||
return _swig_allocatestring(p, l)
|
||||
}
|
||||
|
||||
func _swig_internal_gopanic(p *byte, l int) {
|
||||
panic(_swig_allocatestring(p, l))
|
||||
}
|
||||
|
||||
func _swig_wrap_add(*int, *int, *int)
|
||||
|
||||
func Add(arg1 *int, arg2 *int, arg3 *int) {
|
||||
_swig_wrap_add(arg1, arg2, arg3)
|
||||
}
|
||||
|
||||
func New_intp() *int
|
||||
func Copy_intp(int) *int
|
||||
func _swig_wrap_delete_intp(*int)
|
||||
|
||||
func Delete_intp(arg1 *int) {
|
||||
_swig_wrap_delete_intp(arg1)
|
||||
}
|
||||
|
||||
func _swig_wrap_intp_assign(*int, int)
|
||||
|
||||
func Intp_assign(arg1 *int, arg2 int) {
|
||||
_swig_wrap_intp_assign(arg1, arg2)
|
||||
}
|
||||
|
||||
func Intp_value(*int) int
|
||||
func _swig_wrap_sub(int, int, []int)
|
||||
|
||||
func Sub(arg1 int, arg2 int, arg3 []int) {
|
||||
_swig_wrap_sub(arg1, arg2, arg3)
|
||||
}
|
||||
|
||||
func Divide(int, int, []int) int
|
||||
|
||||
type SwigcptrVoid uintptr
|
||||
type Void interface {
|
||||
Swigcptr() uintptr;
|
||||
}
|
||||
func (p SwigcptrVoid) Swigcptr() uintptr {
|
||||
return uintptr(p)
|
||||
}
|
||||
|
||||
30
Examples/go/pointer/example.i
Normal file
30
Examples/go/pointer/example.i
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
extern void add(int *, int *, int *);
|
||||
extern void sub(int *, int *, int *);
|
||||
extern int divide(int, int, int *);
|
||||
%}
|
||||
|
||||
/* This example illustrates a couple of different techniques
|
||||
for manipulating C pointers */
|
||||
|
||||
/* First we'll use the pointer library */
|
||||
extern void add(int *x, int *y, int *result);
|
||||
%include cpointer.i
|
||||
%pointer_functions(int, intp);
|
||||
|
||||
/* Next we'll use some typemaps */
|
||||
|
||||
%include typemaps.i
|
||||
extern void sub(int *INPUT, int *INPUT, int *OUTPUT);
|
||||
|
||||
/* Next we'll use typemaps and the %apply directive */
|
||||
|
||||
%apply int *OUTPUT { int *r };
|
||||
extern int divide(int n, int d, int *r);
|
||||
|
||||
|
||||
|
||||
|
||||
143
Examples/go/pointer/index.html
Normal file
143
Examples/go/pointer/index.html
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:go:pointer</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
<tt>SWIG/Examples/go/pointer/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>Simple Pointer Handling</H2>
|
||||
|
||||
<p>
|
||||
This example illustrates a couple of techniques for handling simple
|
||||
pointers in SWIG. The prototypical example is a C function that
|
||||
operates on pointers such as this:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
void add(int *x, int *y, int *r) {
|
||||
*r = *x + *y;
|
||||
}
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
By default, SWIG wraps this function exactly as specified and creates
|
||||
an interface that expects pointer objects for arguments. This only
|
||||
works when there is a precise correspondence between the C type and
|
||||
some Go type.
|
||||
<p>
|
||||
|
||||
<h2>Other approaches</h2>
|
||||
|
||||
<p>
|
||||
<li>The SWIG pointer library provides a different, safer, way to
|
||||
handle pointers. For example, in the interface file you would do
|
||||
this:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
%include cpointer.i
|
||||
%pointer_functions(int, intp);
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
and from Go you would use pointers like this:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
a := example.New_intp()
|
||||
b := example.New_intp()
|
||||
c := example.New_intp()
|
||||
Intp_Assign(a, 37)
|
||||
Intp_Assign(b, 42)
|
||||
|
||||
fmt.Println(" a =", a)
|
||||
fmt.Println(" b =", b)
|
||||
fmt.Println(" c =", c)
|
||||
|
||||
// Call the add() function with some pointers
|
||||
example.Add(a,b,c)
|
||||
|
||||
// Now get the result
|
||||
res := example.Intp_value(c)
|
||||
fmt.Println(" 37 + 42 =", res)
|
||||
|
||||
// Clean up the pointers
|
||||
example.Delete_intp(a)
|
||||
example.Delete_intp(b)
|
||||
example.Delete_intp(c)
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<p>
|
||||
<li>Use the SWIG typemap library. This library allows you to
|
||||
completely change the way arguments are processed by SWIG. For
|
||||
example:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
%include "typemaps.i"
|
||||
void add(int *INPUT, int *INPUT, int *OUTPUT);
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
And in a Go program:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
r := []int{0}
|
||||
example.Sub(37,42,r)
|
||||
fmt.Println("Result =", r[0])
|
||||
</pre>
|
||||
</blockquote>
|
||||
Needless to say, this is substantially easier although a bit unusual.
|
||||
|
||||
<p>
|
||||
<li>A final alternative is to use the typemaps library in combination
|
||||
with the %apply directive. This allows you to change the names of parameters
|
||||
that behave as input or output parameters. For example:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
%include "typemaps.i"
|
||||
%apply int *INPUT {int *x, int *y};
|
||||
%apply int *OUTPUT {int *r};
|
||||
|
||||
void add(int *x, int *y, int *r);
|
||||
void sub(int *x, int *y, int *r);
|
||||
void mul(int *x, int *y, int *r);
|
||||
... etc ...
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
</ul>
|
||||
|
||||
<h2>Example</h2>
|
||||
|
||||
The following example illustrates the use of these features for pointer
|
||||
extraction.
|
||||
|
||||
<ul>
|
||||
<li> <a href="example.c">example.c</a> (C Source)
|
||||
<li> <a href="example.i">example.i</a> (SWIG interface)
|
||||
<li> <a href="runme.go">runme.go</a> (Go program)
|
||||
</ul>
|
||||
|
||||
<h2>Notes</h2>
|
||||
|
||||
<ul>
|
||||
<li>Since pointers are used for so many different things (arrays, output values,
|
||||
etc...) the complexity of pointer handling can be as complicated as you want to
|
||||
make it.
|
||||
|
||||
<p>
|
||||
<li>More documentation on the typemaps.i and cpointer.i library files can be
|
||||
found in the SWIG user manual. The files also contain documentation.
|
||||
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
</body>
|
||||
</html>
|
||||
47
Examples/go/pointer/runme.go
Normal file
47
Examples/go/pointer/runme.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
. "./example"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// First create some objects using the pointer library.
|
||||
fmt.Println("Testing the pointer library")
|
||||
a := New_intp()
|
||||
b := New_intp()
|
||||
c := New_intp()
|
||||
Intp_assign(a, 37)
|
||||
Intp_assign(b, 42)
|
||||
|
||||
fmt.Println(" a =", a)
|
||||
fmt.Println(" b =", b)
|
||||
fmt.Println(" c =", c)
|
||||
|
||||
// Call the add() function with some pointers
|
||||
Add(a, b, c)
|
||||
|
||||
// Now get the result
|
||||
res := Intp_value(c)
|
||||
fmt.Println(" 37 + 42 =", res)
|
||||
|
||||
// Clean up the pointers
|
||||
Delete_intp(a)
|
||||
Delete_intp(b)
|
||||
Delete_intp(c)
|
||||
|
||||
// Now try the typemap library
|
||||
// Now it is no longer necessary to manufacture pointers.
|
||||
// Instead we use a single element array which in Java is modifiable.
|
||||
|
||||
fmt.Println("Trying the typemap library")
|
||||
r := []int{0}
|
||||
Sub(37, 42, r)
|
||||
fmt.Println(" 37 - 42 = ", r[0])
|
||||
|
||||
// Now try the version with return value
|
||||
|
||||
fmt.Println("Testing return value")
|
||||
q := Divide(42, 37, r)
|
||||
fmt.Println(" 42/37 = ", q, " remainder ", r[0])
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue