as() no longer uses memset and always throws.

This commit is contained in:
Mike Romberg 2017-07-20 14:48:51 -06:00
commit c3ba9506bf

View file

@ -104,23 +104,21 @@ namespace swig {
template <class Type> template <class Type>
struct traits_as<Type, value_category> { struct traits_as<Type, value_category> {
static Type as(const SwigSciObject& obj, bool throw_error) { static Type as(const SwigSciObject& obj) {
Type v; Type v;
int res = asval(obj, &v); int res = asval(obj, &v);
if (SWIG_IsOK(res)) { if (SWIG_IsOK(res)) {
return v; return v;
} else { } else {
%type_error(swig::type_name<Type>()); %type_error(swig::type_name<Type>());
if (throw_error) throw std::invalid_argument("bad type");
throw std::invalid_argument("bad type");
return res;
} }
} }
}; };
template <class Type> template <class Type>
struct traits_as<Type, pointer_category> { struct traits_as<Type, pointer_category> {
static Type as(const SwigSciObject& obj, bool throw_error) { static Type as(const SwigSciObject& obj) {
Type *v = 0; Type *v = 0;
int res = traits_asptr<Type>::asptr(obj, &v); int res = traits_asptr<Type>::asptr(obj, &v);
if (SWIG_IsOK(res) && v) { if (SWIG_IsOK(res) && v) {
@ -132,36 +130,29 @@ namespace swig {
return *v; return *v;
} }
} else { } else {
// Uninitialized return value, no Type() constructor required. %type_error(swig::type_name<Type>());
static Type *v_def = (Type*) malloc(sizeof(Type)); throw std::invalid_argument("bad type");
%type_error(swig::type_name<Type>());
if (throw_error)
throw std::invalid_argument("bad type");
memset(v_def,0,sizeof(Type));
return *v_def;
} }
} }
}; };
template <class Type> template <class Type>
struct traits_as<Type*, pointer_category> { struct traits_as<Type*, pointer_category> {
static Type* as(const SwigSciObject& obj, bool throw_error) { static Type* as(const SwigSciObject& obj) {
Type *v = 0; Type *v = 0;
int res = traits_asptr<Type>::asptr(obj, &v); int res = traits_asptr<Type>::asptr(obj, &v);
if (SWIG_IsOK(res)) { if (SWIG_IsOK(res)) {
return v; return v;
} else { } else {
%type_error(swig::type_name<Type>()); %type_error(swig::type_name<Type>());
if (throw_error) throw std::invalid_argument("bad type");
throw std::invalid_argument("bad type");
return SWIG_OK;
} }
} }
}; };
template <class Type> template <class Type>
inline Type as(const SwigSciObject& obj, bool te = false) { inline Type as(const SwigSciObject& obj) {
return traits_as<Type, typename traits<Type>::category>::as(obj, te); return traits_as<Type, typename traits<Type>::category>::as(obj);
} }
template <class Type> template <class Type>