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:
Ian Lance Taylor 2010-06-10 01:13:31 +00:00
commit 5af2978f77
259 changed files with 16159 additions and 14 deletions

View 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

View file

@ -0,0 +1,19 @@
/* File : example.c */
int do_op(int a, int b, int (*op)(int,int)) {
return (*op)(a,b);
}
int add(int a, int b) {
return a+b;
}
int sub(int a, int b) {
return a-b;
}
int mul(int a, int b) {
return a*b;
}
int (*funcvar)(int,int) = add;

View file

@ -0,0 +1,54 @@
/* ----------------------------------------------------------------------------
* 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 Do_op(int, int, _swig_fnptr) int
func _swig_getADD() _swig_fnptr
var ADD _swig_fnptr = _swig_getADD()
func _swig_getSUB() _swig_fnptr
var SUB _swig_fnptr = _swig_getSUB()
func _swig_getMUL() _swig_fnptr
var MUL _swig_fnptr = _swig_getMUL()
func _swig_wrap_funcvar_set(_swig_fnptr)
func SetFuncvar(arg1 _swig_fnptr) {
_swig_wrap_funcvar_set(arg1)
}
func GetFuncvar() _swig_fnptr
type SwigcptrVoid uintptr
type Void interface {
Swigcptr() uintptr;
}
func (p SwigcptrVoid) Swigcptr() uintptr {
return uintptr(p)
}

View file

@ -0,0 +1,9 @@
/* file: example.h */
extern int do_op(int,int, int (*op)(int,int));
extern int add(int,int);
extern int sub(int,int);
extern int mul(int,int);
extern int (*funcvar)(int,int);

View file

@ -0,0 +1,16 @@
/* File : example.i */
%module example
%{
#include "example.h"
%}
/* Wrap a function taking a pointer to a function */
extern int do_op(int a, int b, int (*op)(int, int));
/* Now install a bunch of "ops" as constants */
%constant int (*ADD)(int,int) = add;
%constant int (*SUB)(int,int) = sub;
%constant int (*MUL)(int,int) = mul;
extern int (*funcvar)(int,int);

View file

@ -0,0 +1,89 @@
<html>
<head>
<title>SWIG:Examples:go:funcptr</title>
</head>
<body bgcolor="#ffffff">
<tt>SWIG/Examples/go/funcptr/</tt>
<hr>
<H2>Pointers to Functions</H2>
<p>
Okay, just what in the heck does SWIG do with a declaration like this?
<blockquote>
<pre>
int do_op(int a, int b, int (*op)(int, int));
</pre>
</blockquote>
Well, it creates a wrapper as usual. Of course, that does raise some
questions about the third argument (the pointer to a function).
<p>
In this case, SWIG will wrap the function pointer as it does for all
other pointers. However, in order to actually call this function from
a Go program, you will need to pass some kind of C function pointer
object. In C, this is easy, you just supply a function name as an
argument like this:
<blockquote>
<pre>
/* Some callback function */
int add(int a, int b) {
return a+b;
}
...
int r = do_op(x,y,add);
</pre>
</blockquote>
To make this work with SWIG, you will need to do a little extra work.
Specifically, you need to create some function pointer objects using
the %constant directive like this:
<blockquote>
<pre>
%constant(int (*)(int,int)) ADD = add;
</pre>
</blockquote>
Now, in a Go program, you would do this:
<blockquote>
<pre>
int r = do_op(x,y, example.ADD)
</pre>
</blockquote>
where <tt>example</tt> is the module name.
<h2>An Example</h2>
Here are some files that illustrate this with a simple example:
<ul>
<li><a href="example.c">example.c</a>
<li><a href="example.h">example.h</a>
<li><a href="example.i">example.i</a> (SWIG interface)
<li><a href="runme.go">runme.go</a> (Sample program)
</ul>
<h2>Notes</h2>
<ul>
<li>The value of a function pointer must correspond to a function
written in C or C++. It is not possible to pass an arbitrary Go
function in as a substitute for a C function pointer.
</ul>
<hr>
</body>
</html>

View file

@ -0,0 +1,25 @@
package main
import (
"fmt"
. "./example"
)
func main() {
a := 37
b := 42
// Now call our C function with a bunch of callbacks
fmt.Println("Trying some C callback functions")
fmt.Println(" a = ", a)
fmt.Println(" b = ", b)
fmt.Println(" ADD(a,b) = ", Do_op(a, b, ADD))
fmt.Println(" SUB(a,b) = ", Do_op(a, b, SUB))
fmt.Println(" MUL(a,b) = ", Do_op(a, b, MUL))
fmt.Println("Here is what the C callback function classes are called in Go")
fmt.Println(" ADD = ", ADD)
fmt.Println(" SUB = ", SUB)
fmt.Println(" MUL = ", MUL)
}