Merge pull request #486 from michael-schaller/dir-cleanup
[Go] Cleanup of obsolete 'callback' and 'extend' examples.
This commit is contained in:
commit
5e88857760
14 changed files with 3 additions and 376 deletions
|
|
@ -1,16 +0,0 @@
|
||||||
TOP = ../..
|
|
||||||
SWIG = $(TOP)/../preinst-swig
|
|
||||||
CXXSRCS = callback.cxx
|
|
||||||
TARGET = example
|
|
||||||
INTERFACE = example.i
|
|
||||||
SWIGOPT =
|
|
||||||
|
|
||||||
check: build
|
|
||||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' CXXSRCS='$(CXXSRCS)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_run
|
|
||||||
|
|
||||||
build:
|
|
||||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
|
||||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_cpp
|
|
||||||
|
|
||||||
clean:
|
|
||||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' INTERFACE='$(INTERFACE)' go_clean
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
/* File : example.cxx */
|
|
||||||
|
|
||||||
#include "example.h"
|
|
||||||
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
/* 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(); }
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
/* File : example.i */
|
|
||||||
%module(directors="1") example
|
|
||||||
%{
|
|
||||||
#include "example.h"
|
|
||||||
%}
|
|
||||||
|
|
||||||
/* turn on director wrapping Callback */
|
|
||||||
%feature("director") Callback;
|
|
||||||
|
|
||||||
%include "example.h"
|
|
||||||
|
|
||||||
|
|
@ -1,81 +0,0 @@
|
||||||
<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>
|
|
||||||
|
|
@ -1,41 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
. "./example"
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
|
|
@ -1,10 +1,8 @@
|
||||||
# see top-level Makefile.in
|
# see top-level Makefile.in
|
||||||
callback
|
|
||||||
class
|
class
|
||||||
constants
|
constants
|
||||||
director
|
director
|
||||||
enum
|
enum
|
||||||
extend
|
|
||||||
funcptr
|
funcptr
|
||||||
multimap
|
multimap
|
||||||
pointer
|
pointer
|
||||||
|
|
|
||||||
|
|
@ -1,16 +0,0 @@
|
||||||
TOP = ../..
|
|
||||||
SWIG = $(TOP)/../preinst-swig
|
|
||||||
CXXSRCS = extend.cxx
|
|
||||||
TARGET = example
|
|
||||||
INTERFACE = example.i
|
|
||||||
SWIGOPT =
|
|
||||||
|
|
||||||
check: build
|
|
||||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' CXXSRCS='$(CXXSRCS)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_run
|
|
||||||
|
|
||||||
build:
|
|
||||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
|
||||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_cpp
|
|
||||||
|
|
||||||
clean:
|
|
||||||
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' INTERFACE='$(INTERFACE)' go_clean
|
|
||||||
|
|
@ -1,56 +0,0 @@
|
||||||
/* 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", (void *)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;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
/* 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"
|
|
||||||
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
/* File : example.cxx */
|
|
||||||
|
|
||||||
#include "example.h"
|
|
||||||
|
|
||||||
|
|
@ -1,27 +0,0 @@
|
||||||
<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>
|
|
||||||
|
|
@ -1,76 +0,0 @@
|
||||||
// This file illustrates the cross language polymorphism using directors.
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
. "./example"
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
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 overridden
|
|
||||||
// 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")
|
|
||||||
}
|
|
||||||
|
|
@ -21,8 +21,7 @@ certain C declarations are turned into constants.
|
||||||
<li><a href="pointer/index.html">pointer</a>. Simple pointer handling.
|
<li><a href="pointer/index.html">pointer</a>. Simple pointer handling.
|
||||||
<li><a href="funcptr/index.html">funcptr</a>. Pointers to functions.
|
<li><a href="funcptr/index.html">funcptr</a>. Pointers to functions.
|
||||||
<li><a href="template/index.html">template</a>. C++ templates.
|
<li><a href="template/index.html">template</a>. C++ templates.
|
||||||
<li><a href="callback/index.html">callback</a>. C++ callbacks using directors.
|
<li><a href="director/index.html">director</a>. Example how to utilize the director feature.
|
||||||
<li><a href="extend/index.html">extend</a>. Polymorphism using directors.
|
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<h2>Compilation Issues</h2>
|
<h2>Compilation Issues</h2>
|
||||||
|
|
@ -46,7 +45,7 @@ the <tt>6g</tt> or <tt>8g</tt> compiler, the steps look like this
|
||||||
<pre>
|
<pre>
|
||||||
% swig -go interface.i
|
% swig -go interface.i
|
||||||
% gcc -fpic -c interface_wrap.c
|
% gcc -fpic -c interface_wrap.c
|
||||||
% gcc -shared interface_wrap.o $(OBJS) -o interfacemodule.so
|
% gcc -shared interface_wrap.o $(OBJS) -o interfacemodule.so
|
||||||
% 6g interface.go
|
% 6g interface.go
|
||||||
% 6c interface_gc.c
|
% 6c interface_gc.c
|
||||||
% gopack grc interface.a interface.6 interface_gc.6
|
% gopack grc interface.a interface.6 interface_gc.6
|
||||||
|
|
@ -83,7 +82,7 @@ All of the examples were last tested with the following configuration
|
||||||
<li>gcc-4.2.4
|
<li>gcc-4.2.4
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
Your mileage may vary. If you experience a problem, please let us know by
|
Your mileage may vary. If you experience a problem, please let us know by
|
||||||
contacting us on the <a href="http://www.swig.org/mail.html">mailing lists</a>.
|
contacting us on the <a href="http://www.swig.org/mail.html">mailing lists</a>.
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue