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,91 @@
/* File : example.c */
/* I'm a file containing some C global variables */
/* Deal with Microsoft's attempt at deprecating C standard runtime functions */
#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
# define _CRT_SECURE_NO_DEPRECATE
#endif
#include <stdio.h>
#include <stdlib.h>
#include "example.h"
int ivar = 0;
short svar = 0;
long lvar = 0;
unsigned int uivar = 0;
unsigned short usvar = 0;
unsigned long ulvar = 0;
signed char scvar = 0;
unsigned char ucvar = 0;
char cvar = 0;
float fvar = 0;
double dvar = 0;
char *strvar = 0;
const char cstrvar[] = "Goodbye";
int *iptrvar = 0;
char name[256] = "Dave";
char path[256] = "/home/beazley";
/* Global variables involving a structure */
Point *ptptr = 0;
Point pt = { 10, 20 };
/* A variable that we will make read-only in the interface */
int status = 1;
/* A debugging function to print out their values */
void print_vars() {
printf("ivar = %d\n", ivar);
printf("svar = %d\n", svar);
printf("lvar = %ld\n", lvar);
printf("uivar = %u\n", uivar);
printf("usvar = %u\n", usvar);
printf("ulvar = %lu\n", ulvar);
printf("scvar = %d\n", scvar);
printf("ucvar = %u\n", ucvar);
printf("fvar = %g\n", fvar);
printf("dvar = %g\n", dvar);
printf("cvar = %c\n", cvar);
printf("strvar = %s\n", strvar ? strvar : "(null)");
printf("cstrvar = %s\n", cstrvar ? cstrvar : "(null)");
printf("iptrvar = %p\n", iptrvar);
printf("name = %s\n", name);
printf("ptptr = %p (%d, %d)\n", ptptr, ptptr ? ptptr->x : 0, ptptr ? ptptr->y : 0);
printf("pt = (%d, %d)\n", pt.x, pt.y);
printf("status = %d\n", status);
}
/* A function to create an integer (to test iptrvar) */
int *new_int(int value) {
int *ip = (int *) malloc(sizeof(int));
*ip = value;
return ip;
}
/* A function to create a point */
Point *new_Point(int x, int y) {
Point *p = (Point *) malloc(sizeof(Point));
p->x = x;
p->y = y;
return p;
}
char * Point_print(Point *p) {
static char buffer[256];
if (p) {
sprintf(buffer,"(%d,%d)", p->x,p->y);
} else {
sprintf(buffer,"null");
}
return buffer;
}
void pt_print() {
printf("(%d, %d)\n", pt.x, pt.y);
}

View file

@ -0,0 +1,198 @@
/* ----------------------------------------------------------------------------
* 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_ivar_set(int)
func SetIvar(arg1 int) {
_swig_wrap_ivar_set(arg1)
}
func GetIvar() int
func _swig_wrap_svar_set(int16)
func SetSvar(arg1 int16) {
_swig_wrap_svar_set(arg1)
}
func GetSvar() int16
func _swig_wrap_lvar_set(int32)
func SetLvar(arg1 int32) {
_swig_wrap_lvar_set(arg1)
}
func GetLvar() int32
func _swig_wrap_uivar_set(uint)
func SetUivar(arg1 uint) {
_swig_wrap_uivar_set(arg1)
}
func GetUivar() uint
func _swig_wrap_usvar_set(uint16)
func SetUsvar(arg1 uint16) {
_swig_wrap_usvar_set(arg1)
}
func GetUsvar() uint16
func _swig_wrap_ulvar_set(uint32)
func SetUlvar(arg1 uint32) {
_swig_wrap_ulvar_set(arg1)
}
func GetUlvar() uint32
func _swig_wrap_scvar_set(int8)
func SetScvar(arg1 int8) {
_swig_wrap_scvar_set(arg1)
}
func GetScvar() int8
func _swig_wrap_ucvar_set(byte)
func SetUcvar(arg1 byte) {
_swig_wrap_ucvar_set(arg1)
}
func GetUcvar() byte
func _swig_wrap_cvar_set(byte)
func SetCvar(arg1 byte) {
_swig_wrap_cvar_set(arg1)
}
func GetCvar() byte
func _swig_wrap_fvar_set(float32)
func SetFvar(arg1 float32) {
_swig_wrap_fvar_set(arg1)
}
func GetFvar() float32
func _swig_wrap_dvar_set(float64)
func SetDvar(arg1 float64) {
_swig_wrap_dvar_set(arg1)
}
func GetDvar() float64
func _swig_wrap_strvar_set(string)
func SetStrvar(arg1 string) {
_swig_wrap_strvar_set(arg1)
}
func GetStrvar() string
func GetCstrvar() string
func _swig_wrap_iptrvar_set(*int)
func SetIptrvar(arg1 *int) {
_swig_wrap_iptrvar_set(arg1)
}
func GetIptrvar() *int
func _swig_wrap_name_set(string)
func SetName(arg1 string) {
_swig_wrap_name_set(arg1)
}
func GetName() string
func _swig_wrap_ptptr_set(uintptr)
func SetPtptr(arg1 Point) {
_swig_wrap_ptptr_set(arg1.Swigcptr())
}
func _swig_wrap_ptptr_get() SwigcptrPoint
func GetPtptr() Point {
return _swig_wrap_ptptr_get()
}
func _swig_wrap_pt_set(uintptr)
func SetPt(arg1 Point) {
_swig_wrap_pt_set(arg1.Swigcptr())
}
func _swig_wrap_pt_get() SwigcptrPoint
func GetPt() Point {
return _swig_wrap_pt_get()
}
func GetStatus() int
func GetPath() string
func _swig_wrap_print_vars()
func Print_vars() {
_swig_wrap_print_vars()
}
func New_int(int) *int
func _swig_wrap_new_Point(int, int) SwigcptrPoint
func New_Point(arg1 int, arg2 int) Point {
return _swig_wrap_new_Point(arg1, arg2)
}
func _swig_wrap_Point_print(uintptr) string
func Point_print(arg1 Point) string {
return _swig_wrap_Point_print(arg1.Swigcptr())
}
func _swig_wrap_pt_print()
func Pt_print() {
_swig_wrap_pt_print()
}
type SwigcptrPoint uintptr
type Point interface {
Swigcptr() uintptr;
}
func (p SwigcptrPoint) Swigcptr() uintptr {
return uintptr(p)
}
type SwigcptrVoid uintptr
type Void interface {
Swigcptr() uintptr;
}
func (p SwigcptrVoid) Swigcptr() uintptr {
return uintptr(p)
}

View file

@ -0,0 +1,6 @@
/* File: example.h */
typedef struct {
int x,y;
} Point;

View file

@ -0,0 +1,49 @@
/* File : example.i */
%module example
%{
#include "example.h"
%}
/* Some global variable declarations */
%inline %{
extern int ivar;
extern short svar;
extern long lvar;
extern unsigned int uivar;
extern unsigned short usvar;
extern unsigned long ulvar;
extern signed char scvar;
extern unsigned char ucvar;
extern char cvar;
extern float fvar;
extern double dvar;
extern char *strvar;
extern const char cstrvar[];
extern int *iptrvar;
extern char name[256];
extern Point *ptptr;
extern Point pt;
%}
/* Some read-only variables */
%immutable;
%inline %{
extern int status;
extern char path[256];
%}
%mutable;
/* Some helper functions to make it easier to test */
%inline %{
extern void print_vars();
extern int *new_int(int value);
extern Point *new_Point(int x, int y);
extern char *Point_print(Point *p);
extern void pt_print();
%}

View file

@ -0,0 +1,87 @@
<html>
<head>
<title>SWIG:Examples:go:variables</title>
</head>
<body bgcolor="#ffffff">
<tt>SWIG/Examples/go/variables/</tt>
<hr>
<H2>Wrapping C Global Variables</H2>
<p>
When a C global variable appears in an interface file, SWIG provides
getter and setter functions for the variable. The getter function is
named <tt>Get</tt> followed by the capitalized name of the variable.
The setter variable starts with <tt>Set</tt> instead. The getter
function takes no parameters and returns the value of the variable.
The setter function takes a single parameter with the same type as the
variable, and returns nothing.
<p>Click <a href="example.i">here</a> to see a SWIG interface with
some variable declarations in it.
<h2>Manipulating Variables from Go</h2>
For example, if the package is called <tt>example</tt>, the global
variable
<blockquote>
<pre>
double foo;
</pre>
</blockquote>
will be accessed from Go as
<blockquote>
<pre>
example.GetFoo();
example.SetFoo(12.3);
</pre>
</blockquote>
Click <a href="runme.go">here</a> to see the example program that
updates and prints out the values of the variables using this
technique.
<h2>Key points</h2>
<ul>
<li>The name of the variable is capitalized.
<li>When a global variable has the type "<tt>char *</tt>", SWIG
manages it as a character string.
<li><tt>signed char</tt> and <tt>unsigned char</tt> are handled as
small 8-bit integers.
<li>String array variables such as '<tt>char name[256]</tt>' are
managed as Go strings, but when setting the value, the result is
truncated to the maximum length of the array. Furthermore, the string
is assumed to be null-terminated.
<li>When structures and classes are used as global variables, they are
mapped into pointers. Getting the "value" returns a pointer to the
global variable. Setting the value of a structure results in a memory
copy from a pointer to the global.
</ul>
<h2>Creating read-only variables</h2>
The <tt>%immutable</tt> and <tt>%mutable</tt> directives can be used
to specify a collection of read-only variables. A read only variable
will have a getter function but no setter function. For example:
<blockquote>
<pre>
%immutable;
int status;
double blah;
...
%mutable;
</pre>
</blockquote>
The <tt>%immutable</tt> directive remains in effect until it is
explicitly disabled using the <tt>%mutable</tt> directive.
</body>
</html>
<hr>

View file

@ -0,0 +1,67 @@
// This example illustrates global variable access from Go.
package main
import (
"fmt"
"./example"
)
func main() {
// Try to set the values of some global variables
example.SetIvar(42)
example.SetSvar(-31000)
example.SetLvar(65537)
example.SetUivar(123456)
example.SetUsvar(61000)
example.SetUlvar(654321)
example.SetScvar(-13)
example.SetUcvar(251)
example.SetCvar('S')
example.SetFvar(3.14159)
example.SetDvar(2.1828)
example.SetStrvar("Hello World")
example.SetIptrvar(example.New_int(37))
example.SetPtptr(example.New_Point(37, 42))
example.SetName("Bill")
// Now print out the values of the variables
fmt.Println("Variables (values printed from Go)")
fmt.Println("ivar =", example.GetIvar())
fmt.Println("svar =", example.GetSvar())
fmt.Println("lvar =", example.GetLvar())
fmt.Println("uivar =", example.GetUivar())
fmt.Println("usvar =", example.GetUsvar())
fmt.Println("ulvar =", example.GetUlvar())
fmt.Println("scvar =", example.GetScvar())
fmt.Println("ucvar =", example.GetUcvar())
fmt.Println("fvar =", example.GetFvar())
fmt.Println("dvar =", example.GetDvar())
fmt.Printf("cvar = %c\n", example.GetCvar())
fmt.Println("strvar =", example.GetStrvar())
fmt.Println("cstrvar =", example.GetCstrvar())
fmt.Println("iptrvar =", example.GetIptrvar())
fmt.Println("name =", example.GetName())
fmt.Println("ptptr =", example.GetPtptr(), example.Point_print(example.GetPtptr()))
fmt.Println("pt =", example.GetPt(), example.Point_print(example.GetPt()))
fmt.Println("\nVariables (values printed from C)")
example.Print_vars()
// This line would not compile: since status is marked with
// %immutable, there is no SetStatus function.
// fmt.Println("\nNow I'm going to try and modify some read only variables")
// example.SetStatus(0)
fmt.Println("\nI'm going to try and update a structure variable.\n")
example.SetPt(example.GetPtptr())
fmt.Println("The new value is")
example.Pt_print()
fmt.Println("You should see the value", example.Point_print(example.GetPtptr()))
}