aboutsummaryrefslogtreecommitdiff
path: root/Examples/test-suite/valuewrapper_opaque.i
blob: bc7ba8683598e9d1dd8330e9a76c3828228dcdea (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
%module valuewrapper_opaque

/* 
 *  Opaque types
 */

#ifdef SWIGOCAML
%warnfilter(SWIGWARN_PARSE_KEYWORD) method;
#endif

%feature("valuewrapper") C;
class C;

%{
template<typename T> class TemplateClass {
public:
TemplateClass<T>(T a) {}
};

struct B
{
};

class C
{
public:
  C(int){}
};
%}


/*
 * Hint swig that the Opaque type B don't need the value wrapper.
 * This hint is only necessary in very very special cases.
 */
%feature("novaluewrapper") B;
class B;

/*
 * Force swig to use the value wrapper, even when the class
 * has a default constructor, in case you want to save a
 * instance construction.
 * 
 */
%feature("valuewrapper") D;
class D;


%feature("valuewrapper") A;
class A;

%feature("valuewrapper") TemplateClass<A>;
%feature("valuewrapper") TemplateClass<C>;
template<class T> class TemplateClass;

%feature("valuewrapper") BB;
class BB;


%inline %{

struct A 
{
  A(int){}
};

class D {};

class Klass {};


TemplateClass<Klass> getKlass(Klass k) {
  TemplateClass<Klass> t(k);
  return t;
}


TemplateClass<A> getA(A a) {
  TemplateClass<A> t(a);
  return t;
}


TemplateClass<B> getA(B b) {
  TemplateClass<B> t(b);
  return t;
}


TemplateClass<C> getC(C a) {
  TemplateClass<C> t(a);
  return t;
}


TemplateClass<int> getInt(int a) {
  TemplateClass<int> t(a);
  return t;
}

A sgetA(A a) {
  return a;
}

Klass sgetKlass(Klass a) {
  return a;
}

template <class T> 
struct auto_ptr
{
  auto_ptr(T a){}
};

auto_ptr<A> getPtrA(auto_ptr<A> a) {
  return a;
}

B getB(B a) {
  return a;
}

D getD(D a) {
  return a;
}
 
%}

%template() auto_ptr<A>;


/***** Another strange case, member var + opaque, bug #901706 ******/
%{
class BB {
friend class AA;

protected:
	BB(int aa) { this->a = aa; };
	BB() {};
	
	int a;
};
%}
  
%inline %{

class AA {
public:	
	AA(){}
	
	BB innerObj;
};

%}

%{
class Foobar
{
public:
  Foobar()
  {
  }
  
  char *foo_method()
  {
    return 0;
  }
  
};

class Quux
{
public:
  Quux()
  {
  }
  
  Foobar method()
  {
    return Foobar();
  }
  
};
%}

%feature("novaluewrapper") Foobar;
class Foobar;


class Quux {
public:
  Quux();
  
  Foobar method();

  
};


#if defined(SWIGPYTHON) 

/*
  This case can't be fixed by using the valuewrapper feature and the
  old mechanismbut it works fine with the new mechanism 
*/

%{
 
  // Template primitive type, only visible in C++
  template <class T>
  struct Param
  {
    T val;

    // This case is disabled by now
    // Param(T v): val(v) {}

    Param(T v = T()): val(v) {}
    
    operator T() const { return val; }
  };

%}

/*
  Several languages have 'not 100% safe' typemaps, 
  where the following %applies  don't work. 
*/
%apply int { Param<int> };
%apply const int& { const Param<int>& };

%apply double { Param<double> };
%apply const double& { const Param<double>& };

%inline %{

  template <class T>
  T getv(const Param<T>& p) 
  {
    return p.val;
  }

  template <class T>
  Param<T> getp(const T& v)
  {
    return  Param<T>(v);
  }
  
%}
  
%template(getv_i) getv<int>;
%template(getp_i) getp<int>;

%template(getv_d) getv<double>;
%template(getp_d) getp<double>;

#endif