Salome HOME
Merge branch 'BR_HYDRO_IMPS_WIN' of ssh://gitolite3@git.salome-platform.org/modules...
[modules/hydro.git] / src / HYDROPy / CAS / vector.sip
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 %ExportedHeaderCode
20 #include <vector>
21 %End
22
23 // std::vector<TYPE> is implemented as a Python list.
24 template<TYPE>
25 %MappedType std::vector<TYPE>
26 {
27 %TypeHeaderCode
28 #include <vector>
29 %End
30
31 %ConvertFromTypeCode
32     // Create the list.
33     PyObject *l;
34
35     if ( ( l = PyList_New( sipCpp->size() ) ) == NULL )
36       return NULL;
37
38     // Set the list elements.
39     for ( int i = 0, n = sipCpp->size(); i < n; ++i )
40     {
41       TYPE* t = new TYPE( (*sipCpp)[i] );
42
43       PyObject* pobj;
44       if ( ( pobj = sipConvertFromNewType( t, sipType_TYPE, sipTransferObj ) ) == NULL )
45       {
46         Py_DECREF( l );
47         delete t;
48
49         return NULL;
50       }
51
52       PyList_SET_ITEM( l, i, pobj );
53     }
54
55     return l;
56 %End
57
58 %ConvertToTypeCode
59     SIP_SSIZE_T len;
60
61     // Check the type if that is all that is required.
62     if (sipIsErr == NULL)
63     {
64         if (!PySequence_Check(sipPy) || (len = PySequence_Size(sipPy)) < 0)
65             return 0;
66
67         for (SIP_SSIZE_T i = 0; i < len; ++i)
68         {
69             PyObject *itm = PySequence_ITEM(sipPy, i);
70             bool ok = (itm && sipCanConvertToType(itm, sipType_TYPE, SIP_NOT_NONE));
71
72             Py_XDECREF(itm);
73
74             if (!ok)
75                 return 0;
76         }
77
78         return 1;
79     }
80
81     std::vector<TYPE> *aSeq = new std::vector<TYPE>;
82     len = PySequence_Size(sipPy);
83         aSeq->reserve( len );
84  
85     for (SIP_SSIZE_T i = 0; i < len; ++i)
86     {
87         PyObject *itm = PySequence_ITEM(sipPy, i);
88         int state;
89         TYPE *t = reinterpret_cast<TYPE *>(sipConvertToType(itm, sipType_TYPE, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr));
90
91         Py_DECREF(itm);
92  
93         if (*sipIsErr)
94         {
95             sipReleaseType(t, sipType_TYPE, state);
96
97             delete aSeq;
98             return 0;
99         }
100
101         aSeq->push_back(*t);
102
103         sipReleaseType(t, sipType_TYPE, state);
104     }
105  
106     *sipCppPtr = aSeq;
107  
108     return sipGetState(sipTransferObj);
109 %End
110 };
111
112 // std::vector<double> is implemented as a Python list of floats.
113 %MappedType std::vector<double>
114 {
115 %TypeHeaderCode
116 #include <vector>
117 %End
118
119 %ConvertFromTypeCode
120     // Create the list.
121     PyObject *l;
122
123     if ( ( l = PyList_New( sipCpp->size() ) ) == NULL )
124       return NULL;
125
126     // Set the list elements.
127     for ( int i = 0, n = sipCpp->size(); i < n; ++i )
128     {
129       PyObject *pobj;
130       if ( ( pobj = PyFloat_FromDouble( (*sipCpp)[i] ) ) == NULL )
131       {
132         Py_DECREF(l);
133
134         return NULL;
135       }
136
137       PyList_SET_ITEM( l, i, pobj );
138     }
139
140     return l;
141 %End
142
143 %ConvertToTypeCode
144     // Check the type if that is all that is required.
145     if ( sipIsErr == NULL )
146         return ( PySequence_Check( sipPy)  && PySequence_Size( sipPy ) >= 0 );
147
148     std::vector<double> *aSeq = new std::vector<double>;
149
150     SIP_SSIZE_T len = PySequence_Size(sipPy);
151         aSeq->reserve( len );
152     for ( SIP_SSIZE_T i = 0; i < len; ++i )
153     {
154       PyObject *itm = PySequence_ITEM( sipPy, i );
155       if ( !itm )
156       {
157         delete aSeq;
158         *sipIsErr = 1;
159
160         return 0;
161       }
162
163       aSeq->push_back( PyFloat_AsDouble( itm ) );
164
165       Py_DECREF( itm );
166     }
167  
168     *sipCppPtr = aSeq;
169  
170     return sipGetState( sipTransferObj );
171 %End
172 };
173
174 // std::vector<int> is implemented as a Python list of integers.
175 %MappedType std::vector<int>
176 {
177 %TypeHeaderCode
178 #include <vector>
179 %End
180
181 %ConvertFromTypeCode
182     // Create the list.
183     PyObject *l;
184
185     if ( ( l = PyList_New( sipCpp->size() ) ) == NULL )
186       return NULL;
187
188     // Set the list elements.
189     for ( int i = 0, n = sipCpp->size(); i < n; ++i )
190     {
191       PyObject *pobj;
192       if ( ( pobj = SIPLong_FromLong( (*sipCpp)[i] ) ) == NULL )
193       {
194         Py_DECREF(l);
195
196         return NULL;
197       }
198
199       PyList_SET_ITEM( l, i, pobj );
200     }
201
202     return l;
203 %End
204
205 %ConvertToTypeCode
206     // Check the type if that is all that is required.
207     if ( sipIsErr == NULL )
208         return ( PySequence_Check( sipPy)  && PySequence_Size( sipPy ) >= 0 );
209
210     std::vector<int> *aSeq = new std::vector<int>;
211
212     SIP_SSIZE_T len = PySequence_Size(sipPy);
213         aSeq->reserve( len );
214     for ( SIP_SSIZE_T i = 0; i < len; ++i )
215     {
216       PyObject *itm = PySequence_ITEM( sipPy, i );
217       if ( !itm )
218       {
219         delete aSeq;
220         *sipIsErr = 1;
221
222         return 0;
223       }
224
225       aSeq->push_back( SIPLong_AsLong( itm ) );
226
227       Py_DECREF( itm );
228     }
229  
230     *sipCppPtr = aSeq;
231  
232     return sipGetState( sipTransferObj );
233 %End
234 };
235
236 // std::vector<bool> is implemented as a Python list of integers.
237 %MappedType std::vector<bool>
238 {
239 %TypeHeaderCode
240 #include <vector>
241 %End
242
243 %ConvertFromTypeCode
244     // Create the list.
245     PyObject *l;
246
247     if ( ( l = PyList_New( sipCpp->size() ) ) == NULL )
248       return NULL;
249
250     // Set the list elements.
251     for ( int i = 0, n = sipCpp->size(); i < n; ++i )
252     {
253       PyObject *pobj;
254       if ( ( pobj = SIPLong_FromLong( (*sipCpp)[i] ) ) == NULL )
255       {
256         Py_DECREF(l);
257
258         return NULL;
259       }
260
261       PyList_SET_ITEM( l, i, pobj );
262     }
263
264     return l;
265 %End
266
267 %ConvertToTypeCode
268     // Check the type if that is all that is required.
269     if ( sipIsErr == NULL )
270         return ( PySequence_Check( sipPy)  && PySequence_Size( sipPy ) >= 0 );
271
272     std::vector<bool> *aSeq = new std::vector<bool>;
273
274     SIP_SSIZE_T len = PySequence_Size(sipPy);
275         aSeq->reserve( len );
276     for ( SIP_SSIZE_T i = 0; i < len; ++i )
277     {
278       PyObject *itm = PySequence_ITEM( sipPy, i );
279       if ( !itm )
280       {
281         delete aSeq;
282         *sipIsErr = 1;
283
284         return 0;
285       }
286
287       aSeq->push_back( SIPLong_AsLong( itm ) != 0 );
288
289       Py_DECREF( itm );
290     }
291  
292     *sipCppPtr = aSeq;
293  
294     return sipGetState( sipTransferObj );
295 %End
296 };