1 /***********************************************************************
3 * swig_lib/python/python.cfg
5 * Contains variable linking and pointer type-checking code.
6 ************************************************************************/
17 /* Definitions for Windows/Unix exporting */
18 #if defined(_WIN32) || defined(__WIN32__)
19 # if defined(_MSC_VER)
20 # define SWIGEXPORT(a) __declspec(dllexport) a
22 # if defined(__BORLANDC__)
23 # define SWIGEXPORT(a) a _export
25 # define SWIGEXPORT(a) a
29 # define SWIGEXPORT(a) a
33 #define SWIGSTATICRUNTIME(a) SWIGEXPORT(a)
35 #define SWIGSTATICRUNTIME(a) static a
40 PyObject *(*get_attr)(void);
41 int (*set_attr)(PyObject *);
44 typedef struct swig_varlinkobject {
46 swig_globalvar **vars;
51 /* ----------------------------------------------------------------------
54 Function for python repr method
55 ---------------------------------------------------------------------- */
58 swig_varlink_repr(swig_varlinkobject *v)
61 return PyString_FromString("<Global variables>");
64 /* ---------------------------------------------------------------------
67 Print out all of the global variable names
68 --------------------------------------------------------------------- */
71 swig_varlink_print(swig_varlinkobject *v, FILE *fp, int flags)
76 fprintf(fp,"Global variables { ");
78 fprintf(fp,"%s", v->vars[i]->name);
80 if (v->vars[i]) fprintf(fp,", ");
86 /* --------------------------------------------------------------------
89 This function gets the value of a variable and returns it as a
90 PyObject. In our case, we'll be looking at the datatype and
91 converting into a number or string
92 -------------------------------------------------------------------- */
95 swig_varlink_getattr(swig_varlinkobject *v, char *n)
101 if (strcmp(v->vars[i]->name,n) == 0) {
102 return (*v->vars[i]->get_attr)();
106 sprintf(temp,"C global variable %s not found.", n);
107 PyErr_SetString(PyExc_NameError,temp);
111 /* -------------------------------------------------------------------
112 swig_varlink_setattr()
114 This function sets the value of a variable.
115 ------------------------------------------------------------------- */
118 swig_varlink_setattr(swig_varlinkobject *v, char *n, PyObject *p)
123 if (strcmp(v->vars[i]->name,n) == 0) {
124 return (*v->vars[i]->set_attr)(p);
128 sprintf(temp,"C global variable %s not found.", n);
129 PyErr_SetString(PyExc_NameError,temp);
133 statichere PyTypeObject varlinktype = {
134 /* PyObject_HEAD_INIT(&PyType_Type) Note : This doesn't work on some machines */
135 PyObject_HEAD_INIT(0)
137 "varlink", /* Type name */
138 sizeof(swig_varlinkobject), /* Basic size */
141 (printfunc) swig_varlink_print, /* Print */
142 (getattrfunc) swig_varlink_getattr, /* get attr */
143 (setattrfunc) swig_varlink_setattr, /* Set attr */
145 (reprfunc) swig_varlink_repr, /* tp_repr */
146 0, /* tp_as_number */
147 0, /* tp_as_mapping*/
151 /* Create a variable linking object for use later */
153 SWIGSTATICRUNTIME(PyObject *)
154 SWIG_newvarlink(void)
156 swig_varlinkobject *result = 0;
157 result = PyMem_NEW(swig_varlinkobject,1);
158 varlinktype.ob_type = &PyType_Type; /* Patch varlinktype into a PyType */
159 result->ob_type = &varlinktype;
160 /* _Py_NewReference(result); Does not seem to be necessary */
162 result->maxvars = 64;
163 result->vars = (swig_globalvar **) malloc(64*sizeof(swig_globalvar *));
165 result->ob_refcnt = 0;
166 Py_XINCREF((PyObject *) result);
167 return ((PyObject*) result);
170 SWIGSTATICRUNTIME(void)
171 SWIG_addvarlink(PyObject *p, char *name,
172 PyObject *(*get_attr)(void), int (*set_attr)(PyObject *p))
174 swig_varlinkobject *v;
175 v= (swig_varlinkobject *) p;
177 if (v->nvars >= v->maxvars -1) {
178 v->maxvars = 2*v->maxvars;
179 v->vars = (swig_globalvar **) realloc(v->vars,v->maxvars*sizeof(swig_globalvar *));
180 if (v->vars == NULL) {
181 fprintf(stderr,"SWIG : Fatal error in initializing Python module.\n");
185 v->vars[v->nvars] = (swig_globalvar *) malloc(sizeof(swig_globalvar));
186 v->vars[v->nvars]->name = (char *) malloc(strlen(name)+1);
187 strcpy(v->vars[v->nvars]->name,name);
188 v->vars[v->nvars]->get_attr = get_attr;
189 v->vars[v->nvars]->set_attr = set_attr;
191 v->vars[v->nvars] = 0;
194 /* -----------------------------------------------------------------------------
195 * Pointer type-checking
196 * ----------------------------------------------------------------------------- */
198 /* SWIG pointer structure */
199 typedef struct SwigPtrType {
200 char *name; /* Datatype name */
201 int len; /* Length (used for optimization) */
202 void *(*cast)(void *); /* Pointer casting function */
203 struct SwigPtrType *next; /* Linked list pointer */
206 /* Pointer cache structure */
208 int stat; /* Status (valid) bit */
209 SwigPtrType *tp; /* Pointer to type structure */
210 char name[256]; /* Given datatype name */
211 char mapped[256]; /* Equivalent name */
214 static int SwigPtrMax = 64; /* Max entries that can be currently held */
215 static int SwigPtrN = 0; /* Current number of entries */
216 static int SwigPtrSort = 0; /* Status flag indicating sort */
217 static int SwigStart[256]; /* Starting positions of types */
218 static SwigPtrType *SwigPtrTable = 0; /* Table containing pointer equivalences */
221 #define SWIG_CACHESIZE 8
222 #define SWIG_CACHEMASK 0x7
223 static SwigCacheType SwigCache[SWIG_CACHESIZE];
224 static int SwigCacheIndex = 0;
225 static int SwigLastCache = 0;
227 /* Sort comparison function */
228 static int swigsort(const void *data1, const void *data2) {
229 SwigPtrType *d1 = (SwigPtrType *) data1;
230 SwigPtrType *d2 = (SwigPtrType *) data2;
231 return strcmp(d1->name,d2->name);
234 /* Register a new datatype with the type-checker */
235 SWIGSTATICRUNTIME(void)
236 SWIG_RegisterMapping(char *origtype, char *newtype, void *(*cast)(void *)) {
238 SwigPtrType *t = 0,*t1;
240 /* Allocate the pointer table if necessary */
242 SwigPtrTable = (SwigPtrType *) malloc(SwigPtrMax*sizeof(SwigPtrType));
246 if (SwigPtrN >= SwigPtrMax) {
247 SwigPtrMax = 2*SwigPtrMax;
248 SwigPtrTable = (SwigPtrType *) realloc((char *) SwigPtrTable,SwigPtrMax*sizeof(SwigPtrType));
250 for (i = 0; i < SwigPtrN; i++) {
251 if (strcmp(SwigPtrTable[i].name,origtype) == 0) {
252 t = &SwigPtrTable[i];
257 t = &SwigPtrTable[SwigPtrN++];
259 t->len = strlen(t->name);
264 /* Check for existing entries */
266 if ((strcmp(t->name,newtype) == 0)) {
267 if (cast) t->cast = cast;
272 t1 = (SwigPtrType *) malloc(sizeof(SwigPtrType));
274 t1->len = strlen(t1->name);
281 /* Make a pointer value string */
282 SWIGSTATICRUNTIME(void)
283 SWIG_MakePtr(char *c, const void *ptr, char *type) {
284 static char hex[17] = "0123456789abcdef";
288 p = (unsigned long) ptr;
304 /* Function for getting a pointer value */
305 SWIGSTATICRUNTIME(char *)
306 SWIG_GetPtr(char *c, void **ptr, char *t)
309 char temp_type[256], *name;
310 int i, len, start, end;
312 SwigCacheType *cache;
316 /* Pointer values must start with leading underscore */
319 if (strcmp(c,"NULL") == 0) return (char *) 0;
323 /* Extract hex value from pointer */
325 if ((d >= '0') && (d <= '9'))
326 p = (p << 4) + (d - '0');
327 else if ((d >= 'a') && (d <= 'f'))
328 p = (p << 4) + (d - ('a'-10));
334 if ((!t) || (strcmp(t,c)==0)) return (char *) 0;
337 qsort((void *) SwigPtrTable, SwigPtrN, sizeof(SwigPtrType), swigsort);
338 for (i = 0; i < 256; i++) SwigStart[i] = SwigPtrN;
339 for (i = SwigPtrN-1; i >= 0; i--) SwigStart[(int) (SwigPtrTable[i].name[1])] = i;
340 for (i = 255; i >= 1; i--) {
341 if (SwigStart[i-1] > SwigStart[i])
342 SwigStart[i-1] = SwigStart[i];
345 for (i = 0; i < SWIG_CACHESIZE; i++) SwigCache[i].stat = 0;
347 /* First check cache for matches. Uses last cache value as starting point */
348 cache = &SwigCache[SwigLastCache];
349 for (i = 0; i < SWIG_CACHESIZE; i++) {
350 if (cache->stat && (strcmp(t,cache->name) == 0) && (strcmp(c,cache->mapped) == 0)) {
352 if (cache->tp->cast) *ptr = (*(cache->tp->cast))(*ptr);
355 SwigLastCache = (SwigLastCache+1) & SWIG_CACHEMASK;
356 if (!SwigLastCache) cache = SwigCache;
359 /* Type mismatch. Look through type-mapping table */
360 start = SwigStart[(int) t[1]];
361 end = SwigStart[(int) t[1]+1];
362 sp = &SwigPtrTable[start];
364 /* Try to find a match */
365 while (start <= end) {
366 if (strncmp(t,sp->name,sp->len) == 0) {
370 /* Try to find entry for our given datatype */
372 if (tp->len >= 255) {
375 strcpy(temp_type,tp->name);
376 strncat(temp_type,t+len,255-tp->len);
377 if (strcmp(c,temp_type) == 0) {
378 strcpy(SwigCache[SwigCacheIndex].mapped,c);
379 strcpy(SwigCache[SwigCacheIndex].name,t);
380 SwigCache[SwigCacheIndex].stat = 1;
381 SwigCache[SwigCacheIndex].tp = tp;
382 SwigCacheIndex = SwigCacheIndex & SWIG_CACHEMASK;
383 /* Get pointer value */
385 if (tp->cast) *ptr = (*(tp->cast))(*ptr);
397 /* New object-based GetPointer function. This uses the Python abstract
398 * object interface to automatically dereference the 'this' attribute
399 * of shadow objects. */
401 SWIGSTATICRUNTIME(char *)
402 SWIG_GetPtrObj(PyObject *obj, void **ptr, char *type) {
403 PyObject *sobj = obj;
405 if (!PyString_Check(obj)) {
406 sobj = PyObject_GetAttrString(obj,"this");
407 if (!sobj) return "";
409 str = PyString_AsString(sobj);
410 return SWIG_GetPtr(str,ptr,type);