[Go] Updated the 'callback' and 'extend' examples to match the 'director' one.
After the documentation update on how to utilize the director feature with
commit @17b1c1c the 'callback' and 'extend' examples needed an update as well.
This commit is contained in:
parent
a941e5b605
commit
85037c3a33
12 changed files with 130 additions and 100 deletions
|
|
@ -1,6 +1,7 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
CXXSRCS = extend.cxx
|
||||
GOSRCS = ceo.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
|
||||
|
|
|
|||
37
Examples/go/extend/ceo.go
Normal file
37
Examples/go/extend/ceo.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package example
|
||||
|
||||
type CEO interface {
|
||||
Manager
|
||||
deleteManager()
|
||||
IsCEO()
|
||||
}
|
||||
|
||||
type ceo struct {
|
||||
Manager
|
||||
}
|
||||
|
||||
func (p *ceo) deleteManager() {
|
||||
DeleteDirectorManager(p.Manager)
|
||||
}
|
||||
|
||||
func (p *ceo) IsCEO() {}
|
||||
|
||||
type overwrittenMethodsOnManager struct {
|
||||
p Manager
|
||||
}
|
||||
|
||||
func NewCEO(name string) CEO {
|
||||
om := &overwrittenMethodsOnManager{}
|
||||
p := NewDirectorManager(om, name)
|
||||
om.p = p
|
||||
|
||||
return &ceo{Manager: p}
|
||||
}
|
||||
|
||||
func DeleteCEO(p CEO) {
|
||||
p.deleteManager()
|
||||
}
|
||||
|
||||
func (p *ceo) GetPosition() string {
|
||||
return "CEO"
|
||||
}
|
||||
|
|
@ -44,7 +44,7 @@ public:
|
|||
const Employee *get_item(int i) {
|
||||
return list[i];
|
||||
}
|
||||
~EmployeeList() {
|
||||
~EmployeeList() {
|
||||
std::vector<Employee*>::iterator i;
|
||||
std::cout << "~EmployeeList, deleting " << list.size() << " employees." << std::endl;
|
||||
for (i=list.begin(); i!=list.end(); i++) {
|
||||
|
|
|
|||
|
|
@ -12,13 +12,16 @@
|
|||
<H2>Extending a simple C++ class in Go</H2>
|
||||
|
||||
<p>
|
||||
This example illustrates the extending of a C++ class with cross
|
||||
language polymorphism.
|
||||
|
||||
This example illustrates how to inherit from a C++ class 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>
|
||||
|
||||
<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="ceo.go">ceo.go</a>. Go source with the definition of the CEO class.
|
||||
<li><a href="example.h">example.h</a>. Header with the definition of the Employee, Manager and EmployeeList classes.
|
||||
<li><a href="example.i">example.i</a>. SWIG interface file.
|
||||
<li><a href="runme.go">runme.go</a>. Sample Go program.
|
||||
</ul>
|
||||
|
||||
|
|
|
|||
|
|
@ -7,19 +7,12 @@ import (
|
|||
"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")
|
||||
e := NewCEO("Alice")
|
||||
fmt.Println(e.GetName(), " is a ", e.GetPosition())
|
||||
fmt.Println("Just call her \"", e.GetTitle(), "\"")
|
||||
fmt.Println("----------------------")
|
||||
|
|
@ -27,7 +20,6 @@ func main() {
|
|||
// 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
|
||||
|
|
@ -49,15 +41,13 @@ func main() {
|
|||
// 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
|
||||
// ending up back at the Go 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
|
||||
// through the C++ director class to the Go 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(), "\"")
|
||||
|
|
@ -66,11 +56,11 @@ func main() {
|
|||
|
||||
// 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.
|
||||
// which gets destroyed as well and hence there is no need to
|
||||
// call DeleteCEO.
|
||||
DeleteEmployeeList(list)
|
||||
fmt.Println("----------------------")
|
||||
|
||||
// All done.
|
||||
|
||||
fmt.Println("Go exit")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue