[Go] Revert commit 5e88857 to undelete the 'callback' and 'extend' examples.
The 'callback' and 'extend' examples were presumed to be obsoleted by the new
'director' example. The examples are helpful though to have similar examples
across target languages and hence the commit @5e88857 which removed these
examples got reverted.
This commit is contained in:
parent
19a20c794b
commit
a941e5b605
14 changed files with 374 additions and 1 deletions
16
Examples/go/callback/Makefile
Normal file
16
Examples/go/callback/Makefile
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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
|
||||
4
Examples/go/callback/callback.cxx
Normal file
4
Examples/go/callback/callback.cxx
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
/* File : example.cxx */
|
||||
|
||||
#include "example.h"
|
||||
|
||||
23
Examples/go/callback/example.h
Normal file
23
Examples/go/callback/example.h
Normal 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(); }
|
||||
};
|
||||
|
||||
11
Examples/go/callback/example.i
Normal file
11
Examples/go/callback/example.i
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
/* File : example.i */
|
||||
%module(directors="1") example
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* turn on director wrapping Callback */
|
||||
%feature("director") Callback;
|
||||
|
||||
%include "example.h"
|
||||
|
||||
81
Examples/go/callback/index.html
Normal file
81
Examples/go/callback/index.html
Normal 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>
|
||||
41
Examples/go/callback/runme.go
Normal file
41
Examples/go/callback/runme.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
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")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue