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 =
GOSRCS = director.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,72 @@
package example
// FooBarGo is a superset of FooBarAbstract and hence FooBarGo can be used as a
// drop in replacement for FooBarAbstract but the reverse causes a compile time
// error.
type FooBarGo interface {
FooBarAbstract
deleteFooBarAbstract()
IsFooBarGo()
}
// Via embedding fooBarGo "inherits" all methods of FooBarAbstract.
type fooBarGo struct {
FooBarAbstract
}
func (fbgs *fooBarGo) deleteFooBarAbstract() {
DeleteDirectorFooBarAbstract(fbgs.FooBarAbstract)
}
// The IsFooBarGo method ensures that FooBarGo is a superset of FooBarAbstract.
// This is also how the class hierarchy gets represented by the SWIG generated
// wrapper code. For an instance FooBarCpp has the IsFooBarAbstract and
// IsFooBarCpp methods.
func (fbgs *fooBarGo) IsFooBarGo() {}
// Go type that defines the DirectorInterface. It contains the Foo and Bar
// methods that overwrite the respective virtual C++ methods on FooBarAbstract.
type overwrittenMethodsOnFooBarAbstract struct {
// Backlink to FooBarAbstract so that the rest of the class can be used by
// the overridden methods.
fb FooBarAbstract
// If additional constructor arguments have been given they are typically
// stored here so that the overriden methods can use them.
}
func (om *overwrittenMethodsOnFooBarAbstract) Foo() string {
// DirectorFooBarAbstractFoo calls the base method FooBarAbstract::Foo.
return "Go " + DirectorFooBarAbstractFoo(om.fb)
}
func (om *overwrittenMethodsOnFooBarAbstract) Bar() string {
return "Go Bar"
}
func NewFooBarGo() FooBarGo {
// Instantiate FooBarAbstract with selected methods overridden. The methods
// that will be overwritten are defined on
// overwrittenMethodsOnFooBarAbstract and have a compatible signature to the
// respective virtual C++ methods. Furthermore additional constructor
// arguments will be typically stored in the
// overwrittenMethodsOnFooBarAbstract struct.
om := &overwrittenMethodsOnFooBarAbstract{}
fb := NewDirectorFooBarAbstract(om)
om.fb = fb // Backlink causes cycle as fb.v = om!
fbgs := &fooBarGo{FooBarAbstract: fb}
// The memory of the FooBarAbstract director object instance can be
// automatically freed once the FooBarGo instance is garbage collected by
// uncommenting the following line. Please make sure to understand the
// runtime.SetFinalizer specific gotchas before doing this. Furthemore
// DeleteFooBarGo should be deleted if a finalizer is in use or the fooBarGo
// struct needs additional data to prevent double deletion.
// runtime.SetFinalizer(fbgs, FooBarGo.deleteFooBarAbstract)
return fbgs
}
// Recommended to be removed if runtime.SetFinalizer is in use.
func DeleteFooBarGo(fbg FooBarGo) {
fbg.deleteFooBarAbstract()
}

View File

@@ -0,0 +1,41 @@
#ifndef DIRECTOR_H
#define DIRECTOR_H
#include <stdio.h>
#include <string>
class FooBarAbstract
{
public:
FooBarAbstract() {};
virtual ~FooBarAbstract() {};
std::string FooBar() {
return this->Foo() + ", " + this->Bar();
};
protected:
virtual std::string Foo() {
return "Foo";
};
virtual std::string Bar() = 0;
};
class FooBarCpp : public FooBarAbstract
{
protected:
virtual std::string Foo() {
return "C++ " + FooBarAbstract::Foo();
}
virtual std::string Bar() {
return "C++ Bar";
}
};
#endif

View File

@@ -0,0 +1,11 @@
/* File : example.i */
%module(directors="1") example
%include "std_string.i"
%header %{
#include "director.h"
%}
%feature("director") FooBarAbstract;
%include "director.h"

View File

@@ -0,0 +1,28 @@
<html>
<head>
<title>SWIG:Examples:go:director</title>
</head>
<body bgcolor="#ffffff">
<tt>SWIG/Examples/go/director/</tt>
<hr>
<H2>How to subclass a C++ class with a Go type</H2>
<p>
See the <a href="../../../Doc/Manual/Go.html#Go_director_classes">Go Director
Classes</a> documentation subsection for an explanation of this example.
</p>
<p>
<ul>
<li><a href="director.go">director.go</a>. Go source with the definition of the FooBarGo class.
<li><a href="director.h">director.h</a>. Header with the definition of the FooBarAbstract and FooBarCpp classes.
<li><a href="example.i">example.i</a>. SWIG interface file.
<li><a href="runme.go">runme.go</a>. Sample Go program.
</ul>
<hr>
</body>
</html>

View File

@@ -0,0 +1,39 @@
package main
import (
"./example"
"fmt"
"os"
)
func Compare(name string, got string, exp string) error {
fmt.Printf("%s; Got: '%s'; Expected: '%s'\n", name, got, exp)
if got != exp {
return fmt.Errorf("%s returned unexpected string! Got: '%s'; Expected: '%s'\n", name, got, exp)
}
return nil
}
func TestFooBarCpp() error {
fb := example.NewFooBarCpp()
defer example.DeleteFooBarCpp(fb)
return Compare("FooBarCpp.FooBar()", fb.FooBar(), "C++ Foo, C++ Bar")
}
func TestFooBarGo() error {
fb := example.NewFooBarGo()
defer example.DeleteFooBarGo(fb)
return Compare("FooBarGo.FooBar()", fb.FooBar(), "Go Foo, Go Bar")
}
func main() {
fmt.Println("Test output:")
fmt.Println("------------")
err := TestFooBarCpp()
err = TestFooBarGo()
fmt.Println("------------")
if err != nil {
fmt.Fprintf(os.Stderr, "Tests failed! Last error: %s\n", err.Error())
os.Exit(1)
}
}