forked from wxWidgets/Phoenix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrays.sip
260 lines (229 loc) · 8.28 KB
/
arrays.sip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//--------------------------------------------------------------------------
// Name: src/arrays.sip
// Purpose: Implements a %MappedType for wxArrayString and wxArrayInt
//
// Author: Robin Dunn
//
// Created: 2-Sept-2011
// Copyright: (c) 2011-2017 by Total Control Software
// Licence: wxWindows license
//--------------------------------------------------------------------------
// Some SIP MappedTypes that automatically convert Python lists of
// strings/ints to and from wxArrayString/wxArrayInt.
//--------------------------------------------------------------------------
%MappedType wxArrayString
{
%ConvertToTypeCode
// Code to test a PyObject for compatibility
if (!sipIsErr) {
int success = TRUE;
// ensure that it is a sequence
if (! PySequence_Check(sipPy))
success = FALSE;
// Ensure it is not a string or unicode object (they are sequences)
else if (PyBytes_Check(sipPy) || PyUnicode_Check(sipPy))
success = FALSE;
// ensure each item is a string/unicode object
else {
Py_ssize_t i, len = PySequence_Length(sipPy);
for (i=0; i<len; i++) {
PyObject* item = PySequence_GetItem(sipPy, i);
if (!PyBytes_Check(item) && !PyUnicode_Check(item)) {
Py_DECREF(item);
success = FALSE;
break;
}
Py_DECREF(item);
}
}
if (!success)
PyErr_SetString(PyExc_TypeError, "Sequence of string or unicode objects expected.");
return success;
}
// Code to create a new wxArrayString and convert a compatible PyObject
wxArrayString *array = new wxArrayString;
Py_ssize_t i, len = PySequence_Length(sipPy);
for (i=0; i<len; i++) {
PyObject* item = PySequence_GetItem(sipPy, i);
// if it's a string object convert it to unicode first, assuming utf-8
if (PyBytes_Check(item)) {
Py_DECREF(item);
item = PyUnicode_FromEncodedObject(item, "utf-8", "strict");
if (PyErr_Occurred()) {
*sipIsErr = 1;
delete array;
Py_DECREF(item);
return 0;
}
}
PyErr_Clear();
wxString string;
size_t len = PyUnicode_GET_SIZE(item);
if (len) {
wxPyUnicode_AsWideChar(item, wxStringBuffer(string, len), len);
}
if (PyErr_Occurred()) {
*sipIsErr = 1;
delete array;
Py_DECREF(item);
return 0;
}
array->Add(string);
Py_DECREF(item);
}
*sipCppPtr = array;
return sipGetState(sipTransferObj);
%End
%ConvertFromTypeCode
// Code to convert a wxArrayString to a Python list of strings.
PyObject* list = PyList_New(0);
for (size_t i=0; i < sipCpp->GetCount(); i++) {
PyObject* str = wx2PyString(sipCpp->Item(i));
PyList_Append(list, str);
Py_DECREF(str);
}
return list;
%End
};
// Used just for unittesting the MappedType code, it can be removed later
%ModuleCode
wxArrayString testArrayStringTypemap(const wxArrayString& arr)
{
wxArrayString local = arr; // force a copy
return local;
}
%End
wxArrayString testArrayStringTypemap(const wxArrayString& arr);
//--------------------------------------------------------------------------
%MappedType wxArrayInt
{
%ConvertToTypeCode
// Code to test a PyObject for compatibility
if (!sipIsErr) {
int success = TRUE;
// ensure that it is a sequence
if (! PySequence_Check(sipPy))
success = FALSE;
// ensure each item is a number object
else {
Py_ssize_t i, len = PySequence_Length(sipPy);
for (i=0; i<len; i++) {
PyObject* item = PySequence_GetItem(sipPy, i);
if (!PyNumber_Check(item)) {
Py_DECREF(item);
success = FALSE;
break;
}
Py_DECREF(item);
}
}
if (!success)
PyErr_SetString(PyExc_TypeError, "Sequence of numbers expected.");
return success;
}
// Code to create a new wxArrayInt and convert a compatible PyObject
wxArrayInt *array = new wxArrayInt;
Py_ssize_t i, len = PySequence_Length(sipPy);
for (i=0; i<len; i++) {
PyObject* item = PySequence_GetItem(sipPy, i);
PyObject* number = wxPyNumber_Int(item);
if (PyErr_Occurred()) {
*sipIsErr = 1;
delete array;
Py_DECREF(item);
return 0;
}
array->Add(wxPyInt_AS_LONG(number));
Py_DECREF(item);
Py_DECREF(number);
}
*sipCppPtr = array;
return sipGetState(sipTransferObj);
%End
%ConvertFromTypeCode
// Code to convert a wxArrayInt to a Python list of integers.
PyObject* list = PyList_New(0);
for (size_t i=0; i < sipCpp->GetCount(); i++) {
PyObject* number = wxPyInt_FromLong(sipCpp->Item(i));
PyList_Append(list, number);
Py_DECREF(number);
}
return list;
%End
};
// Used just for unittesting the MappedType code, it can be removed later
%ModuleCode
wxArrayInt testArrayIntTypemap(const wxArrayInt& arr)
{
wxArrayInt local = arr; // force a copy
return local;
}
%End
wxArrayInt testArrayIntTypemap(const wxArrayInt& arr);
//--------------------------------------------------------------------------
%MappedType wxArrayDouble
{
%ConvertToTypeCode
// Code to test a PyObject for compatibility
if (!sipIsErr) {
int success = TRUE;
// ensure that it is a sequence
if (! PySequence_Check(sipPy))
success = FALSE;
// ensure each item is a number object
else {
Py_ssize_t i, len = PySequence_Length(sipPy);
for (i=0; i<len; i++) {
PyObject* item = PySequence_GetItem(sipPy, i);
if (!PyNumber_Check(item)) {
Py_DECREF(item);
success = FALSE;
break;
}
Py_DECREF(item);
}
}
if (!success)
PyErr_SetString(PyExc_TypeError, "Sequence of numbers expected.");
return success;
}
// Code to create a new wxArrayDouble and convert a compatible PyObject
wxArrayDouble *array = new wxArrayDouble;
Py_ssize_t i, len = PySequence_Length(sipPy);
for (i=0; i<len; i++) {
PyObject* item = PySequence_GetItem(sipPy, i);
PyObject* number = PyNumber_Float(item);
if (PyErr_Occurred()) {
*sipIsErr = 1;
delete array;
Py_DECREF(item);
return 0;
}
array->Add(PyFloat_AS_DOUBLE(number));
Py_DECREF(item);
Py_DECREF(number);
}
*sipCppPtr = array;
return sipGetState(sipTransferObj);
%End
%ConvertFromTypeCode
// Code to convert a wxArrayDouble to a Python list of floating point values.
PyObject* list = PyList_New(0);
for (size_t i=0; i < sipCpp->GetCount(); i++) {
PyObject* number = PyFloat_FromDouble(sipCpp->Item(i));
PyList_Append(list, number);
Py_DECREF(number);
}
return list;
%End
};
// Used just for unittesting the MappedType code, it can be removed later
%ModuleCode
wxArrayDouble testArrayDoubleTypemap(const wxArrayDouble& arr)
{
wxArrayDouble local = arr; // force a copy
return local;
}
%End
wxArrayDouble testArrayDoubleTypemap(const wxArrayDouble& arr);
//--------------------------------------------------------------------------