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