1 %fragment("StdTraits","header",fragment="StdTraitsCommon")
5 Traits that provides the from method
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);
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);
19 template <class Type> struct traits_from<Type *> {
20 static PyObject *from(Type* val) {
21 return traits_from_ptr<Type>::from(val, 0);
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);
33 inline PyObject *from(const Type& val) {
34 return traits_from<Type>::from(val);
38 inline PyObject *from_ptr(Type* val, int owner) {
39 return traits_from_ptr<Type>::from(val, owner);
43 Traits that provides the asval/as/check method
47 static int asptr(PyObject *obj, Type **val) {
49 int res = SWIG_ConvertPtr(obj, (void**)&p, type_info<Type>(), 0);
58 inline int asptr(PyObject *obj, Type **vptr) {
59 return traits_asptr<Type>::asptr(obj, vptr);
64 static int asval(PyObject *obj, Type *val) {
67 int res = traits_asptr<Type>::asptr(obj, &p);
68 if (!SWIG_IsOK(res)) return res;
70 typedef typename noconst_traits<Type>::noconst_type noconst_type;
71 *(const_cast<noconst_type*>(val)) = *p;
72 if (SWIG_IsNewObj(res)){
74 res = SWIG_DelNewMask(res);
81 return traits_asptr<Type>::asptr(obj, (Type **)(0));
86 template <class Type> struct traits_asval<Type*> {
87 static int asval(PyObject *obj, Type **val) {
89 typedef typename noconst_traits<Type>::noconst_type noconst_type;
91 int res = traits_asptr<noconst_type>::asptr(obj, &p);
93 *(const_cast<noconst_type**>(val)) = p;
97 return traits_asptr<Type>::asptr(obj, (Type **)(0));
102 template <class Type>
103 inline int asval(PyObject *obj, Type *val) {
104 return traits_asval<Type>::asval(obj, val);
107 template <class Type>
108 struct traits_as<Type, value_category> {
109 static Type as(PyObject *obj, bool throw_error) {
111 int res = asval(obj, &v);
112 if (!obj || !SWIG_IsOK(res)) {
113 if (!PyErr_Occurred()) {
114 ::%type_error(swig::type_name<Type>());
116 if (throw_error) throw std::invalid_argument("bad type");
122 template <class Type>
123 struct traits_as<Type, pointer_category> {
124 static Type as(PyObject *obj, bool throw_error) {
126 int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR);
127 if (SWIG_IsOK(res) && v) {
128 if (SWIG_IsNewObj(res)) {
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>());
141 if (throw_error) throw std::invalid_argument("bad type");
142 memset(v_def,0,sizeof(Type));
148 template <class Type>
149 struct traits_as<Type*, pointer_category> {
150 static Type* as(PyObject *obj, bool throw_error) {
152 int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR);
153 if (SWIG_IsOK(res)) {
156 if (!PyErr_Occurred()) {
157 %type_error(swig::type_name<Type>());
159 if (throw_error) throw std::invalid_argument("bad type");
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);
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;
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;
186 template <class Type>
187 inline bool check(PyObject *obj) {
188 return traits_check<Type, typename traits<Type>::category>::check(obj);
194 // Backward compatibility
197 #ifdef SWIG_PYTHON_BACKWARD_COMP
201 PyObject* SwigInt_FromBool(bool b) {
202 return PyInt_FromLong(b ? 1L : 0L);
204 double SwigNumber_Check(PyObject* o) {
205 return PyFloat_Check(o) || PyInt_Check(o) || PyLong_Check(o);
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)));
212 PyObject* SwigString_FromString(const std::string& s) {
213 return PyString_FromStringAndSize(s.data(),s.size());
215 std::string SwigString_AsString(PyObject* o) {
216 return std::string(PyString_AsString(o));
223 %define %specialize_std_container(Type,Check,As,From)
226 template <> struct traits_asval<Type > {
227 typedef Type value_type;
228 static int asval(PyObject *obj, value_type *val) {
230 if (val) *val = As(obj);
236 template <> struct traits_from<Type > {
237 typedef Type value_type;
238 static PyObject *from(const value_type& val) {
244 struct traits_check<Type, value_category> {
245 static int check(PyObject *obj) {
246 int res = Check(obj);
247 return obj && res ? res : 0;
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(Type),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(Type),Check,As,From)
259 #define specialize_std_multiset(Type,Check,As,From) %specialize_std_container(%arg(Type),Check,As,From)