logo       

Re: Vector extensions: msg#00096

programming.swig

Subject: Re: Vector extensions

At 01:20 PM 1/22/03 +0100, Kostyantyn Teplynskyy wrote:
Hellow, please advice me. I am working with vectors. My task is to add my own functions to vectors. I have one decision:

namespace std {
%extend vector {
char *myfunc{
...
};
}
template (pParams) vector <Mystruct>; // myfunc adds to pParams
template (sParams) vector <MyFloat>; // but not adds to sParams
};
And it's works but only for vector of structs or vector of classes, by not for vectors of basic types like double

Kostyantyn,
vector of built-ins are template specializations of the generic vector. My guess is that your %extend is only attached to the generic class. You'll have to attach it to the specializations as well---I think the less annoying way is to write:

namespace std {
%extend vector {
char *myfunc{
...
};
}
%define specialize_myfunc(T)
%extend vector<T> {
char *myfunc{
...
};
}
%enddef
specialize_myfunc(bool);
specialize_myfunc(int);
specialize_myfunc(short);
specialize_myfunc(long);
specialize_myfunc(unsigned int);
specialize_myfunc(unsigned short);
specialize_myfunc(unsigned long);
specialize_myfunc(double);
specialize_myfunc(float);
specialize_myfunc(std::string);
}

which reduces to a minimum the amouont of duplication, even though it does not avoid it completely. If the body of myfunc is large, you might consider defining it as a function in a %{ }% block (so that it is added to the C++ wrappers) and call it from within the %extend-added methods.

Hope this helps,
Luigi

_______________________________________________
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