Initial commit

This commit is contained in:
Bassem Girgis
2018-12-20 17:34:07 -06:00
parent 7a2d899662
commit 81b4b9e273
34743 changed files with 5940233 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
CXXSRCS = callback.cxx
GOSRCS = gocallback.go
TARGET = example
INTERFACE = example.i
SWIGOPT =
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' CXXSRCS='$(CXXSRCS)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_run
build:
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_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_cpp
clean:
if [ -n '$(SRCDIR)' ]; then \
rm -f $(GOSRCS); \
fi
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' INTERFACE='$(INTERFACE)' go_clean

View File

@@ -0,0 +1,4 @@
/* File : example.cxx */
#include "example.h"

View File

@@ -0,0 +1,22 @@
/* 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,11 @@
/* File : example.i */
%module(directors="1") example
%{
#include "example.h"
%}
/* turn on director wrapping Callback */
%feature("director") Callback;
%include "example.h"

View File

@@ -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")
}

View File

@@ -0,0 +1,31 @@
<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.
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 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>
<hr>
</body>
</html>

View File

@@ -0,0 +1,33 @@
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()
go_callback := NewGoCallback()
fmt.Println()
fmt.Println("Adding and calling a Go callback")
fmt.Println("--------------------------------")
caller.SetCallback(go_callback)
caller.Call()
caller.DelCallback()
DeleteGoCallback(go_callback)
fmt.Println()
fmt.Println("Go exit")
}