1 %fragment("StdTraits","header",fragment="StdTraitsCommon") 2 { 3 namespace swig { 4 /* 5 Traits that provides the from method 6 */ 7 8 template <class Type> struct traits_from_ptr { 9 static SWIG_Object from(Type *val, int owner = 0) { 10 return SWIG_NewPointerObj(val, type_info<Type>(), owner); 11 } 12 }; 13 14 template <class Type> struct traits_from { 15 static SWIG_Object from(const Type& val) { 16 return traits_from_ptr<Type>::from(new Type(val), 1); 17 } 18 }; 19 20 template <class Type> struct traits_from<Type *> { 21 static SWIG_Object from(Type* val) { 22 return traits_from_ptr<Type>::from(val, 0); 23 } 24 }; 25 26 template <class Type> 27 inline SWIG_Object from(const Type& val) { 28 return traits_from<Type>::from(val); 29 } 30 31 template <class Type> 32 inline SWIG_Object from_ptr(Type* val, int owner) { 33 return traits_from_ptr<Type>::from(val, owner); 34 } 35 36 /* 37 Traits that provides the asval/as/check method 38 */ 39 template <class Type> 40 struct traits_asptr { 41 static int asptr(SWIG_Object obj, Type **val) { 42 Type *p; 43 int res = SWIG_ConvertPtr(obj, (void**)&p, type_info<Type>(), 0); 44 if (SWIG_IsOK(res)) { 45 if (val) *val = p; 46 } 47 return res; 48 } 49 }; 50 51 template <class Type> 52 inline int asptr(SWIG_Object obj, Type **vptr) { 53 return traits_asptr<Type>::asptr(obj, vptr); 54 } 55 56 template <class Type> 57 struct traits_asval { 58 static int asval(SWIG_Object obj, Type *val) { 59 if (val) { 60 Type *p = 0; 61 int res = traits_asptr<Type>::asptr(obj, &p); 62 if (!SWIG_IsOK(res)) return res; 63 if (p) { 64 typedef typename noconst_traits<Type>::noconst_type noconst_type; 65 *(const_cast<noconst_type*>(val)) = *p; 66 if (SWIG_IsNewObj(res)){ 67 %delete(p); 68 res = SWIG_DelNewMask(res); 69 } 70 return res; 71 } else { 72 return SWIG_ERROR; 73 } 74 } else { 75 return traits_asptr<Type>::asptr(obj, (Type **)(0)); 76 } 77 } 78 }; 79 80 template <class Type> struct traits_asval<Type*> { 81 static int asval(SWIG_Object obj, Type **val) { 82 if (val) { 83 typedef typename noconst_traits<Type>::noconst_type noconst_type; 84 noconst_type *p = 0; 85 int res = traits_asptr<noconst_type>::asptr(obj, &p); 86 if (SWIG_IsOK(res)) { 87 *(const_cast<noconst_type**>(val)) = p; 88 } 89 return res; 90 } else { 91 return traits_asptr<Type>::asptr(obj, (Type **)(0)); 92 } 93 } 94 }; 95 96 template <class Type> 97 inline int asval(SWIG_Object obj, Type *val) { 98 return traits_asval<Type>::asval(obj, val); 99 } 100 101 template <class Type> 102 struct traits_as<Type, value_category> { 103 static Type as(SWIG_Object obj, bool throw_error) { 104 Type v; 105 int res = asval(obj, &v); 106 if (!obj || !SWIG_IsOK(res)) { 107 if (throw_error) 108 throw std::invalid_argument("bad type"); 109 } 110 return v; 111 } 112 }; 113 114 template <class Type> 115 struct traits_as<Type, pointer_category> { 116 static Type as(SWIG_Object obj, bool throw_error) { 117 Type *v = 0; 118 int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR); 119 if (SWIG_IsOK(res) && v) { 120 if (SWIG_IsNewObj(res)) { 121 Type r(*v); 122 %delete(v); 123 return r; 124 } else { 125 return *v; 126 } 127 } else { 128 // Uninitialized return value, no Type() constructor required. 129 static Type *v_def = (Type*) malloc(sizeof(Type)); 130 if (throw_error) 131 throw std::invalid_argument("bad type"); 132 memset(v_def,0,sizeof(Type)); 133 return *v_def; 134 } 135 } 136 }; 137 138 template <class Type> 139 struct traits_as<Type*, pointer_category> { 140 static Type* as(SWIG_Object obj, bool throw_error) { 141 Type *v = 0; 142 int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR); 143 if (SWIG_IsOK(res)) { 144 return v; 145 } else { 146 if (throw_error) 147 throw std::invalid_argument("bad type"); 148 return 0; 149 } 150 } 151 }; 152 153 template <class Type> 154 inline Type as(SWIG_Object obj, bool te = false) { 155 return traits_as<Type, typename traits<Type>::category>::as(obj, te); 156 } 157 158 template <class Type> 159 struct traits_check<Type, value_category> { 160 static bool check(SWIG_Object obj) { 161 int res = obj ? asval(obj, (Type *)(0)) : SWIG_ERROR; 162 return SWIG_IsOK(res) ? true : false; 163 } 164 }; 165 166 template <class Type> 167 struct traits_check<Type, pointer_category> { 168 static bool check(SWIG_Object obj) { 169 int res = obj ? asptr(obj, (Type **)(0)) : SWIG_ERROR; 170 return SWIG_IsOK(res) ? true : false; 171 } 172 }; 173 174 template <class Type> 175 inline bool check(SWIG_Object obj) { 176 return traits_check<Type, typename traits<Type>::category>::check(obj); 177 } 178 } 179 } 180 181 %define %specialize_std_container(Type,Check,As,From) 182 %{ 183 namespace swig { 184 template <> struct traits_asval<Type > { 185 typedef Type value_type; 186 static int asval(SWIG_Object obj, value_type *val) { 187 if (Check(obj)) { 188 if (val) *val = As(obj); 189 return SWIG_OK; 190 } 191 return SWIG_ERROR; 192 } 193 }; 194 template <> struct traits_from<Type > { 195 typedef Type value_type; 196 static SWIG_Object from(const value_type& val) { 197 return From(val); 198 } 199 }; 200 201 template <> 202 struct traits_check<Type, value_category> { 203 static int check(SWIG_Object obj) { 204 int res = Check(obj); 205 return obj && res ? res : 0; 206 } 207 }; 208 } 209 %} 210 %enddef 211