Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / share / swig / 1.3.26 / perl5 / std_vector.i
CommitLineData
920dae64
AT
1//
2// SWIG typemaps for std::vector types
3// Luigi Ballabio
4// May 7, 2002
5// Chris Seatory
6// August 5, 2002
7// Igor Bely
8// May 16, 2003
9//
10// Perl implementation
11
12%include <std_common.i>
13
14// ------------------------------------------------------------------------
15// std::vector
16//
17// The aim of all that follows would be to integrate std::vector with
18// Perl as much as possible, namely, to allow the user to pass and
19// be returned Perl arrays.
20// const declarations are used to guess the intent of the function being
21// exported; therefore, the following rationale is applied:
22//
23// -- f(std::vector<T>), f(const std::vector<T>&), f(const std::vector<T>*):
24// the parameter being read-only, either a Perl sequence or a
25// previously wrapped std::vector<T> can be passed.
26// -- f(std::vector<T>&), f(std::vector<T>*):
27// the parameter must be modified; therefore, only a wrapped std::vector
28// can be passed.
29// -- std::vector<T> f():
30// the vector is returned by copy; therefore, a Perl sequence of T:s
31// is returned which is most easily used in other Perl functions
32// -- std::vector<T>& f(), std::vector<T>* f(), const std::vector<T>& f(),
33// const std::vector<T>* f():
34// the vector is returned by reference; therefore, a wrapped std::vector
35// is returned
36// ------------------------------------------------------------------------
37
38%{
39#include <vector>
40#include <algorithm>
41#include <stdexcept>
42%}
43
44// exported class
45
46namespace std {
47
48 template<class T> class vector {
49 %typemap(in) vector<T> (std::vector<T>* v) {
50 if (SWIG_ConvertPtr($input,(void **) &v,
51 $&1_descriptor,1) != -1) {
52 $1 = *v;
53 } else if (SvROK($input)) {
54 AV *av = (AV *)SvRV($input);
55 if (SvTYPE(av) != SVt_PVAV)
56 SWIG_croak("Type error in argument $argnum of $symname. "
57 "Expected an array of " #T);
58 SV **tv;
59 I32 len = av_len(av) + 1;
60 T* obj;
61 for (int i=0; i<len; i++) {
62 tv = av_fetch(av, i, 0);
63 if (SWIG_ConvertPtr(*tv, (void **)&obj,
64 $descriptor(T *),0) != -1) {
65 $1.push_back(*obj);
66 } else {
67 SWIG_croak("Type error in argument $argnum of "
68 "$symname. "
69 "Expected an array of " #T);
70 }
71 }
72 } else {
73 SWIG_croak("Type error in argument $argnum of $symname. "
74 "Expected an array of " #T);
75 }
76 }
77 %typemap(in) const vector<T>& (std::vector<T> temp,
78 std::vector<T>* v),
79 const vector<T>* (std::vector<T> temp,
80 std::vector<T>* v) {
81 if (SWIG_ConvertPtr($input,(void **) &v,
82 $1_descriptor,1) != -1) {
83 $1 = v;
84 } else if (SvROK($input)) {
85 AV *av = (AV *)SvRV($input);
86 if (SvTYPE(av) != SVt_PVAV)
87 SWIG_croak("Type error in argument $argnum of $symname. "
88 "Expected an array of " #T);
89 SV **tv;
90 I32 len = av_len(av) + 1;
91 T* obj;
92 for (int i=0; i<len; i++) {
93 tv = av_fetch(av, i, 0);
94 if (SWIG_ConvertPtr(*tv, (void **)&obj,
95 $descriptor(T *),0) != -1) {
96 temp.push_back(*obj);
97 } else {
98 SWIG_croak("Type error in argument $argnum of "
99 "$symname. "
100 "Expected an array of " #T);
101 }
102 }
103 $1 = &temp;
104 } else {
105 SWIG_croak("Type error in argument $argnum of $symname. "
106 "Expected an array of " #T);
107 }
108 }
109 %typemap(out) vector<T> {
110 int len = $1.size();
111 SV **svs = new SV*[len];
112 for (unsigned int i=0; i<len; i++) {
113 T* ptr = new T($1[i]);
114 svs[i] = sv_newmortal();
115 SWIG_MakePtr(svs[i], (void*) ptr,
116 $descriptor(T *), $shadow|$owner);
117 }
118 AV *myav = av_make(len, svs);
119 delete[] svs;
120 $result = newRV_noinc((SV*) myav);
121 sv_2mortal($result);
122 argvi++;
123 }
124 %typecheck(SWIG_TYPECHECK_VECTOR) vector<T> {
125 {
126 /* wrapped vector? */
127 std::vector<T >* v;
128 if (SWIG_ConvertPtr($input,(void **) &v,
129 $1_&descriptor,0) != -1) {
130 $1 = 1;
131 } else if (SvROK($input)) {
132 /* native sequence? */
133 AV *av = (AV *)SvRV($input);
134 if (SvTYPE(av) == SVt_PVAV) {
135 SV **tv;
136 I32 len = av_len(av) + 1;
137 if (len == 0) {
138 /* an empty sequence can be of any type */
139 $1 = 1;
140 } else {
141 /* check the first element only */
142 T* obj;
143 tv = av_fetch(av, 0, 0);
144 if (SWIG_ConvertPtr(*tv, (void **)&obj,
145 $descriptor(T *),0) != -1)
146 $1 = 1;
147 else
148 $1 = 0;
149 }
150 }
151 } else {
152 $1 = 0;
153 }
154 }
155 }
156 %typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&,
157 const vector<T>* {
158 {
159 /* wrapped vector? */
160 std::vector<T >* v;
161 if (SWIG_ConvertPtr($input,(void **) &v,
162 $1_descriptor,0) != -1) {
163 $1 = 1;
164 } else if (SvROK($input)) {
165 /* native sequence? */
166 AV *av = (AV *)SvRV($input);
167 if (SvTYPE(av) == SVt_PVAV) {
168 SV **tv;
169 I32 len = av_len(av) + 1;
170 if (len == 0) {
171 /* an empty sequence can be of any type */
172 $1 = 1;
173 } else {
174 /* check the first element only */
175 T* obj;
176 tv = av_fetch(av, 0, 0);
177 if (SWIG_ConvertPtr(*tv, (void **)&obj,
178 $descriptor(T *),0) != -1)
179 $1 = 1;
180 else
181 $1 = 0;
182 }
183 }
184 } else {
185 $1 = 0;
186 }
187 }
188 }
189 public:
190 vector(unsigned int size = 0);
191 vector(unsigned int size, const T& value);
192 vector(const vector<T> &);
193
194 unsigned int size() const;
195 bool empty() const;
196 void clear();
197 %rename(push) push_back;
198 void push_back(const T& x);
199 %extend {
200 T pop() throw (std::out_of_range) {
201 if (self->size() == 0)
202 throw std::out_of_range("pop from empty vector");
203 T x = self->back();
204 self->pop_back();
205 return x;
206 }
207 T& get(int i) throw (std::out_of_range) {
208 int size = int(self->size());
209 if (i>=0 && i<size)
210 return (*self)[i];
211 else
212 throw std::out_of_range("vector index out of range");
213 }
214 void set(int i, const T& x) throw (std::out_of_range) {
215 int size = int(self->size());
216 if (i>=0 && i<size)
217 (*self)[i] = x;
218 else
219 throw std::out_of_range("vector index out of range");
220 }
221 }
222 };
223
224
225 // specializations for built-ins
226
227 %define specialize_std_vector(T,CHECK_T,TO_T,FROM_T)
228 template<> class vector<T> {
229 %typemap(in) vector<T> (std::vector<T>* v) {
230 if (SWIG_ConvertPtr($input,(void **) &v,
231 $&1_descriptor,1) != -1){
232 $1 = *v;
233 } else if (SvROK($input)) {
234 AV *av = (AV *)SvRV($input);
235 if (SvTYPE(av) != SVt_PVAV)
236 SWIG_croak("Type error in argument $argnum of $symname. "
237 "Expected an array of " #T);
238 SV **tv;
239 I32 len = av_len(av) + 1;
240 for (int i=0; i<len; i++) {
241 tv = av_fetch(av, i, 0);
242 if (CHECK_T(*tv)) {
243 $1.push_back(TO_T(*tv));
244 } else {
245 SWIG_croak("Type error in argument $argnum of "
246 "$symname. "
247 "Expected an array of " #T);
248 }
249 }
250 } else {
251 SWIG_croak("Type error in argument $argnum of $symname. "
252 "Expected an array of " #T);
253 }
254 }
255 %typemap(in) const vector<T>& (std::vector<T> temp,
256 std::vector<T>* v),
257 const vector<T>* (std::vector<T> temp,
258 std::vector<T>* v) {
259 if (SWIG_ConvertPtr($input,(void **) &v,
260 $1_descriptor,1) != -1) {
261 $1 = v;
262 } else if (SvROK($input)) {
263 AV *av = (AV *)SvRV($input);
264 if (SvTYPE(av) != SVt_PVAV)
265 SWIG_croak("Type error in argument $argnum of $symname. "
266 "Expected an array of " #T);
267 SV **tv;
268 I32 len = av_len(av) + 1;
269 for (int i=0; i<len; i++) {
270 tv = av_fetch(av, i, 0);
271 if (CHECK_T(*tv)) {
272 temp.push_back(TO_T(*tv));
273 } else {
274 SWIG_croak("Type error in argument $argnum of "
275 "$symname. "
276 "Expected an array of " #T);
277 }
278 }
279 $1 = &temp;
280 } else {
281 SWIG_croak("Type error in argument $argnum of $symname. "
282 "Expected an array of " #T);
283 }
284 }
285 %typemap(out) vector<T> {
286 size_t len = $1.size();
287 SV **svs = new SV*[len];
288 for (size_t i=0; i<len; i++) {
289 svs[i] = sv_newmortal();
290 FROM_T(svs[i], $1[i]);
291 }
292 AV *myav = av_make(len, svs);
293 delete[] svs;
294 $result = newRV_noinc((SV*) myav);
295 sv_2mortal($result);
296 argvi++;
297 }
298 %typecheck(SWIG_TYPECHECK_VECTOR) vector<T> {
299 {
300 /* wrapped vector? */
301 std::vector<T >* v;
302 if (SWIG_ConvertPtr($input,(void **) &v,
303 $1_&descriptor,0) != -1) {
304 $1 = 1;
305 } else if (SvROK($input)) {
306 /* native sequence? */
307 AV *av = (AV *)SvRV($input);
308 if (SvTYPE(av) == SVt_PVAV) {
309 SV **tv;
310 I32 len = av_len(av) + 1;
311 if (len == 0) {
312 /* an empty sequence can be of any type */
313 $1 = 1;
314 } else {
315 /* check the first element only */
316 tv = av_fetch(av, 0, 0);
317 if (CHECK_T(*tv))
318 $1 = 1;
319 else
320 $1 = 0;
321 }
322 }
323 } else {
324 $1 = 0;
325 }
326 }
327 }
328 %typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&,
329 const vector<T>* {
330 {
331 /* wrapped vector? */
332 std::vector<T >* v;
333 if (SWIG_ConvertPtr($input,(void **) &v,
334 $1_descriptor,0) != -1) {
335 $1 = 1;
336 } else if (SvROK($input)) {
337 /* native sequence? */
338 AV *av = (AV *)SvRV($input);
339 if (SvTYPE(av) == SVt_PVAV) {
340 SV **tv;
341 I32 len = av_len(av) + 1;
342 if (len == 0) {
343 /* an empty sequence can be of any type */
344 $1 = 1;
345 } else {
346 /* check the first element only */
347 tv = av_fetch(av, 0, 0);
348 if (CHECK_T(*tv))
349 $1 = 1;
350 else
351 $1 = 0;
352 }
353 }
354 } else {
355 $1 = 0;
356 }
357 }
358 }
359 public:
360 vector(unsigned int size = 0);
361 vector(unsigned int size, T value);
362 vector(const vector<T> &);
363
364 unsigned int size() const;
365 bool empty() const;
366 void clear();
367 %rename(push) push_back;
368 void push_back(T x);
369 %extend {
370 T pop() throw (std::out_of_range) {
371 if (self->size() == 0)
372 throw std::out_of_range("pop from empty vector");
373 T x = self->back();
374 self->pop_back();
375 return x;
376 }
377 T get(int i) throw (std::out_of_range) {
378 int size = int(self->size());
379 if (i>=0 && i<size)
380 return (*self)[i];
381 else
382 throw std::out_of_range("vector index out of range");
383 }
384 void set(int i, T x) throw (std::out_of_range) {
385 int size = int(self->size());
386 if (i>=0 && i<size)
387 (*self)[i] = x;
388 else
389 throw std::out_of_range("vector index out of range");
390 }
391 }
392 };
393 %enddef
394
395 specialize_std_vector(bool,SvIOK,SvIVX,sv_setiv);
396 specialize_std_vector(char,SvIOK,SvIVX,sv_setiv);
397 specialize_std_vector(int,SvIOK,SvIVX,sv_setiv);
398 specialize_std_vector(short,SvIOK,SvIVX,sv_setiv);
399 specialize_std_vector(long,SvIOK,SvIVX,sv_setiv);
400 specialize_std_vector(unsigned char,SvIOK,SvIVX,sv_setiv);
401 specialize_std_vector(unsigned int,SvIOK,SvIVX,sv_setiv);
402 specialize_std_vector(unsigned short,SvIOK,SvIVX,sv_setiv);
403 specialize_std_vector(unsigned long,SvIOK,SvIVX,sv_setiv);
404 specialize_std_vector(float,SvNIOK,SwigSvToNumber,sv_setnv);
405 specialize_std_vector(double,SvNIOK,SwigSvToNumber,sv_setnv);
406 specialize_std_vector(std::string,SvPOK,SvPVX,SwigSvFromString);
407
408}
409