Upstream version 1.3.40
[profile/ivi/swig.git] / Examples / test-suite / valuewrapper_opaque.i
1 %module valuewrapper_opaque
2
3 /* 
4  *  Opaque types
5  */
6
7 %feature("valuewrapper") C;
8 class C;
9
10 %{
11 template<typename T> class TemplateClass {
12 public:
13 TemplateClass<T>(T a) {}
14 };
15
16 struct B
17 {
18 };
19
20 class C
21 {
22 public:
23   C(int){}
24 };
25 %}
26
27
28 /*
29  * Hint swig that the Opaque type B don't need the value wrapper.
30  * This hint is only necessary in very very special cases.
31  */
32 %feature("novaluewrapper") B;
33 class B;
34
35 /*
36  * Force swig to use the value wrapper, even when the class
37  * has a default constructor, in case you want to save a
38  * instance construction.
39  * 
40  */
41 %feature("valuewrapper") D;
42 class D;
43
44
45 %feature("valuewrapper") A;
46 class A;
47
48 %feature("valuewrapper") TemplateClass<A>;
49 %feature("valuewrapper") TemplateClass<C>;
50 template<class T> class TemplateClass;
51
52 %feature("valuewrapper") BB;
53 class BB;
54
55
56 %inline %{
57
58 struct A 
59 {
60   A(int){}
61 };
62
63 class D {};
64
65 class Klass {};
66
67
68 TemplateClass<Klass> getKlass(Klass k) {
69   TemplateClass<Klass> t(k);
70   return t;
71 }
72
73
74 TemplateClass<A> getA(A a) {
75   TemplateClass<A> t(a);
76   return t;
77 }
78
79
80 TemplateClass<B> getA(B b) {
81   TemplateClass<B> t(b);
82   return t;
83 }
84
85
86 TemplateClass<C> getC(C a) {
87   TemplateClass<C> t(a);
88   return t;
89 }
90
91
92 TemplateClass<int> getInt(int a) {
93   TemplateClass<int> t(a);
94   return t;
95 }
96
97 A sgetA(A a) {
98   return a;
99 }
100
101 Klass sgetKlass(Klass a) {
102   return a;
103 }
104
105 template <class T> 
106 struct auto_ptr
107 {
108   auto_ptr(T a){}
109 };
110
111 auto_ptr<A> getPtrA(auto_ptr<A> a) {
112   return a;
113 }
114
115 B getB(B a) {
116   return a;
117 }
118
119 D getD(D a) {
120   return a;
121 }
122  
123 %}
124
125 %template() auto_ptr<A>;
126
127
128 /***** Another strange case, member var + opaque, bug #901706 ******/
129 %{
130 class BB {
131 friend class AA;
132
133 protected:
134         BB(int aa) { this->a = aa; };
135         BB() {};
136         
137         int a;
138 };
139 %}
140   
141 %inline %{
142
143 class AA {
144 public: 
145         AA(){}
146         
147         BB innerObj;
148 };
149
150 %}
151
152 %{
153 class Foobar
154 {
155 public:
156   Foobar()
157   {
158   }
159   
160   char *foo_method()
161   {
162     return 0;
163   }
164   
165 };
166
167 class Quux
168 {
169 public:
170   Quux()
171   {
172   }
173   
174   Foobar method()
175   {
176     return Foobar();
177   }
178   
179 };
180 %}
181
182 %feature("novaluewrapper") Foobar;
183 class Foobar;
184
185
186 class Quux {
187 public:
188   Quux();
189   
190   Foobar method();
191
192   
193 };
194
195
196 #if defined(SWIGPYTHON) 
197
198 /*
199   This case can't be fixed by using the valuewrapper feature and the
200   old mechanismbut it works fine with the new mechanism 
201 */
202
203 %{
204  
205   // Template primitive type, only visible in C++
206   template <class T>
207   struct Param
208   {
209     T val;
210
211     // This case is disabled by now
212     // Param(T v): val(v) {}
213
214     Param(T v = T()): val(v) {}
215     
216     operator T() const { return val; }
217   };
218
219 %}
220
221 /*
222   Several languages have 'not 100% safe' typemaps, 
223   where the following %applies  don't work. 
224 */
225 %apply int { Param<int> };
226 %apply const int& { const Param<int>& };
227
228 %apply double { Param<double> };
229 %apply const double& { const Param<double>& };
230
231 %inline %{
232
233   template <class T>
234   T getv(const Param<T>& p) 
235   {
236     return p.val;
237   }
238
239   template <class T>
240   Param<T> getp(const T& v)
241   {
242     return  Param<T>(v);
243   }
244   
245 %}
246   
247 %template(getv_i) getv<int>;
248 %template(getp_i) getp<int>;
249
250 %template(getv_d) getv<double>;
251 %template(getp_d) getp<double>;
252
253 #endif