From a86e4777ac5638107694343c7332a249dc5347ca Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Mon, 27 Mar 2006 20:11:52 +0000 Subject: [PATCH] fix overloading between unsigned long long and strings, as reported by Tim Lee. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9036 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Examples/test-suite/li_std_string.i | 20 +++++++++++++ .../test-suite/perl5/li_std_string_runme.pl | 13 +++++++++ SWIG/Lib/perl5/perlprimtypes.swg | 18 ++++++++---- SWIG/Lib/perl5/perlrun.swg | 29 +++++++++++++++++++ 4 files changed, 75 insertions(+), 5 deletions(-) diff --git a/SWIG/Examples/test-suite/li_std_string.i b/SWIG/Examples/test-suite/li_std_string.i index 6530c3f40..9916e7255 100644 --- a/SWIG/Examples/test-suite/li_std_string.i +++ b/SWIG/Examples/test-suite/li_std_string.i @@ -107,3 +107,23 @@ struct Structure { const std::string Structure::ConstStaticMemberString = "const static member string"; %} + +%inline %{ +class Foo { +public: + unsigned long long test(unsigned long long l) + { + return l + 1; + } + std::string test(std::string l) + { + return l + "1"; + } + + unsigned long long testl(unsigned long long l) + { + return l + 1; + } + +}; +%} diff --git a/SWIG/Examples/test-suite/perl5/li_std_string_runme.pl b/SWIG/Examples/test-suite/perl5/li_std_string_runme.pl index 155277fda..ec309bf4d 100644 --- a/SWIG/Examples/test-suite/perl5/li_std_string_runme.pl +++ b/SWIG/Examples/test-suite/perl5/li_std_string_runme.pl @@ -105,3 +105,16 @@ if (li_std_string::test_reference_input("hello") ne "hello") { if (li_std_string::test_reference_inout("hello") ne "hellohello") { die ("reference_inout"); } + + +$gen1 = new li_std_string::Foo(); +if ($gen1->test(1) ne 2) { + die ("ulonglong"); +} +if ($gen1->test("1") ne "11") { + die ("ulonglong"); +} +if ($gen1->testl(9234567890121111113) ne 9234567890121111114) { + die ("ulonglong"); +} + diff --git a/SWIG/Lib/perl5/perlprimtypes.swg b/SWIG/Lib/perl5/perlprimtypes.swg index 5791a83f8..a4f0104ee 100644 --- a/SWIG/Lib/perl5/perlprimtypes.swg +++ b/SWIG/Lib/perl5/perlprimtypes.swg @@ -70,7 +70,7 @@ SWIG_AsVal_dec(long)(SV *obj, long* val) } else { if (*endptr == '\0') { if (val) *val = v; - return SWIG_AddCast(SWIG_OK); + return SWIG_Str2NumCast(SWIG_OK); } } } @@ -128,7 +128,7 @@ SWIG_AsVal_dec(unsigned long)(SV *obj, unsigned long *val) } else { if (*endptr == '\0') { if (val) *val = v; - return SWIG_AddCast(SWIG_OK); + return SWIG_Str2NumCast(SWIG_OK); } } } @@ -189,7 +189,7 @@ SWIG_AsVal_dec(long long)(SV *obj, long long *val) } else { if (*endptr == '\0') { if (val) *val = v; - return SWIG_AddCast(SWIG_OK); + return SWIG_Str2NumCast(SWIG_OK); } } } @@ -239,6 +239,14 @@ SWIG_AsVal_dec(unsigned long long)(SV *obj, unsigned long long *val) if (SvUOK(obj)) { if (val) *val = SvUV(obj); return SWIG_OK; + } else if (SvIOK(obj)) { + long v = SvIV(obj); + if (v >= 0) { + if (val) *val = v; + return SWIG_OK; + } else { + return SWIG_OverflowError; + } } else { int dispatch = 0; const char *nptr = SvPV(obj, PL_na); @@ -251,7 +259,7 @@ SWIG_AsVal_dec(unsigned long long)(SV *obj, unsigned long long *val) } else { if (*endptr == '\0') { if (val) *val = v; - return SWIG_AddCast(SWIG_OK); + return SWIG_Str2NumCast(SWIG_OK); } } } @@ -302,7 +310,7 @@ SWIG_AsVal_dec(double)(SV *obj, double *val) } else { if (*endptr == '\0') { if (val) *val = v; - return SWIG_AddCast(SWIG_OK); + return SWIG_Str2NumCast(SWIG_OK); } } } diff --git a/SWIG/Lib/perl5/perlrun.swg b/SWIG/Lib/perl5/perlrun.swg index 4d260ed71..08505ba16 100644 --- a/SWIG/Lib/perl5/perlrun.swg +++ b/SWIG/Lib/perl5/perlrun.swg @@ -158,6 +158,35 @@ static void SWIG_croak_null() #endif +/* + Define how strict is the cast between strings an integers/doubles + when overloading between these types occurs. + + The default is making it as strict as possible by using SWIG_AddCast + when needed. + + You can use -DSWIG_PERL_NO_STRICT_STR2NUM at compilation time to + disable the SWIG_AddCast, making the casting between string and + numbers less strict. + + In the end, we try to solve the overloading between strings and + numerical types in the more natural way, but if you can avoid it, + well, avoid it using %rename, for example. +*/ +#ifndef SWIG_PERL_NO_STRICT_STR2NUM +# ifndef SWIG_PERL_STRICT_STR2NUM +# define SWIG_PERL_STRICT_STR2NUM +# endif +#endif +#ifdef SWIG_PERL_STRICT_STR2NUM +/* string takes precedence */ +#define SWIG_Str2NumCast(x) SWIG_AddCast(x) +#else +/* number takes precedence */ +#define SWIG_Str2NumCast(x) x +#endif + + #include