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