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
CXXSRCS =
TARGET = example
INTERFACE = example.i
SWIGOPT =
all:: go
go::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_cpp
clean::
$(MAKE) -f $(TOP)/Makefile go_clean
check: all
$(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_run

View file

@ -0,0 +1,150 @@
/* ----------------------------------------------------------------------------
* 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 Maxint(int, int) int
func Maxdouble(float64, float64) float64
type SwigcptrVecint uintptr
func (p SwigcptrVecint) Swigcptr() uintptr {
return (uintptr)(p)
}
func (p SwigcptrVecint) SwigIsVecint() {
}
func _swig_wrap_new_vecint(int) SwigcptrVecint
func NewVecint(arg1 int) Vecint {
return _swig_wrap_new_vecint(arg1)
}
func _swig_wrap_vecint_get(SwigcptrVecint, int) *int
func (arg1 SwigcptrVecint) Get(arg2 int) *int {
return _swig_wrap_vecint_get(arg1, arg2)
}
func _swig_wrap_vecint_set(SwigcptrVecint, int, *int)
func (arg1 SwigcptrVecint) Set(arg2 int, arg3 *int) {
_swig_wrap_vecint_set(arg1, arg2, arg3)
}
func _swig_wrap_vecint_getitem(SwigcptrVecint, int) int
func (arg1 SwigcptrVecint) Getitem(arg2 int) int {
return _swig_wrap_vecint_getitem(arg1, arg2)
}
func _swig_wrap_vecint_setitem(SwigcptrVecint, int, int)
func (arg1 SwigcptrVecint) Setitem(arg2 int, arg3 int) {
_swig_wrap_vecint_setitem(arg1, arg2, arg3)
}
func _swig_wrap_delete_vecint(uintptr)
func DeleteVecint(arg1 Vecint) {
_swig_wrap_delete_vecint(arg1.Swigcptr())
}
type Vecint interface {
Swigcptr() uintptr
SwigIsVecint()
Get(arg2 int) *int
Set(arg2 int, arg3 *int)
Getitem(arg2 int) int
Setitem(arg2 int, arg3 int)
}
type SwigcptrVecdouble uintptr
func (p SwigcptrVecdouble) Swigcptr() uintptr {
return (uintptr)(p)
}
func (p SwigcptrVecdouble) SwigIsVecdouble() {
}
func _swig_wrap_new_vecdouble(int) SwigcptrVecdouble
func NewVecdouble(arg1 int) Vecdouble {
return _swig_wrap_new_vecdouble(arg1)
}
func _swig_wrap_vecdouble_get(SwigcptrVecdouble, int) *float64
func (arg1 SwigcptrVecdouble) Get(arg2 int) *float64 {
return _swig_wrap_vecdouble_get(arg1, arg2)
}
func _swig_wrap_vecdouble_set(SwigcptrVecdouble, int, *float64)
func (arg1 SwigcptrVecdouble) Set(arg2 int, arg3 *float64) {
_swig_wrap_vecdouble_set(arg1, arg2, arg3)
}
func _swig_wrap_vecdouble_getitem(SwigcptrVecdouble, int) float64
func (arg1 SwigcptrVecdouble) Getitem(arg2 int) float64 {
return _swig_wrap_vecdouble_getitem(arg1, arg2)
}
func _swig_wrap_vecdouble_setitem(SwigcptrVecdouble, int, float64)
func (arg1 SwigcptrVecdouble) Setitem(arg2 int, arg3 float64) {
_swig_wrap_vecdouble_setitem(arg1, arg2, arg3)
}
func _swig_wrap_delete_vecdouble(uintptr)
func DeleteVecdouble(arg1 Vecdouble) {
_swig_wrap_delete_vecdouble(arg1.Swigcptr())
}
type Vecdouble interface {
Swigcptr() uintptr
SwigIsVecdouble()
Get(arg2 int) *float64
Set(arg2 int, arg3 *float64)
Getitem(arg2 int) float64
Setitem(arg2 int, arg3 float64)
}
type SwigcptrVoid uintptr
type Void interface {
Swigcptr() uintptr;
}
func (p SwigcptrVoid) Swigcptr() uintptr {
return uintptr(p)
}

View file

@ -0,0 +1,32 @@
/* File : example.h */
// Some template definitions
template<class T> T max(T a, T b) { return a>b ? a : b; }
template<class T> class vector {
T *v;
int sz;
public:
vector(int _sz) {
v = new T[_sz];
sz = _sz;
}
T &get(int index) {
return v[index];
}
void set(int index, T &val) {
v[index] = val;
}
#ifdef SWIG
%extend {
T getitem(int index) {
return $self->get(index);
}
void setitem(int index, T val) {
$self->set(index,val);
}
}
#endif
};

View file

@ -0,0 +1,17 @@
/* File : example.i */
%module example
%{
#include "example.h"
%}
/* Let's just grab the original header file here */
%include "example.h"
/* Now instantiate some specific template declarations */
%template(maxint) max<int>;
%template(maxdouble) max<double>;
%template(vecint) vector<int>;
%template(vecdouble) vector<double>;

View file

@ -0,0 +1,113 @@
<html>
<head>
<title>SWIG:Examples:go:template</title>
</head>
<body bgcolor="#ffffff">
<tt>SWIG/Examples/go/template/</tt>
<hr>
<H2>C++ template support</H2>
<p>
This example illustrates how C++ templates can be used from Go using
SWIG.
<h2>The C++ Code</h2>
Lets take a templated function and a templated class as follows:
<blockquote>
<pre>
/* File : example.h */
// Some template definitions
template<class T> T max(T a, T b) { return a&gt;b ? a : b; }
template<class T> class vector {
T *v;
int sz;
public:
vector(int _sz) {
v = new T[_sz];
sz = _sz;
}
T &amp;get(int index) {
return v[index];
}
void set(int index, T &amp;val) {
v[index] = val;
}
#ifdef SWIG
%addmethods {
T getitem(int index) {
return self-&gt;get(index);
}
void setitem(int index, T val) {
self-&gt;set(index,val);
}
}
#endif
};
</pre>
</blockquote>
The %addmethods is used for a neater interface from Go as the
functions <tt>get</tt> and <tt>set</tt> use C++ references to
primitive types. These are tricky to use from Go as they end up as
pointers, which only work when the C++ and Go types correspond
precisely.
<h2>The SWIG interface</h2>
A simple SWIG interface for this can be built by simply grabbing the
header file like this:
<blockquote>
<pre>
/* File : example.i */
%module example
%{
#include "example.h"
%}
/* Let's just grab the original header file here */
%include "example.h"
/* Now instantiate some specific template declarations */
%template(maxint) max<int>;
%template(maxdouble) max<double>;
%template(vecint) vector<int>;
%template(vecdouble) vector<double>;
</pre>
</blockquote>
Note that SWIG parses the templated function <tt>max</tt> and
templated class <tt>vector</tt> and so knows about them. However to
generate code for use from Go, SWIG has to be told which class/type to
use as the template parameter. The SWIG directive %template is used
for this.
<h2>A sample Go program</h2>
Click <a href="runme.go">here</a> to see a Go program that calls the
C++ functions from Go.
<h2>Notes</h2> Use templated classes just like you would any other
SWIG generated Go class. Use the classnames specified by the %template
directive.
<blockquote>
<pre>
vecdouble dv = new vecdouble(1000);
dv.setitem(i, 12.34));
</pre>
</blockquote>
<hr>
</body>
</html>

View file

@ -0,0 +1,43 @@
// This example illustrates how C++ templates can be used from Go.
package main
import (
"fmt"
. "./example"
)
func main() {
// Call some templated functions
fmt.Println(Maxint(3, 7))
fmt.Println(Maxdouble(3.14, 2.18))
// Create some class
iv := NewVecint(100)
dv := NewVecdouble(1000)
for i := 0; i < 100; i++ {
iv.Setitem(i, 2*i)
}
for i := 0; i < 1000; i++ {
dv.Setitem(i, 1.0/float64(i+1))
}
{
sum := 0
for i := 0; i < 100; i++ {
sum = sum + iv.Getitem(i)
}
fmt.Println(sum)
}
{
sum := float64(0.0)
for i := 0; i < 1000; i++ {
sum = sum + dv.Getitem(i)
}
fmt.Println(sum)
}
}