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

18
Examples/go/enum/Makefile Normal file
View file

@ -0,0 +1,18 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
CXXSRCS = example.cxx
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,37 @@
/* File : example.cxx */
#include "example.h"
#include <stdio.h>
void Foo::enum_test(speed s) {
if (s == IMPULSE) {
printf("IMPULSE speed\n");
} else if (s == WARP) {
printf("WARP speed\n");
} else if (s == LUDICROUS) {
printf("LUDICROUS speed\n");
} else {
printf("Unknown speed\n");
}
}
void enum_test(color c, Foo::speed s) {
if (c == RED) {
printf("color = RED, ");
} else if (c == BLUE) {
printf("color = BLUE, ");
} else if (c == GREEN) {
printf("color = GREEN, ");
} else {
printf("color = Unknown color!, ");
}
if (s == Foo::IMPULSE) {
printf("speed = IMPULSE speed\n");
} else if (s == Foo::WARP) {
printf("speed = WARP speed\n");
} else if (s == Foo::LUDICROUS) {
printf("speed = LUDICROUS speed\n");
} else {
printf("speed = Unknown speed!\n");
}
}

View file

@ -0,0 +1,93 @@
/* ----------------------------------------------------------------------------
* 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))
}
type Color int
func _swig_getRED() Color
var RED Color = _swig_getRED()
func _swig_getBLUE() Color
var BLUE Color = _swig_getBLUE()
func _swig_getGREEN() Color
var GREEN Color = _swig_getGREEN()
type SwigcptrFoo uintptr
func (p SwigcptrFoo) Swigcptr() uintptr {
return (uintptr)(p)
}
func (p SwigcptrFoo) SwigIsFoo() {
}
func _swig_wrap_new_Foo() SwigcptrFoo
func NewFoo() Foo {
return _swig_wrap_new_Foo()
}
type FooSpeed int
func _swig_getFoo_IMPULSE_Foo() FooSpeed
var FooIMPULSE FooSpeed = _swig_getFoo_IMPULSE_Foo()
func _swig_getFoo_WARP_Foo() FooSpeed
var FooWARP FooSpeed = _swig_getFoo_WARP_Foo()
func _swig_getFoo_LUDICROUS_Foo() FooSpeed
var FooLUDICROUS FooSpeed = _swig_getFoo_LUDICROUS_Foo()
func _swig_wrap_Foo_enum_test(SwigcptrFoo, FooSpeed)
func (arg1 SwigcptrFoo) Enum_test(arg2 FooSpeed) {
_swig_wrap_Foo_enum_test(arg1, arg2)
}
func _swig_wrap_delete_Foo(uintptr)
func DeleteFoo(arg1 Foo) {
_swig_wrap_delete_Foo(arg1.Swigcptr())
}
type Foo interface {
Swigcptr() uintptr
SwigIsFoo()
Enum_test(arg2 FooSpeed)
}
func _swig_wrap_enum_test(Color, FooSpeed)
func Enum_test(arg1 Color, arg2 FooSpeed) {
_swig_wrap_enum_test(arg1, arg2)
}
type SwigcptrVoid uintptr
type Void interface {
Swigcptr() uintptr;
}
func (p SwigcptrVoid) Swigcptr() uintptr {
return uintptr(p)
}

View file

@ -0,0 +1,13 @@
/* File : example.h */
enum color { RED, BLUE, GREEN };
class Foo {
public:
Foo() { }
enum speed { IMPULSE=10, WARP=20, LUDICROUS=30 };
void enum_test(speed s);
};
void enum_test(color c, Foo::speed s);

View file

@ -0,0 +1,11 @@
/* File : example.i */
%module example
%{
#include "example.h"
%}
/* Let's just grab the original header file here */
%include "example.h"

View file

@ -0,0 +1,42 @@
<html>
<head>
<title>SWIG:Examples:go:enum</title>
</head>
<body bgcolor="#ffffff">
<tt>SWIG/Examples/go/enum/</tt>
<hr>
<H2>Wrapping enumerations</H2>
<p>
This example tests SWIG's ability to wrap enumerations.
<ul>
<li>
Enum values are expressed as constants or variables in GO.
<li>
If the enum is named, then that name, capitalized, as defined as a new
type name for <tt>int</tt>. All the enum values will be defined to
have that type.
<li>
If the enum is declared at global level, then the name in Go is simply
the enum value, capitalized.
<li>
If the enum is declared within a C++ class or struct, then the name in
Go is the capitalized name of the class or struct followed by the
capitalized name of the enum value.
<li>
</ul>
<p>
<ul>
<li><a href="example.h">example.h</a>. Header file containing some enums.
<li><a href="example.i">example.i</a>. Interface file.
<li><a href="runme.go">runme.go</a>. Sample Go program.
</ul>
<hr>
</body>
</html>

32
Examples/go/enum/runme.go Normal file
View file

@ -0,0 +1,32 @@
package main
import (
"fmt"
. "./example"
)
func main() {
// Print out the value of some enums
fmt.Println("*** color ***")
fmt.Println(" RED = ", RED)
fmt.Println(" BLUE = ", BLUE)
fmt.Println(" GREEN = ", GREEN)
fmt.Println("\n*** Foo::speed ***")
fmt.Println(" Foo::IMPULSE = ", FooIMPULSE)
fmt.Println(" Foo::WARP = ", FooWARP)
fmt.Println(" Foo::LUDICROUS = ", FooLUDICROUS)
fmt.Println("\nTesting use of enums with functions\n")
Enum_test(RED, FooIMPULSE)
Enum_test(BLUE, FooWARP)
Enum_test(GREEN, FooLUDICROUS)
fmt.Println("\nTesting use of enum with class method")
f := NewFoo()
f.Enum_test(FooIMPULSE)
f.Enum_test(FooWARP)
f.Enum_test(FooLUDICROUS)
}