Salome HOME
Merge remote-tracking branch 'origin/BR_DEMO' into BR_2017
[modules/hydro.git] / src / HYDROPy / CAS / NCollection_Sequence.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 <NCollection_Sequence.hxx>
21 %End
22
23 // NCollection_Sequence<TYPE> is implemented as a Python list.
24 template<TYPE>
25 %MappedType NCollection_Sequence<TYPE>
26 {
27 %TypeHeaderCode
28 #include <NCollection_Sequence.hxx>
29 %End
30
31 %ConvertFromTypeCode
32     // Create the list.
33     PyObject *l;
34
35     if ( ( l = PyList_New( sipCpp->Length() ) ) == NULL )
36       return NULL;
37
38     // Set the list elements.
39     for ( int i = 1, n = sipCpp->Length(); i <= n; ++i )
40     {
41       TYPE* t = new TYPE( sipCpp->Value( 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 - 1, 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     NCollection_Sequence<TYPE> *aSeq = new NCollection_Sequence<TYPE>;
82     len = PySequence_Size(sipPy);
83  
84     for (SIP_SSIZE_T i = 0; i < len; ++i)
85     {
86         PyObject *itm = PySequence_ITEM(sipPy, i);
87         int state;
88         TYPE *t = reinterpret_cast<TYPE *>(sipConvertToType(itm, sipType_TYPE, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr));
89
90         Py_DECREF(itm);
91  
92         if (*sipIsErr)
93         {
94             sipReleaseType(t, sipType_TYPE, state);
95
96             delete aSeq;
97             return 0;
98         }
99
100         aSeq->Append(*t);
101
102         sipReleaseType(t, sipType_TYPE, state);
103     }
104  
105     *sipCppPtr = aSeq;
106  
107     return sipGetState(sipTransferObj);
108 %End
109 };
110
111 // NCollection_Sequence<double> is implemented as a Python list of floats.
112 %MappedType NCollection_Sequence<double>
113 {
114 %TypeHeaderCode
115 #include <NCollection_Sequence.hxx>
116 %End
117
118 %ConvertFromTypeCode
119     // Create the list.
120     PyObject *l;
121
122     if ( ( l = PyList_New( sipCpp->Length() ) ) == NULL )
123       return NULL;
124
125     // Set the list elements.
126     for ( int i = 1, n = sipCpp->Length(); i <= n; ++i )
127     {
128       PyObject *pobj;
129       if ( ( pobj = PyFloat_FromDouble( sipCpp->Value( i ) ) ) == NULL )
130       {
131         Py_DECREF(l);
132
133         return NULL;
134       }
135
136       PyList_SET_ITEM( l, i - 1, pobj );
137     }
138
139     return l;
140 %End
141
142 %ConvertToTypeCode
143     // Check the type if that is all that is required.
144     if ( sipIsErr == NULL )
145         return ( PySequence_Check( sipPy)  && PySequence_Size( sipPy ) >= 0 );
146
147     NCollection_Sequence<double> *aSeq = new NCollection_Sequence<double>;
148
149     SIP_SSIZE_T len = PySequence_Size(sipPy);
150     for ( SIP_SSIZE_T i = 0; i < len; ++i )
151     {
152       PyObject *itm = PySequence_ITEM( sipPy, i );
153       if ( !itm )
154       {
155         delete aSeq;
156         *sipIsErr = 1;
157
158         return 0;
159       }
160
161       aSeq->Append( PyFloat_AsDouble( itm ) );
162
163       Py_DECREF( itm );
164     }
165  
166     *sipCppPtr = aSeq;
167  
168     return sipGetState( sipTransferObj );
169 %End
170 };
171
172 // NCollection_Sequence<int> is implemented as a Python list of integers.
173 %MappedType NCollection_Sequence<int>
174 {
175 %TypeHeaderCode
176 #include <NCollection_Sequence.hxx>
177 %End
178
179 %ConvertFromTypeCode
180     // Create the list.
181     PyObject *l;
182
183     if ( ( l = PyList_New( sipCpp->Length() ) ) == NULL )
184       return NULL;
185
186     // Set the list elements.
187     for ( int i = 1, n = sipCpp->Length(); i <= n; ++i )
188     {
189       PyObject *pobj;
190       if ( ( pobj = SIPLong_FromLong( sipCpp->Value( i ) ) ) == NULL )
191       {
192         Py_DECREF(l);
193
194         return NULL;
195       }
196
197       PyList_SET_ITEM( l, i - 1, pobj );
198     }
199
200     return l;
201 %End
202
203 %ConvertToTypeCode
204     // Check the type if that is all that is required.
205     if ( sipIsErr == NULL )
206         return ( PySequence_Check( sipPy)  && PySequence_Size( sipPy ) >= 0 );
207
208     NCollection_Sequence<int> *aSeq = new NCollection_Sequence<int>;
209
210     SIP_SSIZE_T len = PySequence_Size(sipPy);
211     for ( SIP_SSIZE_T i = 0; i < len; ++i )
212     {
213       PyObject *itm = PySequence_ITEM( sipPy, i );
214       if ( !itm )
215       {
216         delete aSeq;
217         *sipIsErr = 1;
218
219         return 0;
220       }
221
222       aSeq->Append( SIPLong_AsLong( itm ) );
223
224       Py_DECREF( itm );
225     }
226  
227     *sipCppPtr = aSeq;
228  
229     return sipGetState( sipTransferObj );
230 %End
231 };
232
233 // NCollection_Sequence<bool> is implemented as a Python list of integers.
234 %MappedType NCollection_Sequence<bool>
235 {
236 %TypeHeaderCode
237 #include <NCollection_Sequence.hxx>
238 %End
239
240 %ConvertFromTypeCode
241     // Create the list.
242     PyObject *l;
243
244     if ( ( l = PyList_New( sipCpp->Length() ) ) == NULL )
245       return NULL;
246
247     // Set the list elements.
248     for ( int i = 1, n = sipCpp->Length(); i <= n; ++i )
249     {
250       PyObject *pobj;
251       if ( ( pobj = SIPLong_FromLong( sipCpp->Value( i ) ) ) == NULL )
252       {
253         Py_DECREF(l);
254
255         return NULL;
256       }
257
258       PyList_SET_ITEM( l, i - 1, pobj );
259     }
260
261     return l;
262 %End
263
264 %ConvertToTypeCode
265     // Check the type if that is all that is required.
266     if ( sipIsErr == NULL )
267         return ( PySequence_Check( sipPy)  && PySequence_Size( sipPy ) >= 0 );
268
269     NCollection_Sequence<bool> *aSeq = new NCollection_Sequence<bool>;
270
271     SIP_SSIZE_T len = PySequence_Size(sipPy);
272     for ( SIP_SSIZE_T i = 0; i < len; ++i )
273     {
274       PyObject *itm = PySequence_ITEM( sipPy, i );
275       if ( !itm )
276       {
277         delete aSeq;
278         *sipIsErr = 1;
279
280         return 0;
281       }
282
283       aSeq->Append( SIPLong_AsLong( itm ) != 0 );
284
285       Py_DECREF( itm );
286     }
287  
288     *sipCppPtr = aSeq;
289  
290     return sipGetState( sipTransferObj );
291 %End
292 };