summaryrefslogtreecommitdiff
path: root/Examples/test-suite/go_director_inout.i
blob: 38059ea40eb06096e6de6688ef8c42327a00ec9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Test the goin and goout typemaps for directors.

%module(directors="1") go_director_inout

%include <std_string.i>

%{
#include <string>
%}

%inline
%{

struct MyStruct {
  std::string str;
};

struct RetStruct {
  std::string str;
};

%}

%go_import("encoding/json")

%insert(go_header)
%{

type GoRetStruct struct {
	Str string
}

%}

%typemap(gotype) RetStruct "GoRetStruct"

%typemap(imtype) RetStruct "string"

%typemap(goin) RetStruct
%{
	$result = $input.Str
%}

%typemap(in) RetStruct
%{
  $result.str.assign($input.p, $input.n);
%}

%typemap(out,fragment="AllocateString") RetStruct
%{
  $result = Swig_AllocateString($1.str.data(), $1.str.length());
%}

%typemap(goout,fragment="CopyString") RetStruct
%{
	$result = GoRetStruct{Str: swigCopyString($input)}
%}

%typemap(godirectorout) RetStruct
%{
	$result = $input.Str
%}

%typemap(directorout) RetStruct
%{
  $result.str.assign($input.p, $input.n);
%}

%typemap(godirectorin) RetStruct
%{
%}

%typemap(gotype) MyStruct "map[string]interface{}"

%typemap(imtype) MyStruct "string"

%typemap(goin) MyStruct
%{
	if b, err := json.Marshal($input); err != nil {
		panic(err)
	} else {
		$result = string(b)
	}
%}

%typemap(directorin,fragment="AllocateString") MyStruct
%{
  $input = Swig_AllocateString($1.str.data(), $1.str.length());
%}

%typemap(godirectorin,fragment="CopyString") MyStruct
%{
	if err := json.Unmarshal([]byte(swigCopyString($input)), &$result); err != nil {
		panic(err)
	}
%}

%typemap(out,fragment="AllocateString") MyStruct
%{
  $result = Swig_AllocateString($1.str.data(), $1.str.length());
%}

%typemap(goout,fragment="CopyString") MyStruct
%{
	$result = swigCopyString($input)
%}

%typemap(in) MyStruct
%{
  $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);
}

%feature("director") MyClass;

%inline
%{

class MyClass {
 public:
  virtual ~MyClass() {}
  virtual RetStruct Adjust(MyStruct s) {
    RetStruct r;
    r.str = s.str;
    return r;
  }

  virtual void S1(std::string s) = 0;
  virtual void S2(std::string& s) = 0;
  virtual void S3(std::string* s) = 0;
};

%}