summaryrefslogtreecommitdiff
path: root/Examples/go/callback
diff options
context:
space:
mode:
authorWilliam S Fulton <wsf@fultondesigns.co.uk>2015-08-12 18:30:49 +0100
committerWilliam S Fulton <wsf@fultondesigns.co.uk>2015-08-12 18:30:49 +0100
commit4de4123b6a990b0e6747499cb6b3d0c14643876f (patch)
tree19ddd198af1859705da972bb2984123991212d15 /Examples/go/callback
parent5817a1163dd3ec01cbcd2c314484d69f7e4e27cf (diff)
parent9aa0f85cdacff59180cde416d5b10406cff1bda1 (diff)
downloadswig-travis-osx.tar.gz
Merge branch 'master' into travis-osxtravis-osx
* master: Workaround Appveyor random failures due to nuget install errors Revert "Merge pull request #494 from richardbeare/enumR2015B" This is a modification to support use of tricky enumerations in R. It includes the addition of a _runme for an existing test - preproc_constants that was previously not run. That tests includes a preprocessor based setting of an enumeration which is ignored by the existing r enumeration infrastructure. The new version correctly reports the enumeration value as 4 - previous versions set it to 0. Traditional enumerations are unchanged. [Go] Renamed 'FooBarAbs' to 'FooBarAbstract' in the documentation and examples. [Go] Updated the 'callback' and 'extend' examples to match the 'director' one. [Go] Revert commit 5e88857 to undelete the 'callback' and 'extend' examples. update CHANGES.current check ranges in perlprimtype.swg more carefully to avoid clang warnings capture the current behavior of perlprimtypes.swg is more detail Changes entry for numpydoc conforming docstrings. pep257 & numpydoc conforming docstrings Make (char*, size_t) typemap usable for strings of other types in Java. [Go] update build instructions in Examples/go/index.html [Go] Documentation cleanup of obsolete 'callback' and 'extend' examples. Some minor changes after first code review by ianlancetaylor. Fixed Examples/go/director/Makefile as there might be no copy of director.go during clean if a separate build directory is in use. Fixed Examples/go/director/Makefile as the copy of director.go wasn't cleaned up in separate build directories. Fixed Examples/go/director/Makefile as director.go was missing in separate build directories. Removed empty line in table of contents of the Go documentation. Fleshed out Go's documentation about the director feature and added a director example. Bump version to 3.0.8 Update Scilab test-suite output wording Add 3.0.7 release summary and release date Test case warning suppression for visual c++ fix
Diffstat (limited to 'Examples/go/callback')
-rw-r--r--Examples/go/callback/Makefile12
-rw-r--r--Examples/go/callback/example.h1
-rw-r--r--Examples/go/callback/gocallback.go41
-rw-r--r--Examples/go/callback/index.html64
-rw-r--r--Examples/go/callback/runme.go16
5 files changed, 62 insertions, 72 deletions
diff --git a/Examples/go/callback/Makefile b/Examples/go/callback/Makefile
index bf5275f14..7441e09bd 100644
--- a/Examples/go/callback/Makefile
+++ b/Examples/go/callback/Makefile
@@ -1,6 +1,7 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
CXXSRCS = callback.cxx
+GOSRCS = gocallback.go
TARGET = example
INTERFACE = example.i
SWIGOPT =
@@ -9,8 +10,15 @@ 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
+ if [ -n '$(SRCDIR)' ]; then \
+ cp $(GOSRCS:%=$(SRCDIR)/%) .; \
+ fi
+ @# Note: example.go gets generated by SWIG
+ $(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' CXXSRCS='$(CXXSRCS)' GOSRCS='example.go $(GOSRCS)' \
+ SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_cpp
clean:
+ if [ -n '$(SRCDIR)' ]; then \
+ rm $(GOSRCS) || true; \
+ fi
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' INTERFACE='$(INTERFACE)' go_clean
diff --git a/Examples/go/callback/example.h b/Examples/go/callback/example.h
index 1a0e8c432..74ddad954 100644
--- a/Examples/go/callback/example.h
+++ b/Examples/go/callback/example.h
@@ -20,4 +20,3 @@ public:
void setCallback(Callback *cb) { delCallback(); _callback = cb; }
void call() { if (_callback) _callback->run(); }
};
-
diff --git a/Examples/go/callback/gocallback.go b/Examples/go/callback/gocallback.go
new file mode 100644
index 000000000..20fd0627a
--- /dev/null
+++ b/Examples/go/callback/gocallback.go
@@ -0,0 +1,41 @@
+package example
+
+import (
+ "fmt"
+)
+
+type GoCallback interface {
+ Callback
+ deleteCallback()
+ IsGoCallback()
+}
+
+type goCallback struct {
+ Callback
+}
+
+func (p *goCallback) deleteCallback() {
+ DeleteDirectorCallback(p.Callback)
+}
+
+func (p *goCallback) IsGoCallback() {}
+
+type overwrittenMethodsOnCallback struct {
+ p Callback
+}
+
+func NewGoCallback() GoCallback {
+ om := &overwrittenMethodsOnCallback{}
+ p := NewDirectorCallback(om)
+ om.p = p
+
+ return &goCallback{Callback: p}
+}
+
+func DeleteGoCallback(p GoCallback) {
+ p.deleteCallback()
+}
+
+func (p *goCallback) Run() {
+ fmt.Println("GoCallback.Run")
+}
diff --git a/Examples/go/callback/index.html b/Examples/go/callback/index.html
index b053cf547..9a53065b0 100644
--- a/Examples/go/callback/index.html
+++ b/Examples/go/callback/index.html
@@ -12,67 +12,17 @@
<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.
+This example illustrates how to use directors to implement C++ callbacks in Go.
+See the <a href="../../../Doc/Manual/Go.html#Go_director_classes">Go Director
+Classes</a> documentation subsection for an in-depth explanation how to use the
+director feature.
</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="example.h">example.h</a>. Header with the definition of the Caller and Callback classes.
+<li><a href="example.i">example.i</a>. SWIG interface file.
+<li><a href="gocallback.go">gocallback.go</a>. Go source with the definition of the GoCallback class.
<li><a href="runme.go">runme.go</a>. Sample Go program.
</ul>
diff --git a/Examples/go/callback/runme.go b/Examples/go/callback/runme.go
index 2eef77fdb..03ab0c5e2 100644
--- a/Examples/go/callback/runme.go
+++ b/Examples/go/callback/runme.go
@@ -16,26 +16,18 @@ func main() {
caller.Call()
caller.DelCallback()
- callback = NewDirectorCallback(new(GoCallback))
+ go_callback := NewGoCallback()
fmt.Println()
fmt.Println("Adding and calling a Go callback")
- fmt.Println("------------------------------------")
+ fmt.Println("--------------------------------")
- caller.SetCallback(callback)
+ caller.SetCallback(go_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)
+ DeleteGoCallback(go_callback)
fmt.Println()
fmt.Println("Go exit")
}
-
-type GoCallback struct{}
-
-func (p *GoCallback) Run() {
- fmt.Println("GoCallback.Run")
-}