aboutsummaryrefslogtreecommitdiff
path: root/Examples/test-suite/go_director_inout.i
diff options
context:
space:
mode:
Diffstat (limited to 'Examples/test-suite/go_director_inout.i')
-rw-r--r--Examples/test-suite/go_director_inout.i125
1 files changed, 125 insertions, 0 deletions
diff --git a/Examples/test-suite/go_director_inout.i b/Examples/test-suite/go_director_inout.i
index 5a7fbdf89..0be3ca1f7 100644
--- a/Examples/test-suite/go_director_inout.i
+++ b/Examples/test-suite/go_director_inout.i
@@ -2,6 +2,8 @@
%module(directors="1") go_director_inout
+%include <std_string.i>
+
%{
#include <string>
%}
@@ -108,6 +110,102 @@ type GoRetStruct struct {
$1.str.assign($input.p, $input.n);
%}
+%typemap(directorin) std::string & (_gostring_ temp) {
+ $input = &temp;
+ temp.p = (char *) $1.data();
+ temp.n = $1.size();
+}
+%typemap(directorargout) std::string & {
+ _gostring_ *tmp = $input;
+ $1.assign(tmp->p, tmp->p + tmp->n);
+}
+
+%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") MyClass;
%inline
@@ -121,6 +219,33 @@ class MyClass {
r.str = s.str;
return r;
}
+
+ void CallS4(const char * const *strarray);
+ virtual void S1(std::string s);
+ virtual void S2(std::string& s) = 0;
+ virtual void S3(std::string* s) = 0;
+ virtual void S4(const char * const *strarray);
+ virtual int S5(const std::string* s);
};
+void MyClass::S1(std::string s) {
+ throw "Base S1 called!";
+}
+
+void MyClass::S4(const char * const *strarray) {
+ throw "Base S4 called!";
+}
+
+void MyClass::CallS4(const char * const *strarray) {
+ this->S4(strarray);
+}
+
+int MyClass::S5(const std::string* s) {
+ if (s) {
+ return (*s)[0];
+ } else {
+ return 0;
+ }
+}
+
%}