%fragment("StdTraits","header",fragment="StdTraitsCommon") { namespace swig { /* Traits that provides the from method */ template struct traits_from_ptr { static SWIG_Object from(Type *val, int owner = 0) { return SWIG_NewPointerObj(val, type_info(), owner); } }; template struct traits_from { static SWIG_Object from(const Type& val) { return traits_from_ptr::from(new Type(val), 1); } }; template struct traits_from { static SWIG_Object from(Type* val) { return traits_from_ptr::from(val, 0); } }; template inline SWIG_Object from(const Type& val) { return traits_from::from(val); } template inline SWIG_Object from_ptr(Type* val, int owner) { return traits_from_ptr::from(val, owner); } /* Traits that provides the asval/as/check method */ template struct traits_asptr { static int asptr(SWIG_Object obj, Type **val) { Type *p; int res = SWIG_ConvertPtr(obj, (void**)&p, type_info(), 0); if (SWIG_IsOK(res)) { if (val) *val = p; } return res; } }; template inline int asptr(SWIG_Object obj, Type **vptr) { return traits_asptr::asptr(obj, vptr); } template struct traits_asval { static int asval(SWIG_Object obj, Type *val) { if (val) { Type *p = 0; int res = traits_asptr::asptr(obj, &p); if (!SWIG_IsOK(res)) return res; if (p) { typedef typename noconst_traits::noconst_type noconst_type; *(const_cast(val)) = *p; if (SWIG_IsNewObj(res)){ %delete(p); res = SWIG_DelNewMask(res); } return res; } else { return SWIG_ERROR; } } else { return traits_asptr::asptr(obj, (Type **)(0)); } } }; template struct traits_asval { static int asval(SWIG_Object obj, Type **val) { if (val) { typedef typename noconst_traits::noconst_type noconst_type; noconst_type *p = 0; int res = traits_asptr::asptr(obj, &p); if (SWIG_IsOK(res)) { *(const_cast(val)) = p; } return res; } else { return traits_asptr::asptr(obj, (Type **)(0)); } } }; template inline int asval(SWIG_Object obj, Type *val) { return traits_asval::asval(obj, val); } template struct traits_as { static Type as(SWIG_Object obj, bool throw_error) { Type v; int res = asval(obj, &v); if (!obj || !SWIG_IsOK(res)) { if (throw_error) throw std::invalid_argument("bad type"); } return v; } }; template struct traits_as { static Type as(SWIG_Object obj, bool throw_error) { Type *v = 0; int res = (obj ? traits_asptr::asptr(obj, &v) : SWIG_ERROR); if (SWIG_IsOK(res) && v) { if (SWIG_IsNewObj(res)) { Type r(*v); %delete(v); return r; } else { return *v; } } else { // Uninitialized return value, no Type() constructor required. static Type *v_def = (Type*) malloc(sizeof(Type)); if (throw_error) throw std::invalid_argument("bad type"); memset(v_def,0,sizeof(Type)); return *v_def; } } }; template struct traits_as { static Type* as(SWIG_Object obj, bool throw_error) { Type *v = 0; int res = (obj ? traits_asptr::asptr(obj, &v) : SWIG_ERROR); if (SWIG_IsOK(res)) { return v; } else { if (throw_error) throw std::invalid_argument("bad type"); return 0; } } }; template inline Type as(SWIG_Object obj, bool te = false) { return traits_as::category>::as(obj, te); } template struct traits_check { static bool check(SWIG_Object obj) { int res = obj ? asval(obj, (Type *)(0)) : SWIG_ERROR; return SWIG_IsOK(res) ? true : false; } }; template struct traits_check { static bool check(SWIG_Object obj) { int res = obj ? asptr(obj, (Type **)(0)) : SWIG_ERROR; return SWIG_IsOK(res) ? true : false; } }; template inline bool check(SWIG_Object obj) { return traits_check::category>::check(obj); } } } %define %specialize_std_container(Type,Check,As,From) %{ namespace swig { template <> struct traits_asval { typedef Type value_type; static int asval(SWIG_Object obj, value_type *val) { if (Check(obj)) { if (val) *val = As(obj); return SWIG_OK; } return SWIG_ERROR; } }; template <> struct traits_from { typedef Type value_type; static SWIG_Object from(const value_type& val) { return From(val); } }; template <> struct traits_check { static int check(SWIG_Object obj) { int res = Check(obj); return obj && res ? res : 0; } }; } %} %enddef