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,397 @@
/* ----------------------------------------------------------------------------
* 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_DirectorEmployee struct {
SwigcptrEmployee
v interface{}
}
func (p *_swig_DirectorEmployee) Swigcptr() uintptr {
return p.SwigcptrEmployee.Swigcptr()
}
func (p *_swig_DirectorEmployee) SwigIsEmployee() {
}
func (p *_swig_DirectorEmployee) DirectorInterface() interface{} {
return p.v
}
func _swig_NewDirectorEmployeeEmployee(*_swig_DirectorEmployee, string) SwigcptrEmployee
func NewDirectorEmployee(v interface{}, arg1 string) Employee {
p := &_swig_DirectorEmployee{0, v}
p.SwigcptrEmployee = _swig_NewDirectorEmployeeEmployee(p, arg1)
return p
}
type _swig_DirectorInterfaceEmployeeGetTitle interface {
GetTitle() string
}
func _swig_wrap__swig_DirectorEmployee_upcall_GetTitle(SwigcptrEmployee) string
func (swig_p *_swig_DirectorEmployee) GetTitle() string {
if swig_g, swig_ok := swig_p.v.(_swig_DirectorInterfaceEmployeeGetTitle); swig_ok {
return swig_g.GetTitle()
}
return _swig_wrap__swig_DirectorEmployee_upcall_GetTitle(swig_p.SwigcptrEmployee)
}
func DirectorEmployeeGetTitle(p Employee) string {
return _swig_wrap__swig_DirectorEmployee_upcall_GetTitle(p.(*_swig_DirectorEmployee).SwigcptrEmployee)
}
func Swig_DirectorEmployee_callback_getTitle(p *_swig_DirectorEmployee) (swig_result string) {
return p.GetTitle()
}
type _swig_DirectorInterfaceEmployeeGetName interface {
GetName() string
}
func _swig_wrap__swig_DirectorEmployee_upcall_GetName(SwigcptrEmployee) string
func (swig_p *_swig_DirectorEmployee) GetName() string {
if swig_g, swig_ok := swig_p.v.(_swig_DirectorInterfaceEmployeeGetName); swig_ok {
return swig_g.GetName()
}
return _swig_wrap__swig_DirectorEmployee_upcall_GetName(swig_p.SwigcptrEmployee)
}
func DirectorEmployeeGetName(p Employee) string {
return _swig_wrap__swig_DirectorEmployee_upcall_GetName(p.(*_swig_DirectorEmployee).SwigcptrEmployee)
}
func Swig_DirectorEmployee_callback_getName(p *_swig_DirectorEmployee) (swig_result string) {
return p.GetName()
}
type _swig_DirectorInterfaceEmployeeGetPosition interface {
GetPosition() string
}
func _swig_wrap__swig_DirectorEmployee_upcall_GetPosition(SwigcptrEmployee) string
func (swig_p *_swig_DirectorEmployee) GetPosition() string {
if swig_g, swig_ok := swig_p.v.(_swig_DirectorInterfaceEmployeeGetPosition); swig_ok {
return swig_g.GetPosition()
}
return _swig_wrap__swig_DirectorEmployee_upcall_GetPosition(swig_p.SwigcptrEmployee)
}
func DirectorEmployeeGetPosition(p Employee) string {
return _swig_wrap__swig_DirectorEmployee_upcall_GetPosition(p.(*_swig_DirectorEmployee).SwigcptrEmployee)
}
func Swig_DirectorEmployee_callback_getPosition(p *_swig_DirectorEmployee) (swig_result string) {
return p.GetPosition()
}
func _swig_wrap_DeleteDirectorEmployee(uintptr)
func DeleteDirectorEmployee(arg1 Employee) {
_swig_wrap_DeleteDirectorEmployee(arg1.Swigcptr())
}
func Swiggo_DeleteDirector_Employee(p *_swig_DirectorEmployee) {
p.SwigcptrEmployee = 0
}
type SwigcptrEmployee uintptr
func (p SwigcptrEmployee) Swigcptr() uintptr {
return (uintptr)(p)
}
func (p SwigcptrEmployee) SwigIsEmployee() {
}
func (p SwigcptrEmployee) DirectorInterface() interface{} {
return nil
}
func _swig_wrap_new_Employee(string) SwigcptrEmployee
func NewEmployee(arg1 string) Employee {
return _swig_wrap_new_Employee(arg1)
}
func _swig_wrap_Employee_getTitle(SwigcptrEmployee) string
func (arg1 SwigcptrEmployee) GetTitle() string {
return _swig_wrap_Employee_getTitle(arg1)
}
func _swig_wrap_Employee_getName(SwigcptrEmployee) string
func (arg1 SwigcptrEmployee) GetName() string {
return _swig_wrap_Employee_getName(arg1)
}
func _swig_wrap_Employee_getPosition(SwigcptrEmployee) string
func (arg1 SwigcptrEmployee) GetPosition() string {
return _swig_wrap_Employee_getPosition(arg1)
}
func _swig_wrap_delete_Employee(uintptr)
func DeleteEmployee(arg1 Employee) {
_swig_wrap_delete_Employee(arg1.Swigcptr())
}
type Employee interface {
Swigcptr() uintptr
SwigIsEmployee()
DirectorInterface() interface{}
GetTitle() string
GetName() string
GetPosition() string
}
type _swig_DirectorManager struct {
SwigcptrManager
v interface{}
}
func (p *_swig_DirectorManager) Swigcptr() uintptr {
return p.SwigcptrManager.Swigcptr()
}
func (p *_swig_DirectorManager) SwigIsManager() {
}
func (p *_swig_DirectorManager) DirectorInterface() interface{} {
return p.v
}
func _swig_NewDirectorManagerManager(*_swig_DirectorManager, string) SwigcptrManager
func NewDirectorManager(v interface{}, arg1 string) Manager {
p := &_swig_DirectorManager{0, v}
p.SwigcptrManager = _swig_NewDirectorManagerManager(p, arg1)
return p
}
type _swig_DirectorInterfaceManagerGetTitle interface {
GetTitle() string
}
func _swig_wrap__swig_DirectorManager_upcall_GetTitle(SwigcptrManager) string
func (swig_p *_swig_DirectorManager) GetTitle() string {
if swig_g, swig_ok := swig_p.v.(_swig_DirectorInterfaceManagerGetTitle); swig_ok {
return swig_g.GetTitle()
}
return _swig_wrap__swig_DirectorManager_upcall_GetTitle(swig_p.SwigcptrManager)
}
func DirectorManagerGetTitle(p Manager) string {
return _swig_wrap__swig_DirectorManager_upcall_GetTitle(p.(*_swig_DirectorManager).SwigcptrManager)
}
func Swig_DirectorManager_callback_getTitle(p *_swig_DirectorManager) (swig_result string) {
return p.GetTitle()
}
type _swig_DirectorInterfaceManagerGetName interface {
GetName() string
}
func _swig_wrap__swig_DirectorManager_upcall_GetName(SwigcptrManager) string
func (swig_p *_swig_DirectorManager) GetName() string {
if swig_g, swig_ok := swig_p.v.(_swig_DirectorInterfaceManagerGetName); swig_ok {
return swig_g.GetName()
}
return _swig_wrap__swig_DirectorManager_upcall_GetName(swig_p.SwigcptrManager)
}
func DirectorManagerGetName(p Manager) string {
return _swig_wrap__swig_DirectorManager_upcall_GetName(p.(*_swig_DirectorManager).SwigcptrManager)
}
func Swig_DirectorManager_callback_getName(p *_swig_DirectorManager) (swig_result string) {
return p.GetName()
}
type _swig_DirectorInterfaceManagerGetPosition interface {
GetPosition() string
}
func _swig_wrap__swig_DirectorManager_upcall_GetPosition(SwigcptrManager) string
func (swig_p *_swig_DirectorManager) GetPosition() string {
if swig_g, swig_ok := swig_p.v.(_swig_DirectorInterfaceManagerGetPosition); swig_ok {
return swig_g.GetPosition()
}
return _swig_wrap__swig_DirectorManager_upcall_GetPosition(swig_p.SwigcptrManager)
}
func DirectorManagerGetPosition(p Manager) string {
return _swig_wrap__swig_DirectorManager_upcall_GetPosition(p.(*_swig_DirectorManager).SwigcptrManager)
}
func Swig_DirectorManager_callback_getPosition(p *_swig_DirectorManager) (swig_result string) {
return p.GetPosition()
}
func _swig_wrap_DeleteDirectorManager(uintptr)
func DeleteDirectorManager(arg1 Manager) {
_swig_wrap_DeleteDirectorManager(arg1.Swigcptr())
}
func Swiggo_DeleteDirector_Manager(p *_swig_DirectorManager) {
p.SwigcptrManager = 0
}
type SwigcptrManager uintptr
func (p SwigcptrManager) Swigcptr() uintptr {
return (uintptr)(p)
}
func (p SwigcptrManager) SwigIsManager() {
}
func (p SwigcptrManager) DirectorInterface() interface{} {
return nil
}
func _swig_wrap_new_Manager(string) SwigcptrManager
func NewManager(arg1 string) Manager {
return _swig_wrap_new_Manager(arg1)
}
func _swig_wrap_Manager_getPosition(SwigcptrManager) string
func (arg1 SwigcptrManager) GetPosition() string {
return _swig_wrap_Manager_getPosition(arg1)
}
func _swig_wrap_delete_Manager(uintptr)
func DeleteManager(arg1 Manager) {
_swig_wrap_delete_Manager(arg1.Swigcptr())
}
func _swig_wrap_Manager_getTitle(SwigcptrManager) string
func (_swig_base SwigcptrManager) GetTitle() string {
return _swig_wrap_Manager_getTitle(_swig_base)
}
func _swig_wrap_Manager_getName(SwigcptrManager) string
func (_swig_base SwigcptrManager) GetName() string {
return _swig_wrap_Manager_getName(_swig_base)
}
func (p SwigcptrManager) SwigIsEmployee() {
}
func (p SwigcptrManager) SwigGetEmployee() Employee {
return SwigcptrEmployee(p.Swigcptr())
}
type Manager interface {
Swigcptr() uintptr
SwigIsManager()
DirectorInterface() interface{}
GetPosition() string
GetTitle() string
GetName() string
SwigIsEmployee()
SwigGetEmployee() Employee
}
type SwigcptrEmployeeList uintptr
func (p SwigcptrEmployeeList) Swigcptr() uintptr {
return (uintptr)(p)
}
func (p SwigcptrEmployeeList) SwigIsEmployeeList() {
}
func _swig_wrap_new_EmployeeList() SwigcptrEmployeeList
func NewEmployeeList() EmployeeList {
return _swig_wrap_new_EmployeeList()
}
func _swig_wrap_EmployeeList_addEmployee(SwigcptrEmployeeList, uintptr)
func (arg1 SwigcptrEmployeeList) AddEmployee(arg2 Employee) {
_swig_wrap_EmployeeList_addEmployee(arg1, arg2.Swigcptr())
}
func _swig_wrap_EmployeeList_get_item(SwigcptrEmployeeList, int) SwigcptrEmployee
func (arg1 SwigcptrEmployeeList) Get_item(arg2 int) Employee {
return _swig_wrap_EmployeeList_get_item(arg1, arg2)
}
func _swig_wrap_delete_EmployeeList(uintptr)
func DeleteEmployeeList(arg1 EmployeeList) {
_swig_wrap_delete_EmployeeList(arg1.Swigcptr())
}
type EmployeeList interface {
Swigcptr() uintptr
SwigIsEmployeeList()
AddEmployee(arg2 Employee)
Get_item(arg2 int) Employee
}
type SwigcptrSwigDirector_Manager uintptr
type SwigDirector_Manager interface {
Swigcptr() uintptr;
}
func (p SwigcptrSwigDirector_Manager) Swigcptr() uintptr {
return uintptr(p)
}
type SwigcptrSwigDirector_Employee uintptr
type SwigDirector_Employee interface {
Swigcptr() uintptr;
}
func (p SwigcptrSwigDirector_Employee) 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,56 @@
/* File : example.h */
#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
class Employee {
private:
std::string name;
public:
Employee(const char* n): name(n) {}
virtual std::string getTitle() { return getPosition() + " " + getName(); }
virtual std::string getName() { return name; }
virtual std::string getPosition() const { return "Employee"; }
virtual ~Employee() { printf("~Employee() @ %p\n", this); }
};
class Manager: public Employee {
public:
Manager(const char* n): Employee(n) {}
virtual std::string getPosition() const { return "Manager"; }
};
class EmployeeList {
std::vector<Employee*> list;
public:
EmployeeList() {
list.push_back(new Employee("Bob"));
list.push_back(new Employee("Jane"));
list.push_back(new Manager("Ted"));
}
void addEmployee(Employee *p) {
list.push_back(p);
std::cout << "New employee added. Current employees are:" << std::endl;
std::vector<Employee*>::iterator i;
for (i=list.begin(); i!=list.end(); i++) {
std::cout << " " << (*i)->getTitle() << std::endl;
}
}
const Employee *get_item(int i) {
return list[i];
}
~EmployeeList() {
std::vector<Employee*>::iterator i;
std::cout << "~EmployeeList, deleting " << list.size() << " employees." << std::endl;
for (i=list.begin(); i!=list.end(); i++) {
delete *i;
}
std::cout << "~EmployeeList empty." << std::endl;
}
};

View file

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

View file

@ -0,0 +1,27 @@
<html>
<head>
<title>SWIG:Examples:go:extend</title>
</head>
<body bgcolor="#ffffff">
<tt>SWIG/Examples/go/extend/</tt>
<hr>
<H2>Extending a simple C++ class in Go</H2>
<p>
This example illustrates the extending of a C++ class with cross
language polymorphism.
<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,77 @@
// This file illustrates the cross language polymorphism using directors.
package main
import (
"fmt"
. "./example"
)
type CEO struct{}
func (p *CEO) GetPosition() string {
return "CEO"
}
func main() {
// Create an instance of CEO, a class derived from the Go
// proxy of the underlying C++ class. The calls to getName()
// and getPosition() are standard, the call to getTitle() uses
// the director wrappers to call CEO.getPosition().
e := NewDirectorManager(new(CEO), "Alice")
fmt.Println(e.GetName(), " is a ", e.GetPosition())
fmt.Println("Just call her \"", e.GetTitle(), "\"")
fmt.Println("----------------------")
// Create a new EmployeeList instance. This class does not
// have a C++ director wrapper, but can be used freely with
// other classes that do.
list := NewEmployeeList()
// EmployeeList owns its items, so we must surrender ownership
// of objects we add.
// e.DisownMemory()
list.AddEmployee(e)
fmt.Println("----------------------")
// Now we access the first four items in list (three are C++
// objects that EmployeeList's constructor adds, the last is
// our CEO). The virtual methods of all these instances are
// treated the same. For items 0, 1, and 2, all methods
// resolve in C++. For item 3, our CEO, GetTitle calls
// GetPosition which resolves in Go. The call to GetPosition
// is slightly different, however, because of the overidden
// GetPosition() call, since now the object reference has been
// "laundered" by passing through EmployeeList as an
// Employee*. Previously, Go resolved the call immediately in
// CEO, but now Go thinks the object is an instance of class
// Employee. So the call passes through the Employee proxy
// class and on to the C wrappers and C++ director, eventually
// ending up back at the Java CEO implementation of
// getPosition(). The call to GetTitle() for item 3 runs the
// C++ Employee::getTitle() method, which in turn calls
// GetPosition(). This virtual method call passes down
// through the C++ director class to the Java implementation
// in CEO. All this routing takes place transparently.
fmt.Println("(position, title) for items 0-3:")
fmt.Println(" ", list.Get_item(0).GetPosition(), ", \"", list.Get_item(0).GetTitle(), "\"")
fmt.Println(" ", list.Get_item(1).GetPosition(), ", \"", list.Get_item(1).GetTitle(), "\"")
fmt.Println(" ", list.Get_item(2).GetPosition(), ", \"", list.Get_item(2).GetTitle(), "\"")
fmt.Println(" ", list.Get_item(3).GetPosition(), ", \"", list.Get_item(3).GetTitle(), "\"")
fmt.Println("----------------------")
// Time to delete the EmployeeList, which will delete all the
// Employee* items it contains. The last item is our CEO,
// which gets destroyed as well.
DeleteEmployeeList(list)
fmt.Println("----------------------")
// All done.
fmt.Println("Go exit")
}