]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/src/libptr.c
5fad186f890e6295aa63b322836e38f473941aa7
1 /*****************************************************************************
6 * This file contains supporting code for the SWIG run-time type checking
7 * mechanism. The following functions are available :
9 * SWIG_RegisterMapping(char *origtype, char *newtype, void *(*cast)(void *));
11 * Registers a new type-mapping with the type-checker. origtype is the
12 * original datatype and newtype is an equivalent type. cast is optional
13 * pointer to a function to cast pointer values between types (this
14 * is typically used to cast pointers from derived classes to base classes in C++)
16 * SWIG_MakePtr(char *buffer, void *ptr, char *typestring);
18 * Makes a pointer string from a pointer and typestring. The result is returned
19 * in buffer which is assumed to hold enough space for the result.
21 * char * SWIG_GetPtr(char *buffer, void **ptr, char *type)
23 * Gets a pointer value from a string. If there is a type-mismatch, returns
24 * a character string to the received type. On success, returns NULL.
27 * You can remap these functions by making a file called "swigptr.swg" in
28 * your the same directory as the interface file you are wrapping.
30 * These functions are normally declared static, but this file can be
31 * can be used in a multi-module environment by redefining the symbol
33 *****************************************************************************/
39 #define SWIGSTATIC extern "C"
46 #define SWIGSTATIC static
50 /* SWIG pointer structure */
52 typedef struct SwigPtrType
{
53 char *name
; /* Datatype name */
54 int len
; /* Length (used for optimization) */
55 void *(*cast
)(void *); /* Pointer casting function */
56 struct SwigPtrType
*next
; /* Linked list pointer */
59 /* Pointer cache structure */
62 int stat
; /* Status (valid) bit */
63 SwigPtrType
*tp
; /* Pointer to type structure */
64 char name
[256]; /* Given datatype name */
65 char mapped
[256]; /* Equivalent name */
70 static int SwigPtrMax
= 64; /* Max entries that can be currently held */
71 /* This value may be adjusted dynamically */
72 static int SwigPtrN
= 0; /* Current number of entries */
73 static int SwigPtrSort
= 0; /* Status flag indicating sort */
74 static int SwigStart
[256]; /* Starting positions of types */
77 static SwigPtrType
*SwigPtrTable
= 0; /* Table containing pointer equivalences */
81 #define SWIG_CACHESIZE 8
82 #define SWIG_CACHEMASK 0x7
83 static SwigCacheType SwigCache
[SWIG_CACHESIZE
];
84 static int SwigCacheIndex
= 0;
85 static int SwigLastCache
= 0;
87 /* Sort comparison function */
88 static int swigsort(const void *data1
, const void *data2
) {
89 SwigPtrType
*d1
= (SwigPtrType
*) data1
;
90 SwigPtrType
*d2
= (SwigPtrType
*) data2
;
91 return strcmp(d1
->name
,d2
->name
);
94 /* Binary Search function */
95 static int swigcmp(const void *key
, const void *data
) {
96 char *k
= (char *) key
;
97 SwigPtrType
*d
= (SwigPtrType
*) data
;
98 return strncmp(k
,d
->name
,d
->len
);
101 /* Register a new datatype with the type-checker */
104 void SWIG_RegisterMapping(char *origtype
, char *newtype
, void *(*cast
)(void *)) {
107 SwigPtrType
*t
= 0,*t1
;
109 /* Allocate the pointer table if necessary */
112 SwigPtrTable
= (SwigPtrType
*) malloc(SwigPtrMax
*sizeof(SwigPtrType
));
116 if (SwigPtrN
>= SwigPtrMax
) {
117 SwigPtrMax
= 2*SwigPtrMax
;
118 SwigPtrTable
= (SwigPtrType
*) realloc((char *) SwigPtrTable
,SwigPtrMax
*sizeof(SwigPtrType
));
120 for (i
= 0; i
< SwigPtrN
; i
++)
121 if (strcmp(SwigPtrTable
[i
].name
,origtype
) == 0) {
122 t
= &SwigPtrTable
[i
];
126 t
= &SwigPtrTable
[SwigPtrN
];
128 t
->len
= strlen(t
->name
);
134 /* Check for existing entry */
137 if ((strcmp(t
->name
,newtype
) == 0)) {
138 if (cast
) t
->cast
= cast
;
144 /* Now place entry (in sorted order) */
146 t1
= (SwigPtrType
*) malloc(sizeof(SwigPtrType
));
148 t1
->len
= strlen(t1
->name
);
155 /* Make a pointer value string */
158 void SWIG_MakePtr(char *_c
, const void *_ptr
, char *type
) {
159 static char _hex
[16] =
160 {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
161 'a', 'b', 'c', 'd', 'e', 'f'};
162 unsigned long _p
, _s
;
163 char _result
[20], *_r
; /* Note : a 64-bit hex number = 16 digits */
165 _p
= (unsigned long) _ptr
;
173 while (_r
>= _result
)
182 /* Define for backwards compatibility */
184 #define _swig_make_hex SWIG_MakePtr
186 /* Function for getting a pointer value */
189 char *SWIG_GetPtr(char *_c
, void **ptr
, char *_t
)
196 SwigCacheType
*cache
;
200 /* Pointer values must start with leading underscore */
203 /* Extract hex value from pointer */
205 if ((*_c
>= '0') && (*_c
<= '9'))
206 _p
= (_p
<< 4) + (*_c
- '0');
207 else if ((*_c
>= 'a') && (*_c
<= 'f'))
208 _p
= (_p
<< 4) + ((*_c
- 'a') + 10);
217 qsort((void *) SwigPtrTable
, SwigPtrN
, sizeof(SwigPtrType
), swigsort
);
218 for (i
= 0; i
< 256; i
++) {
219 SwigStart
[i
] = SwigPtrN
;
221 for (i
= SwigPtrN
-1; i
>= 0; i
--) {
222 SwigStart
[(int) (SwigPtrTable
[i
].name
[1])] = i
;
224 for (i
= 255; i
>= 1; i
--) {
225 if (SwigStart
[i
-1] > SwigStart
[i
])
226 SwigStart
[i
-1] = SwigStart
[i
];
229 for (i
= 0; i
< SWIG_CACHESIZE
; i
++)
230 SwigCache
[i
].stat
= 0;
233 /* First check cache for matches. Uses last cache value as starting point */
234 cache
= &SwigCache
[SwigLastCache
];
235 for (i
= 0; i
< SWIG_CACHESIZE
; i
++) {
237 if (strcmp(_t
,cache
->name
) == 0) {
238 if (strcmp(_c
,cache
->mapped
) == 0) {
241 if (cache
->tp
->cast
) *ptr
= (*(cache
->tp
->cast
))(*ptr
);
246 SwigLastCache
= (SwigLastCache
+1) & SWIG_CACHEMASK
;
247 if (!SwigLastCache
) cache
= SwigCache
;
250 /* We have a type mismatch. Will have to look through our type
251 mapping table to figure out whether or not we can accept this datatype */
253 start
= SwigStart
[(int) _t
[1]];
254 end
= SwigStart
[(int) _t
[1]+1];
255 sp
= &SwigPtrTable
[start
];
256 while (start
< end
) {
257 if (swigcmp(_t
,sp
) == 0) break;
261 if (start
>= end
) sp
= 0;
262 /* Try to find a match for this */
264 while (swigcmp(_t
,sp
) == 0) {
268 /* Try to find entry for our given datatype */
270 if (tp
->len
>= 255) {
273 strcpy(temp_type
,tp
->name
);
274 strncat(temp_type
,_t
+len
,255-tp
->len
);
275 if (strcmp(_c
,temp_type
) == 0) {
277 strcpy(SwigCache
[SwigCacheIndex
].mapped
,_c
);
278 strcpy(SwigCache
[SwigCacheIndex
].name
,_t
);
279 SwigCache
[SwigCacheIndex
].stat
= 1;
280 SwigCache
[SwigCacheIndex
].tp
= tp
;
281 SwigCacheIndex
= SwigCacheIndex
& SWIG_CACHEMASK
;
283 /* Get pointer value */
285 if (tp
->cast
) *ptr
= (*(tp
->cast
))(*ptr
);
291 /* Hmmm. Didn't find it this time */
294 /* Didn't find any sort of match for this data.
295 Get the pointer value and return the received type */
299 /* Found a match on the first try. Return pointer value */
304 /* No type specified. Good luck */
309 if (strcmp (_c
, "NULL") == 0) {
318 /* Compatibility mode */
320 #define _swig_get_hex SWIG_GetPtr