logo       

RE: SWIG: std::string returned as reference - solution: msg#00134

programming.swig

Subject: RE: SWIG: std::string returned as reference - solution

I've created the following typemaps to support argument passing by
reference.

Unfortunately SWIG does not like function templates such as:

template< class T>
%typemap(in) T& ($basetype temp) {

--code
}

So I have to implement this separately.

/**
* IN STRING TYPE MAP
*
*/
%typemap(in) std::string& ($basetype string_temp) {
if (!SvPOK($input)) {
croak("Argument $argnum is not a string");
}
$1 = &string_temp;
}

/**
* IN STRING ARG OUT TYPE MAP
*
*/
%typemap(argout) std::string& OUTPUT {

sv_setpv( $input, (*$1).c_str() );
}

/**
* IN INT TYPE MAP
*
*/
%typemap(in) unsigned short& ($basetype int_temp),
short& ($basetype int_temp),
unsigned int& ($basetype int_temp),
int& ($basetype int_temp),
unsigned char& ($basetype int_temp) {

if (!SvIOK($input)) {
croak("Argument $argnum is not an integer");
}
$1 = &int_temp;
}

/**
* IN Int ARG OUT TYPE MAP
*
*/
%typemap(argout) unsigned short& OUTPUT,
short& OUTPUT,
unsigned int& OUTPUT,
int& OUTPUT,
unsigned char& OUTPUT {

sv_setiv( $input, (*$1) );
}

/**
* APPPLY THIS TYPEMAP TO ALL REF PARAMS.
*/
%apply std::string& OUTPUT { std::string& };
%apply int& OUTPUT { int& };
%apply int& OUTPUT { unsigned short& };
%apply int& OUTPUT { short& };
%apply unsigned short& OUTPUT { unsigned short& };
%apply unsigned char& OUTPUT { unsigned char& };



Thanks,
-sanjay








-----Original Message-----
From: David Beazley [mailto:beazley@xxxxxxxxxxxxxxx]
Sent: Wednesday, November 19, 2003 1:48 PM
To: Nair, Sanjay S
Cc: David Beazley; swig@xxxxxxxxxxxxxxx
Subject: RE: [Swig] SWIG: std::string returned as reference - solution

Nair, Sanjay S writes:
> > This is different than most OUTPUT typemaps which do not modify
their
> > arguments. Use std::string &REFERENCE if you're going to do that
(to
> > be consistent with other typemaps that behave in the same way).
>
> What happens if you have multiple out params?
>
>

Well, that's an entirely different problem.
> >
> > %typemap(in) std::string*, std::string& ($basetype string_temp) {
> > if (!SvPOK($input)) {
> > croak("Argument $argnum is not a string");
> > }
> > $1 = &string_temp;
> > argvi++;
> > }
> >

I just noticed this, but why are you incrementing argvi++ in an "in"
typemap? That's related to output, not input.

-- Dave
_______________________________________________
Swig maillist - Swig@xxxxxxxxxxxxxxx
http://mailman.cs.uchicago.edu/mailman/listinfo/swig



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise