add std::vector<T*> specialization to fix #1386582

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@8928 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Marcelo Matus 2006-03-02 17:58:47 +00:00
commit 7475a6b25e
2 changed files with 171 additions and 1 deletions

View file

@ -108,7 +108,7 @@ SWIG_STD_VECTOR_SPECIALIZE(MyClass, MyClass *)
public:
MyClassVector GetAllResources(int n) const
{
return MyClassVector(n);
return MyClassVector(n, 0);
}
};
}

View file

@ -221,6 +221,176 @@ namespace std {
}
};
// specializations for pointers
template<class T> class vector<T*> {
%typemap(in) vector<T*> (std::vector<T*>* v) {
int res = SWIG_ConvertPtr($input,(void **) &v, $&1_descriptor,0);
if (SWIG_IsOK(res)){
$1 = *v;
} else 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);
I32 len = av_len(av) + 1;
for (int i=0; i<len; i++) {
void *v;
SV **tv = av_fetch(av, i, 0);
int res = SWIG_ConvertPtr(*tv, &v, $descriptor(T *),0);
if (SWIG_IsOK(res)) {
$1.push_back(%static_cast(v, T *));
} else {
SWIG_croak("Type error in argument $argnum of "
"$symname. "
"Expected an array of " #T);
}
}
} else {
SWIG_croak("Type error in argument $argnum of $symname. "
"Expected an array of " #T);
}
}
%typemap(in) const vector<T *>& (std::vector<T *> temp,std::vector<T *>* v),
const vector<T *>* (std::vector<T *> temp,std::vector<T *>* v) {
int res = SWIG_ConvertPtr($input,(void **) &v, $1_descriptor,0);
if (SWIG_IsOK(res)) {
$1 = v;
} else 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);
I32 len = av_len(av) + 1;
for (int i=0; i<len; i++) {
void *v;
SV **tv = av_fetch(av, i, 0);
int res = SWIG_ConvertPtr(*tv, &v, $descriptor(T *),0);
if (SWIG_IsOK(res)) {
temp.push_back(%static_cast(v, T *));
} else {
SWIG_croak("Type error in argument $argnum of "
"$symname. "
"Expected an array of " #T);
}
}
$1 = &temp;
} else {
SWIG_croak("Type error in argument $argnum of $symname. "
"Expected an array of " #T);
}
}
%typemap(out) vector<T *> {
size_t len = $1.size();
SV **svs = new SV*[len];
for (size_t i=0; i<len; i++) {
T *x = (($1_type &)$1)[i];
svs[i] = sv_newmortal();
sv_setsv(svs[i], SWIG_NewPointerObj(x, $descriptor(T *), 0));
}
AV *myav = av_make(len, svs);
delete[] svs;
$result = newRV_noinc((SV*) myav);
sv_2mortal($result);
argvi++;
}
%typecheck(SWIG_TYPECHECK_VECTOR) vector<T *> {
{
/* wrapped vector? */
std::vector<T *>* v;
int res = SWIG_ConvertPtr($input,(void **) &v, $&1_descriptor,0);
if (SWIG_IsOK(res)) {
$1 = 1;
} else if (SvROK($input)) {
/* native sequence? */
AV *av = (AV *)SvRV($input);
if (SvTYPE(av) == SVt_PVAV) {
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 */
void *v;
SV **tv = av_fetch(av, 0, 0);
int res = SWIG_ConvertPtr(*tv, &v, $descriptor(T *),0);
if (SWIG_IsOK(res))
$1 = 1;
else
$1 = 0;
}
}
} else {
$1 = 0;
}
}
}
%typecheck(SWIG_TYPECHECK_VECTOR) const vector<T *>&,const vector<T *>* {
{
/* wrapped vector? */
std::vector<T *> *v;
int res = SWIG_ConvertPtr($input,%as_voidptrptr(&v), $1_descriptor,0);
if (SWIG_IsOK(res)) {
$1 = 1;
} else if (SvROK($input)) {
/* native sequence? */
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 */
void *v;
SV **tv = av_fetch(av, 0, 0);
int res = SWIG_ConvertPtr(*tv, &v, $descriptor(T *),0);
if (SWIG_IsOK(res))
$1 = 1;
else
$1 = 0;
}
}
} else {
$1 = 0;
}
}
}
public:
vector(unsigned int size = 0);
vector(unsigned int size, T *value);
vector(const vector<T *> &);
unsigned int size() const;
bool empty() const;
void clear();
%rename(push) push_back;
void push_back(T *x);
%extend {
T *pop() throw (std::out_of_range) {
if (self->size() == 0)
throw std::out_of_range("pop from empty vector");
T *x = self->back();
self->pop_back();
return x;
}
T *get(int i) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
return (*self)[i];
else
throw std::out_of_range("vector index out of range");
}
void set(int i, T *x) throw (std::out_of_range) {
int size = int(self->size());
if (i>=0 && i<size)
(*self)[i] = x;
else
throw std::out_of_range("vector index out of range");
}
}
};
// specializations for built-ins