1/* 2 * This file contains type and function definitions that are helpfull for 3 * adding constants to the python module 4 * 5 * NOTE: The functions are defined as 'static inline' to avoid compiler 6 * warnings. 7 * NOTE2: The functions are not in a seperate file because I am lazy and 8 * to make using this file easier 9 * NOTE3: This file is used by the files generated by the scripts in 10 * ~/Scripts/CodeGenerators. 11 * 12 */ 13 14#import <ApplicationServices/ApplicationServices.h> 15#import <Foundation/NSString.h> 16#import <Foundation/NSArray.h> 17 18struct vartable { 19 NSString* name; 20 char* type; 21}; 22 23struct inttable { 24 char* name; 25 int is_unsigned; 26 int value; 27}; 28 29struct stringtable { 30 char* name; 31 NSString* const* value; 32}; 33 34static inline int add_double(PyObject*d, char* name, double value) 35{ 36 int res; 37 PyObject* v; 38 39 v = PyObjC_ObjCToPython(@encode(double), &value); 40 if (v == NULL) return -1; 41 42 res = PyDict_SetItemString(d, name, v); 43 Py_DECREF(v); 44 if (res < 0) return -1; 45 return 0; 46} 47 48#ifndef NO_OBJC2_RUNTIME 49static inline int add_CGFloat(PyObject*d, char* name, double value) 50{ 51 int res; 52 PyObject* v; 53 54 v = PyObjC_ObjCToPython(@encode(CGFloat), &value); 55 if (v == NULL) return -1; 56 57 res = PyDict_SetItemString(d, name, v); 58 Py_DECREF(v); 59 if (res < 0) return -1; 60 return 0; 61} 62#endif 63 64 65static inline int add_float(PyObject*d, char* name, float value) 66{ 67 int res; 68 PyObject* v; 69 70 v = PyObjC_ObjCToPython(@encode(float), &value); 71 if (v == NULL) return -1; 72 73 res = PyDict_SetItemString(d, name, v); 74 Py_DECREF(v); 75 if (res < 0) return -1; 76 return 0; 77} 78 79static inline int add_unsigned(PyObject*d, char* name, unsigned value) 80{ 81 int res; 82 PyObject* v; 83 84 v = PyObjC_ObjCToPython(@encode(unsigned), &value); 85 if (v == NULL) return -1; 86 87 res = PyDict_SetItemString(d, name, v); 88 Py_DECREF(v); 89 if (res < 0) return -1; 90 return 0; 91} 92 93static inline int add_BOOL(PyObject*d, char* name, BOOL value) 94{ 95 int res; 96 PyObject* v; 97 98 v = PyBool_FromLong(value); 99 if (v == NULL) return -1; 100 101 res = PyDict_SetItemString(d, name, v); 102 Py_DECREF(v); 103 if (res < 0) return -1; 104 return 0; 105} 106 107static inline int add_int(PyObject*d, char* name, int value) 108{ 109 int res; 110 PyObject* v; 111 112 v = PyObjC_ObjCToPython(@encode(int), &value); 113 if (v == NULL) return -1; 114 115 res = PyDict_SetItemString(d, name, v); 116 Py_DECREF(v); 117 if (res < 0) return -1; 118 return 0; 119} 120#ifndef NO_OBJC2_RUNTIME 121static inline int add_NSUInteger(PyObject*d, char* name, NSUInteger value) 122{ 123 int res; 124 PyObject* v; 125 126 v = PyObjC_ObjCToPython(@encode(NSUInteger), &value); 127 if (v == NULL) return -1; 128 129 res = PyDict_SetItemString(d, name, v); 130 Py_DECREF(v); 131 if (res < 0) return -1; 132 return 0; 133} 134#endif 135#if 0 136static inline int add_NSInteger(PyObject*d, char* name, NSInteger value) 137{ 138 int res; 139 PyObject* v; 140 141 v = PyObjC_ObjCToPython(@encode(NSUInteger), &value); 142 if (v == NULL) return -1; 143 144 res = PyDict_SetItemString(d, name, v); 145 Py_DECREF(v); 146 if (res < 0) return -1; 147 return 0; 148} 149#endif 150 151static inline int register_ints(PyObject* d, struct inttable* table) 152{ 153 while (table->name != NULL) { 154 if (table->is_unsigned) { 155 int res = add_unsigned(d, table->name, 156 (unsigned)table->value); 157 if (res == -1) return -1; 158 } else { 159 int res = add_int(d, table->name, table->value); 160 if (res == -1) return -1; 161 } 162 163 table++; 164 } 165 return 0; 166} 167 168static inline int add_string(PyObject* d, char* name, NSString* value) 169{ 170 int res; 171 PyObject* v; 172 173 v = PyObjC_ObjCToPython(@encode(id), &value); 174 if (v == NULL) return -1; 175 176 res = PyDict_SetItemString(d, name, v); 177 if (res < 0) return -1; 178 return 0; 179} 180 181static inline int add_id(PyObject* d, char* name, id value) 182{ 183 int res; 184 PyObject* v; 185 186 v = PyObjC_ObjCToPython(@encode(id), &value); 187 if (v == NULL) return -1; 188 189 res = PyDict_SetItemString(d, name, v); 190 if (res < 0) return -1; 191 return 0; 192} 193 194 195static inline int register_strings(PyObject* d, struct stringtable* table) 196{ 197 while (table->name != NULL) { 198 add_string(d, table->name, *table->value); 199 table++; 200 } 201 return 0; 202} 203 204#import <CoreFoundation/CoreFoundation.h> 205 206static inline int 207register_variableList(PyObject* d, CFBundleRef bundle __attribute__((__unused__)), struct vartable* table, size_t count) 208{ 209 void** ptrs = NULL; 210 NSMutableArray* names = nil; 211 size_t i; 212 int retVal = 0; 213 214 ptrs = PyMem_Malloc(sizeof(void*) * count); 215 if (ptrs == NULL) { 216 PyErr_NoMemory(); 217 return -1; 218 } 219 220 names = [[NSMutableArray alloc] init]; 221 if (names == NULL) { 222 PyErr_NoMemory(); 223 retVal = -1; 224 goto cleanup; 225 } 226 227 for (i = 0; i < count; i++) { 228 [names addObject:table[i].name]; 229 } 230 231 CFBundleGetDataPointersForNames(bundle, 232 (CFArrayRef)names, ptrs); 233 234 for (i = 0; i < count; i++) { 235 PyObject* val; 236 if (ptrs[i] == NULL) continue; /* Skip undefined names */ 237 238 val = PyObjC_ObjCToPython(table[i].type, ptrs[i]); 239 if (val == NULL) { 240 retVal = -1; 241 goto cleanup; 242 } 243 PyDict_SetItemString(d, (char*)[table[i].name cString], val); 244 Py_DECREF(val); 245 } 246 247cleanup: 248 if (ptrs) { 249 PyMem_Free(ptrs); 250 } 251 252 [names release]; 253 254 return retVal; 255} 256