1#include "Python.h"
2#include "pyobjc-api.h"
3#include <stdarg.h>
4
5#import <Foundation/Foundation.h>
6
7struct ArrayStruct {
8	int first;
9	int second;
10};
11
12typedef int int_array_t[4];
13typedef struct ArrayStruct struct_array_t[4];
14
15@interface OC_ArrayTest : NSObject
16{
17}
18#if 0
19	/* It seems to be impossible to return C arrays */
20+(int_array_t)arrayOf4Integers;
21+(struct_array_t)arrayOf4Structs;
22#endif
23
24-(NSObject*)arrayOf4Ints:(int_array_t)array;
25-(NSObject*)arrayOf4IntsIn:(in int_array_t)array;
26-(NSObject*)arrayOf4IntsInOut:(inout int_array_t)array;
27-(void)arrayOf4IntsOut:(out int_array_t)array;
28
29-(NSObject*)arrayOf4Structs:(struct_array_t)array;
30-(NSObject*)arrayOf4StructsIn:(in struct_array_t)array;
31-(NSObject*)arrayOf4StructsInOut:(inout struct_array_t)array;
32-(void)arrayOf4StructsOut:(out struct_array_t)array;
33
34+(NSObject*)callArrayOf4Ints:(OC_ArrayTest*)object;
35+(NSObject*)callArrayOf4IntsOut:(OC_ArrayTest*)object;
36+(NSObject*)callArrayOf4Structs:(OC_ArrayTest*)object;
37+(NSObject*)callArrayOf4StructsOut:(OC_ArrayTest*)object;
38@end
39
40@implementation OC_ArrayTest
41
42#if 0 
43	/* These don't compile for some reason */
44+(int_array_t)arrayOf4Integers;
45{
46static int_array_t gValue = { 4, 5, 6, 7 };
47	return gValue;
48}
49+(struct_array_t)arrayOf4Structs
50{
51static struct_array_t gValue = {
52		{ 9, 10 },
53		{ 12, 13 },
54		{ 21, 24 },
55		{ -1, -2 },
56	};
57	return gValue;
58}
59#endif
60
61-(NSObject*)arrayOf4Ints:(int_array_t)array
62{
63	return [NSArray arrayWithObjects:
64		[NSNumber numberWithInt:array[0]],
65		[NSNumber numberWithInt:array[1]],
66		[NSNumber numberWithInt:array[2]],
67		[NSNumber numberWithInt:array[3]],
68		nil];
69}
70-(NSObject*)arrayOf4IntsIn:(in int_array_t)array
71{
72	return [NSArray arrayWithObjects:
73		[NSNumber numberWithInt:array[0]],
74		[NSNumber numberWithInt:array[1]],
75		[NSNumber numberWithInt:array[2]],
76		[NSNumber numberWithInt:array[3]],
77		nil];
78}
79-(NSObject*)arrayOf4IntsInOut:(inout int_array_t)array
80{
81	NSObject* result = [NSArray arrayWithObjects:
82		[NSNumber numberWithInt:array[0]],
83		[NSNumber numberWithInt:array[1]],
84		[NSNumber numberWithInt:array[2]],
85		[NSNumber numberWithInt:array[3]],
86		nil];
87
88	array[0] += 42;
89	array[1] += 42;
90	array[2] += 42;
91	array[3] += 42;
92
93	return result;
94}
95-(void)arrayOf4IntsOut:(out int_array_t)array
96{
97	array[0] = 99;
98	array[1] = 100;
99	array[2] = 102;
100	array[3] = 110;
101}
102
103-(NSObject*)arrayOf4Structs:(struct_array_t)array
104{
105	return [NSArray arrayWithObjects:
106			[NSArray arrayWithObjects:
107				[NSNumber numberWithInt:array[0].first],
108				[NSNumber numberWithInt:array[0].second],
109				nil],
110			[NSArray arrayWithObjects:
111				[NSNumber numberWithInt:array[1].first],
112				[NSNumber numberWithInt:array[1].second],
113				nil],
114			[NSArray arrayWithObjects:
115				[NSNumber numberWithInt:array[2].first],
116				[NSNumber numberWithInt:array[2].second],
117				nil],
118			[NSArray arrayWithObjects:
119				[NSNumber numberWithInt:array[3].first],
120				[NSNumber numberWithInt:array[3].second],
121				nil],
122			nil
123		];
124}
125-(NSObject*)arrayOf4StructsIn:(in struct_array_t)array
126{
127	return [NSArray arrayWithObjects:
128			[NSArray arrayWithObjects:
129				[NSNumber numberWithInt:array[0].first],
130				[NSNumber numberWithInt:array[0].second],
131				nil],
132			[NSArray arrayWithObjects:
133				[NSNumber numberWithInt:array[1].first],
134				[NSNumber numberWithInt:array[1].second],
135				nil],
136			[NSArray arrayWithObjects:
137				[NSNumber numberWithInt:array[2].first],
138				[NSNumber numberWithInt:array[2].second],
139				nil],
140			[NSArray arrayWithObjects:
141				[NSNumber numberWithInt:array[3].first],
142				[NSNumber numberWithInt:array[3].second],
143				nil],
144			nil
145		];
146}
147-(NSObject*)arrayOf4StructsInOut:(inout struct_array_t)array
148{
149	NSObject* result = [NSArray arrayWithObjects:
150			[NSArray arrayWithObjects:
151				[NSNumber numberWithInt:array[0].first],
152				[NSNumber numberWithInt:array[0].second],
153				nil],
154			[NSArray arrayWithObjects:
155				[NSNumber numberWithInt:array[1].first],
156				[NSNumber numberWithInt:array[1].second],
157				nil],
158			[NSArray arrayWithObjects:
159				[NSNumber numberWithInt:array[2].first],
160				[NSNumber numberWithInt:array[2].second],
161				nil],
162			[NSArray arrayWithObjects:
163				[NSNumber numberWithInt:array[3].first],
164				[NSNumber numberWithInt:array[3].second],
165				nil],
166			nil
167		];
168
169	int i;
170	for (i = 0; i < 4; i++) {
171		array[i].first += 42;
172		array[i].second -= 42;
173	}
174
175	return result;
176}
177-(void)arrayOf4StructsOut:(out struct_array_t)array
178{
179	int i;
180	for (i = 0; i < 4; i++) {
181		array[i].first = 1 + i * i;
182		array[i].second = -4 - i * i * i;
183	}
184}
185
186+(NSObject*)callArrayOf4Ints:(OC_ArrayTest*)object
187{
188	int_array_t array = { 1, 2, 3, 4 };
189	return [object arrayOf4Ints:array];
190}
191
192+(NSObject*)callArrayOf4IntsOut:(OC_ArrayTest*)object
193{
194	int_array_t array;
195	[object arrayOf4IntsOut:array];
196	return [NSArray arrayWithObjects:
197			[NSNumber numberWithInt: array[0]],
198			[NSNumber numberWithInt: array[1]],
199			[NSNumber numberWithInt: array[2]],
200			[NSNumber numberWithInt: array[3]],
201			nil];
202}
203
204+(NSObject*)callArrayOf4Structs:(OC_ArrayTest*)object
205{
206	struct_array_t array = {
207		{ 1, 2 },
208		{ 3, 4 },
209		{ 5, 6 },
210		{ 7, 8 }
211	};
212	return [object arrayOf4Structs:array];
213}
214
215+(NSObject*)callArrayOf4StructsOut:(OC_ArrayTest*)object
216{
217	struct_array_t array;
218	[object arrayOf4StructsOut:array];
219	
220	return [NSArray arrayWithObjects:
221			[NSArray arrayWithObjects:
222				[NSNumber numberWithInt:array[0].first],
223				[NSNumber numberWithInt:array[0].second],
224				nil],
225			[NSArray arrayWithObjects:
226				[NSNumber numberWithInt:array[1].first],
227				[NSNumber numberWithInt:array[1].second],
228				nil],
229			[NSArray arrayWithObjects:
230				[NSNumber numberWithInt:array[2].first],
231				[NSNumber numberWithInt:array[2].second],
232				nil],
233			[NSArray arrayWithObjects:
234				[NSNumber numberWithInt:array[3].first],
235				[NSNumber numberWithInt:array[3].second],
236				nil],
237			nil
238		];
239}
240@end
241
242
243static PyMethodDef mod_methods[] = {
244	        { 0, 0, 0, 0 }
245};
246
247#if PY_VERSION_HEX >= 0x03000000
248
249static struct PyModuleDef mod_module = {
250	PyModuleDef_HEAD_INIT,
251	"arrays",
252	NULL,
253	0,
254	mod_methods,
255	NULL,
256	NULL,
257	NULL,
258	NULL
259};
260
261#define INITERROR() return NULL
262#define INITDONE() return m
263
264PyObject* PyInit_arrays(void);
265
266PyObject*
267PyInit_arrays(void)
268
269#else
270
271#define INITERROR() return
272#define INITDONE() return
273
274void initarrays(void);
275
276void
277initarrays(void)
278#endif
279{
280	PyObject* m;
281
282#if PY_VERSION_HEX >= 0x03000000
283	m = PyModule_Create(&mod_module);
284#else
285	m = Py_InitModule4("arrays", mod_methods,
286		NULL, NULL, PYTHON_API_VERSION);
287#endif
288	if (!m) {
289		INITERROR();
290	}
291
292	PyObjC_ImportAPI(m);
293
294	if (PyModule_AddObject(m, "OC_ArrayTest", 
295		PyObjCClass_New([OC_ArrayTest class])) < 0) {
296		INITERROR();
297	}
298
299	INITDONE();
300}
301