Array return argument with typemaps
"Alberto Luaces" <[email protected]>
| Newsgroups | gmane.comp.programming.swig |
|---|---|
| Message-ID | <[email protected]> |
Hello,
I am wrapping a function that receives a pointer to class, double
parameter and writes the computation to an array:
extern "C" void interp5C(spline *stat, double G, double *f);
The input arguments are working well. The "stat" object holds the size
of the system and thus the size that "f" should have.
If I do
%typemap(in) spline *stat (int nelems)
{
// stat->n +1 is the size of the problem
nelems = $1->n + 1;
}
%typemap(in, numinputs=0) double *f (double *temp) {
temp = new double [nelems];
$1 = temp;
}
%typemap(argout) double *f {
dim_vector dv(nelems, 1);
$result = Matrix(dv);
}
I face the problem that "nelems" is adjusted after it is used in "new
double [nelems]".
I finally created an utility function that outputs the double *:
%inline{
extern "C" void interp5C(spline *stat, double G, double *f);
double *interp(spline *stat, double x)
{
double *f = new double [stat->n + 1];
interp5C(stat, x, f);
return f;
}
}
with the typemaps
%typemap(out) double * {
int nelems = arg1->n + 1;
dim_vector dv(nelems, 1);
Matrix r(dv);
for (int i = 0; i < nelems; i++){
r(i) = $1[i];
}
delete [] $1;
$result = r;
}
and this works, but I am not entirely convinced with this, because I am
accessing "arg1" directly to refer to "stat", which I think it is not
very clean.
I guess the original problem is similar to wrapping strcpy(), but
slightly modified:
void strcpy( char *dest, const char *src );
where "dest" is to be resized at runtime given the nature of "src".
Is there any way to do it simpler?
Thanks!