1/* 2 * Helper methods struct tests (objc.test.test_struct) 3 */ 4#include "Python.h" 5#include "pyobjc-api.h" 6#include <stdarg.h> 7 8#import <Foundation/Foundation.h> 9 10struct FooStruct { 11 int first; 12 int second; 13}; 14 15@interface OC_StructTest : NSObject 16{ 17} 18+(struct FooStruct)createWithFirst:(int)first andSecond:(int)second; 19+(int)sumFields:(struct FooStruct)foo; 20-(NSObject*)arrayOf4Structs:(struct FooStruct[4])argument; 21 22+(NSObject*)callArrayOf4Structs:(OC_StructTest*)object; 23@end 24 25@implementation OC_StructTest 26+(struct FooStruct)createWithFirst:(int)first andSecond:(int)second 27{ 28 struct FooStruct f; 29 f.first = first; 30 f.second = second; 31 return f; 32} 33 34+(int)sumFields:(struct FooStruct)foo 35{ 36 return foo.first + foo.second; 37} 38 39+(NSObject*)callArrayOf4Structs:(OC_StructTest*)object 40{ 41static struct FooStruct structs[4] = { 42 { 1, 2 }, 43 { 3, 4 }, 44 { 5, 6 }, 45 { 7, 8 }, 46 }; 47 48 return [object arrayOf4Structs:structs]; 49} 50-(NSObject*)arrayOf4Structs:(struct FooStruct[4])argument 51{ 52 return [NSData dataWithBytes:(void*)argument length:4*sizeof(*argument)]; 53} 54 55@end 56 57 58static PyMethodDef mod_methods[] = { 59 { 0, 0, 0, 0 } 60}; 61 62#if PY_VERSION_HEX >= 0x03000000 63 64static struct PyModuleDef mod_module = { 65 PyModuleDef_HEAD_INIT, 66 "structs", 67 NULL, 68 0, 69 mod_methods, 70 NULL, 71 NULL, 72 NULL, 73 NULL 74}; 75 76#define INITERROR() return NULL 77#define INITDONE() return m 78 79PyObject* PyInit_structs(void); 80 81PyObject* 82PyInit_structs(void) 83 84#else 85 86#define INITERROR() return 87#define INITDONE() return 88 89void initstructs(void); 90 91void 92initstructs(void) 93#endif 94{ 95 PyObject* m; 96 97 98#if PY_VERSION_HEX >= 0x03000000 99 m = PyModule_Create(&mod_module); 100#else 101 m = Py_InitModule4("structs", mod_methods, 102 NULL, NULL, PYTHON_API_VERSION); 103#endif 104 if (!m) { 105 INITERROR(); 106 } 107 108 if (PyObjC_ImportAPI(m) < 0) { 109 INITERROR(); 110 } 111 112 if (PyModule_AddObject(m, "OC_StructTest", 113 PyObjCClass_New([OC_StructTest class])) < 0) { 114 INITERROR(); 115 } 116 117 INITDONE(); 118} 119