diff --git a/SWIG/Lib/perl5/std_common.i b/SWIG/Lib/perl5/std_common.i index 8b1378917..e0be18ff4 100644 --- a/SWIG/Lib/perl5/std_common.i +++ b/SWIG/Lib/perl5/std_common.i @@ -1 +1,22 @@ +// +// SWIG typemaps for STL - common utilities +// Luigi Ballabio +// May 16, 2003 +// +// Perl implementation + +%{ +#include + +double SwigSvToNumber(SV* sv) { + return SvIOK(sv) ? double(SvIVX(sv)) : SvNVX(sv); +} +std::string SwigSvToString(SV* sv) { + STRLEN len; + return SvPV(sv,len); +} +void SwigSvFromString(SV* sv, const std::string& s) { + sv_setpv(sv,s.c_str()); +} +%} diff --git a/SWIG/Lib/perl5/std_vector.i b/SWIG/Lib/perl5/std_vector.i index 613da594a..7ff281dd5 100644 --- a/SWIG/Lib/perl5/std_vector.i +++ b/SWIG/Lib/perl5/std_vector.i @@ -4,9 +4,12 @@ // May 7, 2002 // Chris Seatory // August 5, 2002 +// Igor Bely +// May 16, 2003 // // Perl implementation +%include std_common.i %include exception.i // containers @@ -42,7 +45,7 @@ // // The aim of all that follows would be to integrate std::vector with // Perl as much as possible, namely, to allow the user to pass and -// be returned Perl lists. +// be returned Perl arrays. // const declarations are used to guess the intent of the function being // exported; therefore, the following rationale is applied: // @@ -244,8 +247,137 @@ namespace std { // specializations for built-ins - %define specialize_std_vector(T) + %define specialize_std_vector(T,CHECK_T,TO_T,FROM_T) template<> class vector { + %typemap(in) vector (std::vector* v) { + if (SvROK($input)) { + AV *av = (AV *)SvRV($input); + if (SvTYPE(av) != SVt_PVAV) + SWIG_croak("Type error in argument $argnum of $symname. " + "Expected an array of " #T); + SV **tv; + I32 len = av_len(av) + 1; + for (int i=0; i& (std::vector temp, + std::vector* v), + const vector* (std::vector temp, + std::vector* v) { + if (SvROK($input)) { + AV *av = (AV *)SvRV($input); + if (SvTYPE(av) != SVt_PVAV) + SWIG_croak("Type error in argument $argnum of $symname. " + "Expected an array of " #T); + SV **tv; + I32 len = av_len(av) + 1; + T* obj; + for (int i=0; i { + int len = $1.size(); + SV **svs = new SV*[len]; + for (unsigned int i=0; i { + /* native sequence? */ + if (SvROK($input)) { + AV *av = (AV *)SvRV($input); + if (SvTYPE(av) == SVt_PVAV) { + SV **tv; + I32 len = av_len(av) + 1; + if (len == 0) { + /* an empty sequence can be of any type */ + $1 = 1; + } else { + /* check the first element only */ + tv = av_fetch(av, 0, 0); + if (CHECK_T(*tv)) + $1 = 1; + else + $1 = 0; + } + } + } else { + /* wrapped vector? */ + std::vector* v; + if (SWIG_ConvertPtr($input,(void **) &v, + $1_&descriptor,0) != -1) + $1 = 1; + else + $1 = 0; + } + } + %typecheck(SWIG_TYPECHECK_VECTOR) const vector&, + const vector* { + /* native sequence? */ + if (SvROK($input)) { + AV *av = (AV *)SvRV($input); + if (SvTYPE(av) == SVt_PVAV) { + SV **tv; + I32 len = av_len(av) + 1; + if (len == 0) { + /* an empty sequence can be of any type */ + $1 = 1; + } else { + /* check the first element only */ + tv = av_fetch(av, 0, 0); + if (CHECK_T(*tv)) + $1 = 1; + else + $1 = 0; + } + } + } else { + /* wrapped vector? */ + std::vector* v; + if (SWIG_ConvertPtr($input,(void **) &v, + $1_descriptor,0) != -1) + $1 = 1; + else + $1 = 0; + } + } // add specialized typemaps here public: vector(); @@ -283,15 +415,16 @@ namespace std { }; %enddef - specialize_std_vector(bool); - specialize_std_vector(int); - specialize_std_vector(short); - specialize_std_vector(long); - specialize_std_vector(unsigned int); - specialize_std_vector(unsigned short); - specialize_std_vector(unsigned long); - specialize_std_vector(float); - specialize_std_vector(double); + specialize_std_vector(bool,SvIOK,SvIVX,sv_setiv); + specialize_std_vector(int,SvIOK,SvIVX,sv_setiv); + specialize_std_vector(short,SvIOK,SvIVX,sv_setiv); + specialize_std_vector(long,SvIOK,SvIVX,sv_setiv); + specialize_std_vector(unsigned int,SvIOK,SvIVX,sv_setiv); + specialize_std_vector(unsigned short,SvIOK,SvIVX,sv_setiv); + specialize_std_vector(unsigned long,SvIOK,SvIVX,sv_setiv); + specialize_std_vector(float,SvNIOK,SwigSvToNumber,sv_setnv); + specialize_std_vector(double,SvNIOK,SwigSvToNumber,sv_setnv); + specialize_std_vector(std::string,SvPOK,SvPVX,SwigSvFromString); }