OLD | NEW |
(Empty) | |
| 1 %fragment("StdTraits","header",fragment="StdTraitsCommon") |
| 2 { |
| 3 namespace swig { |
| 4 /* |
| 5 Traits that provides the from method |
| 6 */ |
| 7 template <class Type> struct traits_from_ptr { |
| 8 static PyObject *from(Type *val, int owner = 0) { |
| 9 return SWIG_NewPointerObj(val, type_info<Type>(), owner); |
| 10 } |
| 11 }; |
| 12 |
| 13 template <class Type> struct traits_from { |
| 14 static PyObject *from(const Type& val) { |
| 15 return traits_from_ptr<Type>::from(new Type(val), 1); |
| 16 } |
| 17 }; |
| 18 |
| 19 template <class Type> struct traits_from<Type *> { |
| 20 static PyObject *from(Type* val) { |
| 21 return traits_from_ptr<Type>::from(val, 0); |
| 22 } |
| 23 }; |
| 24 |
| 25 template <class Type> struct traits_from<const Type *> { |
| 26 static PyObject *from(const Type* val) { |
| 27 return traits_from_ptr<Type>::from(const_cast<Type*>(val), 0); |
| 28 } |
| 29 }; |
| 30 |
| 31 |
| 32 template <class Type> |
| 33 inline PyObject *from(const Type& val) { |
| 34 return traits_from<Type>::from(val); |
| 35 } |
| 36 |
| 37 template <class Type> |
| 38 inline PyObject *from_ptr(Type* val, int owner) { |
| 39 return traits_from_ptr<Type>::from(val, owner); |
| 40 } |
| 41 |
| 42 /* |
| 43 Traits that provides the asval/as/check method |
| 44 */ |
| 45 template <class Type> |
| 46 struct traits_asptr { |
| 47 static int asptr(PyObject *obj, Type **val) { |
| 48 Type *p; |
| 49 int res = SWIG_ConvertPtr(obj, (void**)&p, type_info<Type>(), 0); |
| 50 if (SWIG_IsOK(res)) { |
| 51 if (val) *val = p; |
| 52 } |
| 53 return res; |
| 54 } |
| 55 }; |
| 56 |
| 57 template <class Type> |
| 58 inline int asptr(PyObject *obj, Type **vptr) { |
| 59 return traits_asptr<Type>::asptr(obj, vptr); |
| 60 } |
| 61 |
| 62 template <class Type> |
| 63 struct traits_asval { |
| 64 static int asval(PyObject *obj, Type *val) { |
| 65 if (val) { |
| 66 Type *p = 0; |
| 67 int res = traits_asptr<Type>::asptr(obj, &p); |
| 68 if (!SWIG_IsOK(res)) return res; |
| 69 if (p) { |
| 70 typedef typename noconst_traits<Type>::noconst_type noconst_type; |
| 71 *(const_cast<noconst_type*>(val)) = *p; |
| 72 if (SWIG_IsNewObj(res)){ |
| 73 %delete(p); |
| 74 res = SWIG_DelNewMask(res); |
| 75 } |
| 76 return res; |
| 77 } else { |
| 78 return SWIG_ERROR; |
| 79 } |
| 80 } else { |
| 81 return traits_asptr<Type>::asptr(obj, (Type **)(0)); |
| 82 } |
| 83 } |
| 84 }; |
| 85 |
| 86 template <class Type> struct traits_asval<Type*> { |
| 87 static int asval(PyObject *obj, Type **val) { |
| 88 if (val) { |
| 89 typedef typename noconst_traits<Type>::noconst_type noconst_type; |
| 90 noconst_type *p = 0; |
| 91 int res = traits_asptr<noconst_type>::asptr(obj, &p); |
| 92 if (SWIG_IsOK(res)) { |
| 93 *(const_cast<noconst_type**>(val)) = p; |
| 94 } |
| 95 return res; |
| 96 } else { |
| 97 return traits_asptr<Type>::asptr(obj, (Type **)(0)); |
| 98 } |
| 99 } |
| 100 }; |
| 101 |
| 102 template <class Type> |
| 103 inline int asval(PyObject *obj, Type *val) { |
| 104 return traits_asval<Type>::asval(obj, val); |
| 105 } |
| 106 |
| 107 template <class Type> |
| 108 struct traits_as<Type, value_category> { |
| 109 static Type as(PyObject *obj, bool throw_error) { |
| 110 Type v; |
| 111 int res = asval(obj, &v); |
| 112 if (!obj || !SWIG_IsOK(res)) { |
| 113 if (!PyErr_Occurred()) { |
| 114 ::%type_error(swig::type_name<Type>()); |
| 115 } |
| 116 if (throw_error) throw std::invalid_argument("bad type"); |
| 117 } |
| 118 return v; |
| 119 } |
| 120 }; |
| 121 |
| 122 template <class Type> |
| 123 struct traits_as<Type, pointer_category> { |
| 124 static Type as(PyObject *obj, bool throw_error) { |
| 125 Type *v = 0; |
| 126 int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR); |
| 127 if (SWIG_IsOK(res) && v) { |
| 128 if (SWIG_IsNewObj(res)) { |
| 129 Type r(*v); |
| 130 %delete(v); |
| 131 return r; |
| 132 } else { |
| 133 return *v; |
| 134 } |
| 135 } else { |
| 136 // Uninitialized return value, no Type() constructor required. |
| 137 static Type *v_def = (Type*) malloc(sizeof(Type)); |
| 138 if (!PyErr_Occurred()) { |
| 139 %type_error(swig::type_name<Type>()); |
| 140 } |
| 141 if (throw_error) throw std::invalid_argument("bad type"); |
| 142 memset(v_def,0,sizeof(Type)); |
| 143 return *v_def; |
| 144 } |
| 145 } |
| 146 }; |
| 147 |
| 148 template <class Type> |
| 149 struct traits_as<Type*, pointer_category> { |
| 150 static Type* as(PyObject *obj, bool throw_error) { |
| 151 Type *v = 0; |
| 152 int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR); |
| 153 if (SWIG_IsOK(res)) { |
| 154 return v; |
| 155 } else { |
| 156 if (!PyErr_Occurred()) { |
| 157 %type_error(swig::type_name<Type>()); |
| 158 } |
| 159 if (throw_error) throw std::invalid_argument("bad type"); |
| 160 return 0; |
| 161 } |
| 162 } |
| 163 }; |
| 164 |
| 165 template <class Type> |
| 166 inline Type as(PyObject *obj, bool te = false) { |
| 167 return traits_as<Type, typename traits<Type>::category>::as(obj, te); |
| 168 } |
| 169 |
| 170 template <class Type> |
| 171 struct traits_check<Type, value_category> { |
| 172 static bool check(PyObject *obj) { |
| 173 int res = obj ? asval(obj, (Type *)(0)) : SWIG_ERROR; |
| 174 return SWIG_IsOK(res) ? true : false; |
| 175 } |
| 176 }; |
| 177 |
| 178 template <class Type> |
| 179 struct traits_check<Type, pointer_category> { |
| 180 static bool check(PyObject *obj) { |
| 181 int res = obj ? asptr(obj, (Type **)(0)) : SWIG_ERROR; |
| 182 return SWIG_IsOK(res) ? true : false; |
| 183 } |
| 184 }; |
| 185 |
| 186 template <class Type> |
| 187 inline bool check(PyObject *obj) { |
| 188 return traits_check<Type, typename traits<Type>::category>::check(obj); |
| 189 } |
| 190 } |
| 191 } |
| 192 |
| 193 // |
| 194 // Backward compatibility |
| 195 // |
| 196 |
| 197 #ifdef SWIG_PYTHON_BACKWARD_COMP |
| 198 %{ |
| 199 #include <string> |
| 200 |
| 201 PyObject* SwigInt_FromBool(bool b) { |
| 202 return PyInt_FromLong(b ? 1L : 0L); |
| 203 } |
| 204 double SwigNumber_Check(PyObject* o) { |
| 205 return PyFloat_Check(o) || PyInt_Check(o) || PyLong_Check(o); |
| 206 } |
| 207 double SwigNumber_AsDouble(PyObject* o) { |
| 208 return PyFloat_Check(o) ? PyFloat_AsDouble(o) |
| 209 : (PyInt_Check(o) ? double(PyInt_AsLong(o)) |
| 210 : double(PyLong_AsLong(o))); |
| 211 } |
| 212 PyObject* SwigString_FromString(const std::string& s) { |
| 213 return PyString_FromStringAndSize(s.data(),s.size()); |
| 214 } |
| 215 std::string SwigString_AsString(PyObject* o) { |
| 216 return std::string(PyString_AsString(o)); |
| 217 } |
| 218 %} |
| 219 |
| 220 #endif |
| 221 |
| 222 |
| 223 %define %specialize_std_container(Type,Check,As,From) |
| 224 %{ |
| 225 namespace swig { |
| 226 template <> struct traits_asval<Type > { |
| 227 typedef Type value_type; |
| 228 static int asval(PyObject *obj, value_type *val) { |
| 229 if (Check(obj)) { |
| 230 if (val) *val = As(obj); |
| 231 return SWIG_OK; |
| 232 } |
| 233 return SWIG_ERROR; |
| 234 } |
| 235 }; |
| 236 template <> struct traits_from<Type > { |
| 237 typedef Type value_type; |
| 238 static PyObject *from(const value_type& val) { |
| 239 return From(val); |
| 240 } |
| 241 }; |
| 242 |
| 243 template <> |
| 244 struct traits_check<Type, value_category> { |
| 245 static int check(PyObject *obj) { |
| 246 int res = Check(obj); |
| 247 return obj && res ? res : 0; |
| 248 } |
| 249 }; |
| 250 } |
| 251 %} |
| 252 %enddef |
| 253 |
| 254 |
| 255 #define specialize_std_vector(Type,Check,As,From) %specialize_std_container(%arg
(Type),Check,As,From) |
| 256 #define specialize_std_list(Type,Check,As,From) %specialize_std_container(%arg(T
ype),Check,As,From) |
| 257 #define specialize_std_deque(Type,Check,As,From) %specialize_std_container(%arg(
Type),Check,As,From) |
| 258 #define specialize_std_set(Type,Check,As,From) %specialize_std_container(%arg(Ty
pe),Check,As,From) |
| 259 #define specialize_std_multiset(Type,Check,As,From) %specialize_std_container(%a
rg(Type),Check,As,From) |
OLD | NEW |