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 = 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,4 @@
/* File : example.cxx */
#include "example.h"

View file

@ -0,0 +1,188 @@
/* ----------------------------------------------------------------------------
* 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 _swig_DirectorCallback struct {
SwigcptrCallback
v interface{}
}
func (p *_swig_DirectorCallback) Swigcptr() uintptr {
return p.SwigcptrCallback.Swigcptr()
}
func (p *_swig_DirectorCallback) SwigIsCallback() {
}
func (p *_swig_DirectorCallback) DirectorInterface() interface{} {
return p.v
}
func _swig_NewDirectorCallbackCallback(*_swig_DirectorCallback) SwigcptrCallback
func NewDirectorCallback(v interface{}) Callback {
p := &_swig_DirectorCallback{0, v}
p.SwigcptrCallback = _swig_NewDirectorCallbackCallback(p)
return p
}
func _swig_wrap_DeleteDirectorCallback(uintptr)
func DeleteDirectorCallback(arg1 Callback) {
_swig_wrap_DeleteDirectorCallback(arg1.Swigcptr())
}
func Swiggo_DeleteDirector_Callback(p *_swig_DirectorCallback) {
p.SwigcptrCallback = 0
}
type _swig_DirectorInterfaceCallbackRun interface {
Run()
}
func _swig_wrap__swig_DirectorCallback_upcall_Run(SwigcptrCallback)
func (swig_p *_swig_DirectorCallback) Run() {
if swig_g, swig_ok := swig_p.v.(_swig_DirectorInterfaceCallbackRun); swig_ok {
swig_g.Run()
return
}
_swig_wrap__swig_DirectorCallback_upcall_Run(swig_p.SwigcptrCallback)
}
func DirectorCallbackRun(p Callback) {
_swig_wrap__swig_DirectorCallback_upcall_Run(p.(*_swig_DirectorCallback).SwigcptrCallback)
}
func Swig_DirectorCallback_callback_run(p *_swig_DirectorCallback) {
p.Run()
}
type SwigcptrCallback uintptr
func (p SwigcptrCallback) Swigcptr() uintptr {
return (uintptr)(p)
}
func (p SwigcptrCallback) SwigIsCallback() {
}
func (p SwigcptrCallback) DirectorInterface() interface{} {
return nil
}
func _swig_wrap_delete_Callback(uintptr)
func DeleteCallback(arg1 Callback) {
_swig_wrap_delete_Callback(arg1.Swigcptr())
}
func _swig_wrap_Callback_run(SwigcptrCallback)
func (arg1 SwigcptrCallback) Run() {
_swig_wrap_Callback_run(arg1)
}
func _swig_wrap_new_Callback() SwigcptrCallback
func NewCallback() Callback {
return _swig_wrap_new_Callback()
}
type Callback interface {
Swigcptr() uintptr
SwigIsCallback()
DirectorInterface() interface{}
Run()
}
type SwigcptrCaller uintptr
func (p SwigcptrCaller) Swigcptr() uintptr {
return (uintptr)(p)
}
func (p SwigcptrCaller) SwigIsCaller() {
}
func _swig_wrap_new_Caller() SwigcptrCaller
func NewCaller() Caller {
return _swig_wrap_new_Caller()
}
func _swig_wrap_delete_Caller(uintptr)
func DeleteCaller(arg1 Caller) {
_swig_wrap_delete_Caller(arg1.Swigcptr())
}
func _swig_wrap_Caller_delCallback(SwigcptrCaller)
func (arg1 SwigcptrCaller) DelCallback() {
_swig_wrap_Caller_delCallback(arg1)
}
func _swig_wrap_Caller_setCallback(SwigcptrCaller, uintptr)
func (arg1 SwigcptrCaller) SetCallback(arg2 Callback) {
_swig_wrap_Caller_setCallback(arg1, arg2.Swigcptr())
}
func _swig_wrap_Caller_call(SwigcptrCaller)
func (arg1 SwigcptrCaller) Call() {
_swig_wrap_Caller_call(arg1)
}
type Caller interface {
Swigcptr() uintptr
SwigIsCaller()
DelCallback()
SetCallback(arg2 Callback)
Call()
}
type SwigcptrSwigDirector_Callback uintptr
type SwigDirector_Callback interface {
Swigcptr() uintptr;
}
func (p SwigcptrSwigDirector_Callback) 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,23 @@
/* File : example.h */
#include <cstdio>
#include <iostream>
class Callback {
public:
virtual ~Callback() { std::cout << "Callback::~Callback()" << std:: endl; }
virtual void run() { std::cout << "Callback::run()" << std::endl; }
};
class Caller {
private:
Callback *_callback;
public:
Caller(): _callback(0) {}
~Caller() { delCallback(); }
void delCallback() { delete _callback; _callback = 0; }
void setCallback(Callback *cb) { delCallback(); _callback = cb; }
void call() { if (_callback) _callback->run(); }
};

View file

@ -0,0 +1,13 @@
/* File : example.i */
%module(directors="1") example
%{
#include "example.h"
%}
%include "std_string.i"
/* turn on director wrapping Callback */
%feature("director") Callback;
%include "example.h"

View file

@ -0,0 +1,81 @@
<html>
<head>
<title>SWIG:Examples:go:callback</title>
</head>
<body bgcolor="#ffffff">
<tt>SWIG/Examples/go/callback/</tt>
<hr>
<H2>Implementing C++ callbacks in Go</H2>
<p>
This example illustrates how to use directors to implement C++
callbacks in Go.
</p>
<p>
Because Go and C++ use inheritance differently, you must call a
different function to create a class which uses callbacks. Instead of
calling the usual constructor function whose name is <tt>New</tt>
followed by the capitalized name of the class, you call a function
named <tt>NewDirector</tt> followed by the capitalized name of the
class.
</p>
<p>
The first argument to the <tt>NewDirector</tt> function is an instance
of a type. The <tt>NewDirector</tt> function will return an interface
value as usual. However, when calling any method on the returned
value, the program will first check whether the value passed
to <tt>NewDirector</tt> implements that method. If it does, the
method will be called in Go. This is true whether the method is
called from Go code or C++ code.
</p>
<p>
Note that the Go code will be called with just the Go value, not the
C++ value. If the Go code needs to call a C++ method on itself, you
need to get a copy of the C++ object. This is typically done as
follows:
<blockquote>
<pre>
type Child struct { abi Parent }
func (p *Child) ChildMethod() {
p.abi.ParentMethod()
}
func f() {
p := &Child{nil}
d := NewDirectorParent(p)
p.abi = d
...
}
</pre>
</blockquote>
In other words, we first create the Go value. We pass that to
the <tt>NewDirector</tt> function to create the C++ value; this C++
value will be created with an association to the Go value. We then
store the C++ value in the Go value, giving us the reverse
association. That permits us to call parent methods from the child.
</p>
<p>
To delete a director object, use the function <tt>DeleteDirector</tt>
followed by the capitalized name of the class.
</p>
<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>

View file

@ -0,0 +1,41 @@
package main
import (
"fmt"
. "./example"
)
func main() {
fmt.Println("Adding and calling a normal C++ callback")
fmt.Println("----------------------------------------")
caller := NewCaller()
callback := NewCallback()
caller.SetCallback(callback)
caller.Call()
caller.DelCallback()
callback = NewDirectorCallback(new(GoCallback))
fmt.Println()
fmt.Println("Adding and calling a Go callback")
fmt.Println("------------------------------------")
caller.SetCallback(callback)
caller.Call()
caller.DelCallback()
// Test that a double delete does not occur as the object has
// already been deleted from the C++ layer.
DeleteDirectorCallback(callback)
fmt.Println()
fmt.Println("Go exit")
}
type GoCallback struct{}
func (p *GoCallback) Run() {
fmt.Println("GoCallback.Run")
}