]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxSWIG/swig_lib/python/ptrlang.i
Updates for building wxSWIG on Linux
[wxWidgets.git] / wxPython / wxSWIG / swig_lib / python / ptrlang.i
1 //
2 // SWIG pointer conversion and utility library
3 //
4 // Dave Beazley
5 // April 19, 1997
6 //
7 // Python specific implementation. This file is included
8 // by the file ../pointer.i
9
10 %{
11
12 #include <ctype.h>
13
14 /*------------------------------------------------------------------
15 ptrcast(value,type)
16
17 Constructs a new pointer value. Value may either be a string
18 or an integer. Type is a string corresponding to either the
19 C datatype or mangled datatype.
20
21 ptrcast(0,"Vector *")
22 or
23 ptrcast(0,"Vector_p")
24 ------------------------------------------------------------------ */
25
26 static PyObject *ptrcast(PyObject *_PTRVALUE, char *type) {
27
28 char *r,*s;
29 void *ptr;
30 PyObject *obj;
31 char *typestr,*c;
32
33 /* Produce a "mangled" version of the type string. */
34
35 typestr = (char *) malloc(strlen(type)+2);
36
37 /* Go through and munge the typestring */
38
39 r = typestr;
40 *(r++) = '_';
41 c = type;
42 while (*c) {
43 if (!isspace(*c)) {
44 if ((*c == '*') || (*c == '&')) {
45 *(r++) = 'p';
46 }
47 else *(r++) = *c;
48 } else {
49 *(r++) = '_';
50 }
51 c++;
52 }
53 *(r++) = 0;
54
55 /* Check to see what kind of object _PTRVALUE is */
56
57 if (PyInt_Check(_PTRVALUE)) {
58 ptr = (void *) PyInt_AsLong(_PTRVALUE);
59 /* Received a numerical value. Make a pointer out of it */
60 r = (char *) malloc(strlen(typestr)+22);
61 if (ptr) {
62 SWIG_MakePtr(r, ptr, typestr);
63 } else {
64 sprintf(r,"_0%s",typestr);
65 }
66 obj = PyString_FromString(r);
67 free(r);
68 } else if (PyString_Check(_PTRVALUE)) {
69 /* Have a real pointer value now. Try to strip out the pointer
70 value */
71 s = PyString_AsString(_PTRVALUE);
72 r = (char *) malloc(strlen(type)+22);
73
74 /* Now extract the pointer value */
75 if (!SWIG_GetPtr(s,&ptr,0)) {
76 if (ptr) {
77 SWIG_MakePtr(r,ptr,typestr);
78 } else {
79 sprintf(r,"_0%s",typestr);
80 }
81 obj = PyString_FromString(r);
82 } else {
83 obj = NULL;
84 }
85 free(r);
86 } else {
87 obj = NULL;
88 }
89 free(typestr);
90 if (!obj)
91 PyErr_SetString(PyExc_TypeError,"Type error in ptrcast. Argument is not a valid pointer value.");
92 return obj;
93 }
94
95 /*------------------------------------------------------------------
96 ptrvalue(ptr,type = 0)
97
98 Attempts to dereference a pointer value. If type is given, it
99 will try to use that type. Otherwise, this function will attempt
100 to "guess" the proper datatype by checking against all of the
101 builtin C datatypes.
102 ------------------------------------------------------------------ */
103
104 static PyObject *ptrvalue(PyObject *_PTRVALUE, int index, char *type) {
105 void *ptr;
106 char *s;
107 PyObject *obj;
108
109 if (!PyString_Check(_PTRVALUE)) {
110 PyErr_SetString(PyExc_TypeError,"Type error in ptrvalue. Argument is not a valid pointer value.");
111 return NULL;
112 }
113 s = PyString_AsString(_PTRVALUE);
114 if (SWIG_GetPtr(s,&ptr,0)) {
115 PyErr_SetString(PyExc_TypeError,"Type error in ptrvalue. Argument is not a valid pointer value.");
116 return NULL;
117 }
118
119 /* If no datatype was passed, try a few common datatypes first */
120
121 if (!type) {
122
123 /* No datatype was passed. Type to figure out if it's a common one */
124
125 if (!SWIG_GetPtr(s,&ptr,"_int_p")) {
126 type = "int";
127 } else if (!SWIG_GetPtr(s,&ptr,"_double_p")) {
128 type = "double";
129 } else if (!SWIG_GetPtr(s,&ptr,"_short_p")) {
130 type = "short";
131 } else if (!SWIG_GetPtr(s,&ptr,"_long_p")) {
132 type = "long";
133 } else if (!SWIG_GetPtr(s,&ptr,"_float_p")) {
134 type = "float";
135 } else if (!SWIG_GetPtr(s,&ptr,"_char_p")) {
136 type = "char";
137 } else if (!SWIG_GetPtr(s,&ptr,"_char_pp")) {
138 type = "char *";
139 } else {
140 type = "unknown";
141 }
142 }
143
144 if (!ptr) {
145 PyErr_SetString(PyExc_TypeError,"Unable to dereference NULL pointer.");
146 return NULL;
147 }
148
149 /* Now we have a datatype. Try to figure out what to do about it */
150 if (strcmp(type,"int") == 0) {
151 obj = PyInt_FromLong((long) *(((int *) ptr) + index));
152 } else if (strcmp(type,"double") == 0) {
153 obj = PyFloat_FromDouble((double) *(((double *) ptr)+index));
154 } else if (strcmp(type,"short") == 0) {
155 obj = PyInt_FromLong((long) *(((short *) ptr)+index));
156 } else if (strcmp(type,"long") == 0) {
157 obj = PyInt_FromLong((long) *(((long *) ptr)+index));
158 } else if (strcmp(type,"float") == 0) {
159 obj = PyFloat_FromDouble((double) *(((float *) ptr)+index));
160 } else if (strcmp(type,"char") == 0) {
161 obj = PyString_FromString(((char *) ptr)+index);
162 } else if (strcmp(type,"char *") == 0) {
163 char *c = *(((char **) ptr)+index);
164 if (c) obj = PyString_FromString(c);
165 else obj = PyString_FromString("NULL");
166 } else {
167 PyErr_SetString(PyExc_TypeError,"Unable to dereference unsupported datatype.");
168 return NULL;
169 }
170 return obj;
171 }
172
173 /*------------------------------------------------------------------
174 ptrcreate(type,value = 0,numelements = 1)
175
176 Attempts to create a new object of given type. Type must be
177 a basic C datatype. Will not create complex objects.
178 ------------------------------------------------------------------ */
179
180 static PyObject *ptrcreate(char *type, PyObject *_PYVALUE, int numelements) {
181 void *ptr;
182 PyObject *obj;
183 int sz;
184 char *cast;
185 char temp[40];
186
187 /* Check the type string against a variety of possibilities */
188
189 if (strcmp(type,"int") == 0) {
190 sz = sizeof(int)*numelements;
191 cast = "_int_p";
192 } else if (strcmp(type,"short") == 0) {
193 sz = sizeof(short)*numelements;
194 cast = "_short_p";
195 } else if (strcmp(type,"long") == 0) {
196 sz = sizeof(long)*numelements;
197 cast = "_long_p";
198 } else if (strcmp(type,"double") == 0) {
199 sz = sizeof(double)*numelements;
200 cast = "_double_p";
201 } else if (strcmp(type,"float") == 0) {
202 sz = sizeof(float)*numelements;
203 cast = "_float_p";
204 } else if (strcmp(type,"char") == 0) {
205 sz = sizeof(char)*numelements;
206 cast = "_char_p";
207 } else if (strcmp(type,"char *") == 0) {
208 sz = sizeof(char *)*(numelements+1);
209 cast = "_char_pp";
210 } else {
211 PyErr_SetString(PyExc_TypeError,"Unable to create unknown datatype.");
212 return NULL;
213 }
214
215 /* Create the new object */
216
217 ptr = (void *) malloc(sz);
218 if (!ptr) {
219 PyErr_SetString(PyExc_MemoryError,"Out of memory in swig_create.");
220 return NULL;
221 }
222
223 /* Now try to set its default value */
224
225 if (_PYVALUE) {
226 if (strcmp(type,"int") == 0) {
227 int *ip,i,ivalue;
228 ivalue = (int) PyInt_AsLong(_PYVALUE);
229 ip = (int *) ptr;
230 for (i = 0; i < numelements; i++)
231 ip[i] = ivalue;
232 } else if (strcmp(type,"short") == 0) {
233 short *ip,ivalue;
234 int i;
235 ivalue = (short) PyInt_AsLong(_PYVALUE);
236 ip = (short *) ptr;
237 for (i = 0; i < numelements; i++)
238 ip[i] = ivalue;
239 } else if (strcmp(type,"long") == 0) {
240 long *ip,ivalue;
241 int i;
242 ivalue = (long) PyInt_AsLong(_PYVALUE);
243 ip = (long *) ptr;
244 for (i = 0; i < numelements; i++)
245 ip[i] = ivalue;
246 } else if (strcmp(type,"double") == 0) {
247 double *ip,ivalue;
248 int i;
249 ivalue = (double) PyFloat_AsDouble(_PYVALUE);
250 ip = (double *) ptr;
251 for (i = 0; i < numelements; i++)
252 ip[i] = ivalue;
253 } else if (strcmp(type,"float") == 0) {
254 float *ip,ivalue;
255 int i;
256 ivalue = (float) PyFloat_AsDouble(_PYVALUE);
257 ip = (float *) ptr;
258 for (i = 0; i < numelements; i++)
259 ip[i] = ivalue;
260 } else if (strcmp(type,"char") == 0) {
261 char *ip,*ivalue;
262 ivalue = (char *) PyString_AsString(_PYVALUE);
263 ip = (char *) ptr;
264 strncpy(ip,ivalue,numelements-1);
265 } else if (strcmp(type,"char *") == 0) {
266 char **ip, *ivalue;
267 int i;
268 ivalue = (char *) PyString_AsString(_PYVALUE);
269 ip = (char **) ptr;
270 for (i = 0; i < numelements; i++) {
271 if (ivalue) {
272 ip[i] = (char *) malloc(strlen(ivalue)+1);
273 strcpy(ip[i],ivalue);
274 } else {
275 ip[i] = 0;
276 }
277 }
278 ip[numelements] = 0;
279 }
280 }
281 /* Create the pointer value */
282
283 SWIG_MakePtr(temp,ptr,cast);
284 obj = PyString_FromString(temp);
285 return obj;
286 }
287
288
289 /*------------------------------------------------------------------
290 ptrset(ptr,value,index = 0,type = 0)
291
292 Attempts to set the value of a pointer variable. If type is
293 given, we will use that type. Otherwise, we'll guess the datatype.
294 ------------------------------------------------------------------ */
295
296 static PyObject *ptrset(PyObject *_PTRVALUE, PyObject *_PYVALUE, int index, char *type) {
297 void *ptr;
298 char *s;
299 PyObject *obj;
300
301 if (!PyString_Check(_PTRVALUE)) {
302 PyErr_SetString(PyExc_TypeError,"Type error in ptrset. Argument is not a valid pointer value.");
303 return NULL;
304 }
305 s = PyString_AsString(_PTRVALUE);
306 if (SWIG_GetPtr(s,&ptr,0)) {
307 PyErr_SetString(PyExc_TypeError,"Type error in ptrset. Argument is not a valid pointer value.");
308 return NULL;
309 }
310
311 /* If no datatype was passed, try a few common datatypes first */
312
313 if (!type) {
314
315 /* No datatype was passed. Type to figure out if it's a common one */
316
317 if (!SWIG_GetPtr(s,&ptr,"_int_p")) {
318 type = "int";
319 } else if (!SWIG_GetPtr(s,&ptr,"_double_p")) {
320 type = "double";
321 } else if (!SWIG_GetPtr(s,&ptr,"_short_p")) {
322 type = "short";
323 } else if (!SWIG_GetPtr(s,&ptr,"_long_p")) {
324 type = "long";
325 } else if (!SWIG_GetPtr(s,&ptr,"_float_p")) {
326 type = "float";
327 } else if (!SWIG_GetPtr(s,&ptr,"_char_p")) {
328 type = "char";
329 } else if (!SWIG_GetPtr(s,&ptr,"_char_pp")) {
330 type = "char *";
331 } else {
332 type = "unknown";
333 }
334 }
335
336 if (!ptr) {
337 PyErr_SetString(PyExc_TypeError,"Unable to set NULL pointer.");
338 return NULL;
339 }
340
341 /* Now we have a datatype. Try to figure out what to do about it */
342 if (strcmp(type,"int") == 0) {
343 *(((int *) ptr)+index) = (int) PyInt_AsLong(_PYVALUE);
344 } else if (strcmp(type,"double") == 0) {
345 *(((double *) ptr)+index) = (double) PyFloat_AsDouble(_PYVALUE);
346 } else if (strcmp(type,"short") == 0) {
347 *(((short *) ptr)+index) = (short) PyInt_AsLong(_PYVALUE);
348 } else if (strcmp(type,"long") == 0) {
349 *(((long *) ptr)+index) = (long) PyInt_AsLong(_PYVALUE);
350 } else if (strcmp(type,"float") == 0) {
351 *(((float *) ptr)+index) = (float) PyFloat_AsDouble(_PYVALUE);
352 } else if (strcmp(type,"char") == 0) {
353 char *c = PyString_AsString(_PYVALUE);
354 strcpy(((char *) ptr)+index, c);
355 } else if (strcmp(type,"char *") == 0) {
356 char *c = PyString_AsString(_PYVALUE);
357 char **ca = (char **) ptr;
358 if (ca[index]) free(ca[index]);
359 if (strcmp(c,"NULL") == 0) {
360 ca[index] = 0;
361 } else {
362 ca[index] = (char *) malloc(strlen(c)+1);
363 strcpy(ca[index],c);
364 }
365 } else {
366 PyErr_SetString(PyExc_TypeError,"Unable to set unsupported datatype.");
367 return NULL;
368 }
369 Py_INCREF(Py_None);
370 return Py_None;
371 }
372
373
374 /*------------------------------------------------------------------
375 ptradd(ptr,offset)
376
377 Adds a value to an existing pointer value. Will do a type-dependent
378 add for basic datatypes. For other datatypes, will do a byte-add.
379 ------------------------------------------------------------------ */
380
381 static PyObject *ptradd(PyObject *_PTRVALUE, int offset) {
382
383 char *r,*s;
384 void *ptr,*junk;
385 PyObject *obj;
386 char *type;
387
388 /* Check to see what kind of object _PTRVALUE is */
389
390 if (PyString_Check(_PTRVALUE)) {
391 /* Have a potential pointer value now. Try to strip out the value */
392 s = PyString_AsString(_PTRVALUE);
393
394 /* Try to handle a few common datatypes first */
395
396 if (!SWIG_GetPtr(s,&ptr,"_int_p")) {
397 ptr = (void *) (((int *) ptr) + offset);
398 } else if (!SWIG_GetPtr(s,&ptr,"_double_p")) {
399 ptr = (void *) (((double *) ptr) + offset);
400 } else if (!SWIG_GetPtr(s,&ptr,"_short_p")) {
401 ptr = (void *) (((short *) ptr) + offset);
402 } else if (!SWIG_GetPtr(s,&ptr,"_long_p")) {
403 ptr = (void *) (((long *) ptr) + offset);
404 } else if (!SWIG_GetPtr(s,&ptr,"_float_p")) {
405 ptr = (void *) (((float *) ptr) + offset);
406 } else if (!SWIG_GetPtr(s,&ptr,"_char_p")) {
407 ptr = (void *) (((char *) ptr) + offset);
408 } else if (!SWIG_GetPtr(s,&ptr,0)) {
409 ptr = (void *) (((char *) ptr) + offset);
410 } else {
411 PyErr_SetString(PyExc_TypeError,"Type error in ptradd. Argument is not a valid pointer value.");
412 return NULL;
413 }
414 type = SWIG_GetPtr(s,&junk,"INVALID POINTER");
415 r = (char *) malloc(strlen(type)+20);
416 if (ptr) {
417 SWIG_MakePtr(r,ptr,type);
418 } else {
419 sprintf(r,"_0%s",type);
420 }
421 obj = PyString_FromString(r);
422 free(r);
423 }
424 return obj;
425 }
426
427 /*------------------------------------------------------------------
428 ptrmap(type1,type2)
429
430 Allows a mapping between type1 and type2. (Like a typedef)
431 ------------------------------------------------------------------ */
432
433 static void ptrmap(char *type1, char *type2) {
434
435 char *typestr1,*typestr2,*c,*r;
436
437 /* Produce a "mangled" version of the type string. */
438
439 typestr1 = (char *) malloc(strlen(type1)+2);
440
441 /* Go through and munge the typestring */
442
443 r = typestr1;
444 *(r++) = '_';
445 c = type1;
446 while (*c) {
447 if (!isspace(*c)) {
448 if ((*c == '*') || (*c == '&')) {
449 *(r++) = 'p';
450 }
451 else *(r++) = *c;
452 } else {
453 *(r++) = '_';
454 }
455 c++;
456 }
457 *(r++) = 0;
458
459 typestr2 = (char *) malloc(strlen(type2)+2);
460
461 /* Go through and munge the typestring */
462
463 r = typestr2;
464 *(r++) = '_';
465 c = type2;
466 while (*c) {
467 if (!isspace(*c)) {
468 if ((*c == '*') || (*c == '&')) {
469 *(r++) = 'p';
470 }
471 else *(r++) = *c;
472 } else {
473 *(r++) = '_';
474 }
475 c++;
476 }
477 *(r++) = 0;
478 SWIG_RegisterMapping(typestr1,typestr2,0);
479 SWIG_RegisterMapping(typestr2,typestr1,0);
480 }
481
482 /*------------------------------------------------------------------
483 ptrfree(ptr)
484
485 Destroys a pointer value
486 ------------------------------------------------------------------ */
487
488 PyObject *ptrfree(PyObject *_PTRVALUE) {
489 void *ptr, *junk;
490 char *s;
491
492 if (!PyString_Check(_PTRVALUE)) {
493 PyErr_SetString(PyExc_TypeError,"Type error in ptrfree. Argument is not a valid pointer value.");
494 return NULL;
495 }
496 s = PyString_AsString(_PTRVALUE);
497 if (SWIG_GetPtr(s,&ptr,0)) {
498 PyErr_SetString(PyExc_TypeError,"Type error in ptrfree. Argument is not a valid pointer value.");
499 return NULL;
500 }
501
502 /* Check to see if this pointer is a char ** */
503 if (!SWIG_GetPtr(s,&junk,"_char_pp")) {
504 char **c = (char **) ptr;
505 if (c) {
506 int i = 0;
507 while (c[i]) {
508 free(c[i]);
509 i++;
510 }
511 }
512 }
513 if (ptr)
514 free((char *) ptr);
515
516 Py_INCREF(Py_None);
517 return Py_None;
518 }
519
520 %}
521 %typemap(python,in) PyObject *ptr, PyObject *value {
522 $target = $source;
523 }
524
525 %typemap(python,out) PyObject *ptrcast,
526 PyObject *ptrvalue,
527 PyObject *ptrcreate,
528 PyObject *ptrset,
529 PyObject *ptradd,
530 PyObject *ptrfree
531 {
532 $target = $source;
533 }
534
535 %typemap(python,ret) int ptrset {
536 if ($source == -1) return NULL;
537 }
538
539 PyObject *ptrcast(PyObject *ptr, char *type);
540 // Casts a pointer ptr to a new datatype given by the string type.
541 // type may be either the SWIG generated representation of a datatype
542 // or the C representation. For example :
543 //
544 // ptrcast(ptr,"double_p"); # Python representation
545 // ptrcast(ptr,"double *"); # C representation
546 //
547 // A new pointer value is returned. ptr may also be an integer
548 // value in which case the value will be used to set the pointer
549 // value. For example :
550 //
551 // a = ptrcast(0,"Vector_p");
552 //
553 // Will create a NULL pointer of type "Vector_p"
554 //
555 // The casting operation is sensitive to formatting. As a result,
556 // "double *" is different than "double*". As a result of thumb,
557 // there should always be exactly one space between the C datatype
558 // and any pointer specifiers (*).
559
560 PyObject *ptrvalue(PyObject *ptr, int index = 0, char *type = 0);
561 // Returns the value that a pointer is pointing to (ie. dereferencing).
562 // The type is automatically inferred by the pointer type--thus, an
563 // integer pointer will return an integer, a double will return a double,
564 // and so on. The index and type fields are optional parameters. When
565 // an index is specified, this function returns the value of ptr[index].
566 // This allows array access. When a type is specified, it overrides
567 // the given pointer type. Examples :
568 //
569 // ptrvalue(a) # Returns the value *a
570 // ptrvalue(a,10) # Returns the value a[10]
571 // ptrvalue(a,10,"double") # Returns a[10] assuming a is a double *
572
573 PyObject *ptrset(PyObject *ptr, PyObject *value, int index = 0, char *type = 0);
574 // Sets the value pointed to by a pointer. The type is automatically
575 // inferred from the pointer type so this function will work for
576 // integers, floats, doubles, etc... The index and type fields are
577 // optional. When an index is given, it provides array access. When
578 // type is specified, it overrides the given pointer type. Examples :
579 //
580 // ptrset(a,3) # Sets the value *a = 3
581 // ptrset(a,3,10) # Sets a[10] = 3
582 // ptrset(a,3,10,"int") # Sets a[10] = 3 assuming a is a int *
583
584 PyObject *ptrcreate(char *type, PyObject *value = 0, int nitems = 1);
585 // Creates a new object and returns a pointer to it. This function
586 // can be used to create various kinds of objects for use in C functions.
587 // type specifies the basic C datatype to create and value is an
588 // optional parameter that can be used to set the initial value of the
589 // object. nitems is an optional parameter that can be used to create
590 // an array. This function results in a memory allocation using
591 // malloc(). Examples :
592 //
593 // a = ptrcreate("double") # Create a new double, return pointer
594 // a = ptrcreate("int",7) # Create an integer, set value to 7
595 // a = ptrcreate("int",0,1000) # Create an integer array with initial
596 // # values all set to zero
597 //
598 // This function only recognizes a few common C datatypes as listed below :
599 //
600 // int, short, long, float, double, char, char *, void
601 //
602 // All other datatypes will result in an error. However, other
603 // datatypes can be created by using the ptrcast function. For
604 // example:
605 //
606 // a = ptrcast(ptrcreate("int",0,100),"unsigned int *")
607
608 PyObject *ptrfree(PyObject *ptr);
609 // Destroys the memory pointed to by ptr. This function calls free()
610 // and should only be used with objects created by ptrcreate(). Since
611 // this function calls free, it may work with other objects, but this
612 // is generally discouraged unless you absolutely know what you're
613 // doing.
614
615 PyObject *ptradd(PyObject *ptr, int offset);
616 // Adds a value to the current pointer value. For the C datatypes of
617 // int, short, long, float, double, and char, the offset value is the
618 // number of objects and works in exactly the same manner as in C. For
619 // example, the following code steps through the elements of an array
620 //
621 // a = ptrcreate("double",0,100); # Create an array double a[100]
622 // b = a;
623 // for i in range(0,100):
624 // ptrset(b,0.0025*i); # set *b = 0.0025*i
625 // b = ptradd(b,1); # b++ (go to next double)
626 //
627 // In this case, adding one to b goes to the next double.
628 //
629 // For all other datatypes (including all complex datatypes), the
630 // offset corresponds to bytes. This function does not perform any
631 // bounds checking and negative offsets are perfectly legal.
632
633 void ptrmap(char *type1, char *type2);
634 // This is a rarely used function that performs essentially the same
635 // operation as a C typedef. To manage datatypes at run-time, SWIG
636 // modules manage an internal symbol table of type mappings. This
637 // table keeps track of which types are equivalent to each other. The
638 // ptrmap() function provides a mechanism for scripts to add symbols
639 // to this table. For example :
640 //
641 // ptrmap("double_p","Real_p");
642 //
643 // would make the types "doublePtr" and "RealPtr" equivalent to each
644 // other. Pointers of either type could now be used interchangably.
645 //
646 // Normally this function is not needed, but it can be used to
647 // circumvent SWIG's normal type-checking behavior or to work around
648 // weird type-handling problems.
649
650
651
652