diff options
Diffstat (limited to 'Examples/go')
-rw-r--r-- | Examples/go/check.list | 1 | ||||
-rw-r--r-- | Examples/go/goin/Makefile | 18 | ||||
-rw-r--r-- | Examples/go/goin/example.i | 106 | ||||
-rw-r--r-- | Examples/go/goin/index.html | 26 | ||||
-rw-r--r-- | Examples/go/goin/runme.go | 38 | ||||
-rw-r--r-- | Examples/go/index.html | 3 | ||||
-rw-r--r-- | Examples/go/reference/index.html | 2 | ||||
-rw-r--r-- | Examples/go/template/index.html | 4 |
8 files changed, 194 insertions, 4 deletions
diff --git a/Examples/go/check.list b/Examples/go/check.list index b3f34b306..6046c8310 100644 --- a/Examples/go/check.list +++ b/Examples/go/check.list @@ -12,3 +12,4 @@ reference simple template variables +goin diff --git a/Examples/go/goin/Makefile b/Examples/go/goin/Makefile new file mode 100644 index 000000000..f79b083cb --- /dev/null +++ b/Examples/go/goin/Makefile @@ -0,0 +1,18 @@ +TOP = ../.. +SWIGEXE = $(TOP)/../swig +SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib +CXXSRCS = +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_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_cpp + +clean: + $(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' INTERFACE='$(INTERFACE)' go_clean diff --git a/Examples/go/goin/example.i b/Examples/go/goin/example.i new file mode 100644 index 000000000..b819b8089 --- /dev/null +++ b/Examples/go/goin/example.i @@ -0,0 +1,106 @@ + +%module(directors="1") example + +%inline %{ +// Helper functions for converting string arrays +#include <stdlib.h> +void *alloc_ptr_array(unsigned int len) +{ + return calloc(len, sizeof(void *)); +} +void set_ptr_array(void *ain, unsigned int pos, void *val) +{ + void **a = (void **) ain; + a[pos] = val; +} +void *get_ptr_array(void *ain, unsigned int pos) +{ + void **a = (void **) ain; + return a[pos]; +} +void free_ptr_array(void *ain) +{ + void **a = (void **) ain; + unsigned int i; + + if (!a) + return; + for (i = 0; a[i]; i++) { + free(a[i]); + } + free(a); +} +char *uintptr_to_string(void *in) +{ + return (char *) in; +} +void *string_to_uintptr(char *in) +{ + return strdup(in); +} +%} + +// These typemaps convert between an array of strings in Go and a +// const char** that is NULL terminated in C++. +%typemap(gotype) (const char * const *) "[]string" +%typemap(imtype) (const char * const *) "uintptr" +%typemap(goin) (const char * const *) { + if $input == nil || len($input) == 0 { + $result = 0 + } else { + $result = Alloc_ptr_array(uint(len($input) + 1)) + defer func() { + Free_ptr_array($result) + }() + var i uint + for i = 0; i < uint(len($input)); i++ { + Set_ptr_array($result, i, String_to_uintptr($input[i])) + } + } +} +%typemap(in) (const char * const *) { + $1 = (char **) $input; +} +%typemap(godirectorin) (const char * const *) { + if ($input == 0) { + $result = nil + } else { + var i uint + for i = 0; ; i++ { + var v uintptr = Get_ptr_array($input, i) + if v == 0 { + break + } + } + if i == 0 { + $result = nil + } else { + $result = make([]string, i) + for i = 0; ; i++ { + var v uintptr = Get_ptr_array($input, i) + if v == 0 { + break + } + $result[i] = Uintptr_to_string(v) + } + } + } +} + +%feature("director") callbacks; + +%inline %{ + class callbacks { + public: + virtual bool call1(int v, const char * const *strarray); + virtual ~callbacks() {} + }; + + bool check1(callbacks *c, int v, const char * const *strarray) { + return c->call1(v, strarray); + } + + bool callbacks::call1(int v, const char * const *strarray) { + return false; + } +%} diff --git a/Examples/go/goin/index.html b/Examples/go/goin/index.html new file mode 100644 index 000000000..852b068da --- /dev/null +++ b/Examples/go/goin/index.html @@ -0,0 +1,26 @@ +<html> +<head> +<title>SWIG:Examples:go:going</title> +</head> + +<body bgcolor="#ffffff"> + + +<tt>SWIG/Examples/go/goin/</tt> +<hr> + +<H2>Example of using goin and godirectorin</H2> + +<p> +This example converts between a Go []string and a "const char * const *" +in C/C++. It does this for a director and for a normal call. + +<p> +<ul> +<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> diff --git a/Examples/go/goin/runme.go b/Examples/go/goin/runme.go new file mode 100644 index 000000000..dfc7b0936 --- /dev/null +++ b/Examples/go/goin/runme.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" + "swigtests/example" +) + +type mycallbacks struct { + example.Callbacks +} + +var tststrs = []string{ "A", "BCD", "EFGH" } +var tstint int = 5 + +func (v *mycallbacks) Call1(val int, strarray []string) bool { + var rv bool = true + + for i, s := range strarray { + fmt.Printf("%d: %s\n", i, s) + if s != tststrs[i] { + fmt.Printf(" ***Mismatch, expected %s\n", tststrs[i]) + rv = false + } + } + if val != tstint { + rv = false + } + return rv +} + +func main() { + cbs := &mycallbacks{} + cbs.Callbacks = example.NewDirectorCallbacks(cbs) + worked := example.Check1(cbs, tstint, tststrs) + if !worked { + panic("Data mismatch") + } +} diff --git a/Examples/go/index.html b/Examples/go/index.html index 467f4ecb7..b8af100ff 100644 --- a/Examples/go/index.html +++ b/Examples/go/index.html @@ -24,6 +24,7 @@ certain C declarations are turned into constants. <li><a href="callback/index.html">callback</a>. C++ callbacks using directors. <li><a href="extend/index.html">extend</a>. Polymorphism using directors. <li><a href="director/index.html">director</a>. Example how to utilize the director feature. +<li><a href="goin/index.html">director</a>. Example how to use goin and godirectorin. </ul> <h2>Compilation Issues</h2> @@ -93,6 +94,6 @@ All of the examples were last tested with the following configuration </ul> Your mileage may vary. If you experience a problem, please let us know by -contacting us on the <a href="http://www.swig.org/mail.html">mailing lists</a>. +contacting us on the <a href="https://www.swig.org/mail.html">mailing lists</a>. </body> </html> diff --git a/Examples/go/reference/index.html b/Examples/go/reference/index.html index 5e8589349..ebf366bc0 100644 --- a/Examples/go/reference/index.html +++ b/Examples/go/reference/index.html @@ -99,7 +99,7 @@ functions like this: class VectorArray { public: ... - %addmethods { + %extend { Vector &get(int index) { return (*self)[index]; } diff --git a/Examples/go/template/index.html b/Examples/go/template/index.html index cf2b1337b..a389c196a 100644 --- a/Examples/go/template/index.html +++ b/Examples/go/template/index.html @@ -42,7 +42,7 @@ template<class T> class vector { v[index] = val; } #ifdef SWIG - %addmethods { + %extend { T getitem(int index) { return self->get(index); } @@ -54,7 +54,7 @@ template<class T> class vector { }; </pre> </blockquote> -The %addmethods is used for a neater interface from Go as the +The %extend is used for a neater interface from Go as the functions <tt>get</tt> and <tt>set</tt> use C++ references to primitive types. These are tricky to use from Go as they end up as pointers, which only work when the C++ and Go types correspond |