PageRenderTime 28ms CodeModel.GetById 20ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Lib/perl5/perlprimtypes.swg

#
Unknown | 352 lines | 332 code | 20 blank | 0 comment | 0 complexity | 3ac148e456ea14aeee7882f463110a3f MD5 | raw file
  1/* ------------------------------------------------------------
  2 * Primitive Types
  3 * ------------------------------------------------------------ */
  4
  5/* bool */
  6
  7%fragment(SWIG_From_frag(bool),"header") {
  8SWIGINTERNINLINE SV *
  9SWIG_From_dec(bool)(bool value)
 10{
 11  return boolSV(value);
 12}
 13}
 14
 15%fragment(SWIG_AsVal_frag(bool),"header") {
 16SWIGINTERN int
 17SWIG_AsVal_dec(bool)(SV *obj, bool* val)
 18{
 19  if (obj == &PL_sv_yes) {
 20    if (val) *val = true;
 21    return SWIG_OK;
 22  } else if (obj == &PL_sv_no) { 
 23    if (val) *val = false;
 24    return SWIG_OK;
 25  } else {
 26    if (val) *val = SvTRUE(obj);
 27    return SWIG_AddCast(SWIG_OK);    
 28  }
 29}
 30}
 31
 32
 33/* long */
 34
 35%fragment(SWIG_From_frag(long),"header") {
 36SWIGINTERNINLINE SV *
 37SWIG_From_dec(long)(long value)
 38{
 39  SV *sv;
 40  if (value >= IV_MIN && value <= IV_MAX)
 41    sv = newSViv(value);
 42  else
 43    sv = newSVpvf("%ld", value);
 44  return sv_2mortal(sv);
 45}
 46}
 47
 48%fragment(SWIG_AsVal_frag(long),"header",
 49	  fragment="SWIG_CanCastAsInteger") {
 50SWIGINTERN int
 51SWIG_AsVal_dec(long)(SV *obj, long* val)
 52{
 53  if (SvUOK(obj)) {
 54    UV v = SvUV(obj);
 55    if (v <= LONG_MAX) {
 56      if (val) *val = v;
 57      return SWIG_OK;
 58    }
 59    return SWIG_OverflowError;
 60  } else if (SvIOK(obj)) {
 61    IV v = SvIV(obj);
 62    if (v >= LONG_MIN && v <= LONG_MAX) {
 63      if(val) *val = v;
 64      return SWIG_OK;
 65    }
 66    return SWIG_OverflowError;
 67  } else {
 68    int dispatch = 0;
 69    const char *nptr = SvPV_nolen(obj);
 70    if (nptr) {
 71      char *endptr;
 72      long v;
 73      errno = 0;
 74      v = strtol(nptr, &endptr,0);
 75      if (errno == ERANGE) {
 76	errno = 0;
 77	return SWIG_OverflowError;
 78      } else {
 79	if (*endptr == '\0') {
 80	  if (val) *val = v;
 81	  return SWIG_Str2NumCast(SWIG_OK);
 82	}
 83      }
 84    }
 85    if (!dispatch) {
 86      double d;
 87      int res = SWIG_AddCast(SWIG_AsVal(double)(obj,&d));
 88      if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, LONG_MIN, LONG_MAX)) {
 89	if (val) *val = (long)(d);
 90	return res;
 91      }
 92    }
 93  }
 94  return SWIG_TypeError;
 95}
 96}
 97
 98/* unsigned long */
 99
100%fragment(SWIG_From_frag(unsigned long),"header") {
101SWIGINTERNINLINE SV *
102SWIG_From_dec(unsigned long)(unsigned long value)
103{
104  SV *sv;
105  if (value <= UV_MAX)
106    sv = newSVuv(value);
107  else
108    sv = newSVpvf("%lu", value);
109  return sv_2mortal(sv);
110}
111}
112
113%fragment(SWIG_AsVal_frag(unsigned long),"header",
114	  fragment="SWIG_CanCastAsInteger") {
115SWIGINTERN int
116SWIG_AsVal_dec(unsigned long)(SV *obj, unsigned long *val) 
117{
118  if (SvUOK(obj)) {
119    UV v = SvUV(obj);
120    if (v <= ULONG_MAX) {
121      if (val) *val = v;
122      return SWIG_OK;
123    }
124    return SWIG_OverflowError;
125  } else if (SvIOK(obj)) {
126    IV v = SvIV(obj);
127    if (v >= 0 && v <= ULONG_MAX) {
128      if (val) *val = v;
129      return SWIG_OK;
130    }
131    return SWIG_OverflowError;
132  } else {
133    int dispatch = 0;
134    const char *nptr = SvPV_nolen(obj);
135    if (nptr) {
136      char *endptr;
137      unsigned long v;
138      errno = 0;
139      v = strtoul(nptr, &endptr,0);
140      if (errno == ERANGE) {
141	errno = 0;
142	return SWIG_OverflowError;
143      } else {
144	if (*endptr == '\0') {
145	  if (val) *val = v;
146	  return SWIG_Str2NumCast(SWIG_OK);
147	}
148      }
149    }
150    if (!dispatch) {
151      double d;
152      int res = SWIG_AddCast(SWIG_AsVal(double)(obj,&d));
153      if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, 0, ULONG_MAX)) {
154	if (val) *val = (unsigned long)(d);
155	return res;
156      }
157    }
158  }
159  return SWIG_TypeError;
160}
161}
162
163/* long long */
164
165%fragment(SWIG_From_frag(long long),"header",
166	  fragment=SWIG_From_frag(long),
167	  fragment="<limits.h>",
168	  fragment="<stdio.h>") {
169SWIGINTERNINLINE SV *
170SWIG_From_dec(long long)(long long value)
171{
172  SV *sv;
173  if (value >= IV_MIN && value <= IV_MAX)
174    sv = newSViv(value);
175  else {
176    //sv = newSVpvf("%lld", value); doesn't work in non 64bit Perl
177    char temp[256];
178    sprintf(temp, "%lld", value);
179    sv = newSVpv(temp, 0);
180  }
181  return sv_2mortal(sv);
182}
183}
184
185%fragment(SWIG_AsVal_frag(long long),"header",
186	  fragment="<limits.h>",
187	  fragment="<stdlib.h>",
188	  fragment="SWIG_CanCastAsInteger") {
189
190SWIGINTERN int
191SWIG_AsVal_dec(long long)(SV *obj, long long *val)
192{
193  if (SvUOK(obj)) {
194    UV v = SvUV(obj);
195    if (v < LLONG_MAX) {
196      if (val) *val = v;
197      return SWIG_OK;
198    }
199    return SWIG_OverflowError;
200  } else if (SvIOK(obj)) {
201    IV v = SvIV(obj);
202    if (v >= LLONG_MIN && v <= LLONG_MAX) {
203      if (val) *val = v;
204      return SWIG_OK;
205    }
206    return SWIG_OverflowError;
207  } else {
208    int dispatch = 0;
209    const char *nptr = SvPV_nolen(obj);
210    if (nptr) {
211      char *endptr;
212      long long v;
213      errno = 0;
214      v = strtoll(nptr, &endptr,0);
215      if (errno == ERANGE) {
216	errno = 0;
217	return SWIG_OverflowError;
218      } else {
219	if (*endptr == '\0') {
220	  if (val) *val = v;
221	  return SWIG_Str2NumCast(SWIG_OK);
222	}
223      }
224    }
225    if (!dispatch) {
226      const double mant_max = 1LL << DBL_MANT_DIG;
227      const double mant_min = -mant_max;
228      double d;
229      int res = SWIG_AddCast(SWIG_AsVal(double)(obj,&d));
230      if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, mant_min, mant_max)) {
231	if (val) *val = (long long)(d);
232	return res;
233      }
234    }
235  }
236  return SWIG_TypeError; 
237}
238}
239
240/* unsigned long long */
241
242%fragment(SWIG_From_frag(unsigned long long),"header",
243	  fragment=SWIG_From_frag(long long),
244	  fragment="<limits.h>",
245	  fragment="<stdio.h>") {
246SWIGINTERNINLINE SV *
247SWIG_From_dec(unsigned long long)(unsigned long long value)
248{
249  SV *sv;
250  if (value <= UV_MAX)
251    sv = newSVuv(value);
252  else {
253    //sv = newSVpvf("%llu", value); doesn't work in non 64bit Perl
254    char temp[256];
255    sprintf(temp, "%llu", value);
256    sv = newSVpv(temp, 0);
257  }
258  return sv_2mortal(sv);
259}
260}
261
262%fragment(SWIG_AsVal_frag(unsigned long long),"header",
263	  fragment="<limits.h>",
264	  fragment="<stdlib.h>",
265	  fragment="SWIG_CanCastAsInteger") {
266SWIGINTERN int
267SWIG_AsVal_dec(unsigned long long)(SV *obj, unsigned long long *val)
268{
269  if (SvUOK(obj)) {
270    if (val) *val = SvUV(obj);
271    return SWIG_OK;
272  } else  if (SvIOK(obj)) {
273    IV v = SvIV(obj);
274    if (v >= 0 && v <= ULLONG_MAX) {
275      if (val) *val = v;
276      return SWIG_OK;
277    } else {
278      return SWIG_OverflowError;
279    }
280  } else {
281    int dispatch = 0;
282    const char *nptr = SvPV_nolen(obj);
283    if (nptr) {
284      char *endptr;
285      unsigned long long v;
286      errno = 0;
287      v = strtoull(nptr, &endptr,0);
288      if (errno == ERANGE) {
289	errno = 0;
290	return SWIG_OverflowError;
291      } else {
292	if (*endptr == '\0') {
293	  if (val) *val = v;
294	  return SWIG_Str2NumCast(SWIG_OK);
295	}
296      }
297    }
298    if (!dispatch) {
299      const double mant_max = 1LL << DBL_MANT_DIG;
300      double d;
301      int res = SWIG_AddCast(SWIG_AsVal(double)(obj,&d));
302      if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, 0, mant_max)) {
303	if (val) *val = (unsigned long long)(d);
304	return res;
305      }
306    }
307  }
308  return SWIG_TypeError;
309}
310}
311
312/* double */
313
314%fragment(SWIG_From_frag(double),"header") {
315SWIGINTERNINLINE SV *
316SWIG_From_dec(double)(double value)
317{
318  return sv_2mortal(newSVnv(value));
319}
320}
321
322%fragment(SWIG_AsVal_frag(double),"header") {
323SWIGINTERN int
324SWIG_AsVal_dec(double)(SV *obj, double *val)
325{
326  if (SvNIOK(obj)) {
327    if (val) *val = SvNV(obj);
328    return SWIG_OK;
329  } else if (SvIOK(obj)) {
330    if (val) *val = (double) SvIV(obj);
331    return SWIG_AddCast(SWIG_OK);
332  } else {
333    const char *nptr = SvPV_nolen(obj);
334    if (nptr) {
335      char *endptr;
336      double v;
337      errno = 0;
338      v = strtod(nptr, &endptr);
339      if (errno == ERANGE) {
340	errno = 0;
341	return SWIG_OverflowError;
342      } else {
343	if (*endptr == '\0') {
344	  if (val) *val = v;
345	  return SWIG_Str2NumCast(SWIG_OK);
346	}
347      }
348    }
349  }
350  return SWIG_TypeError;
351}
352}