Salome HOME
First realisation of Cascade classes.
[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 = 0; i < sipCpp->Length(); ++i)
44     {
45         TYPE *t = new TYPE(sipCpp->Value(i));
46         PyObject *tobj;
47
48         if ((tobj = 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, tobj);
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> *ql = 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 ql;
101             return 0;
102         }
103
104         ql->Append(*t);
105
106         sipReleaseType(t, sipType_TYPE, state);
107     }
108  
109     *sipCppPtr = ql;
110  
111     return sipGetState(sipTransferObj);
112 %End
113 };
114