{Changes contributed by Matthias Koeppe}

(SwigPtrType): Add member `tag' to this struct.

(SwigPtrTable): Add one level of indirection.

(swigsort, swigcmp, SWIG_RegisterMapping): Accomodate new `SwigPtrTable'.

(SWIG_SortTable, SWIG_GetPtrType, SWIG_Cast_Str): New funcs.

(SWIG_GetPtr): Rewrite.

(swig_tag): New static var, #ifdef `SWIGGUILE'.

(SWIG_Guile_MakePtr, SWIG_Guile_GetPtr, SWIG_Guile_MakePtr_Str,
SWIG_Guile_GetPtr_Str): New static funcs, #ifdef `SWIGGUILE'.


git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@354 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Thien-Thi Nguyen 2000-04-03 07:17:36 +00:00
commit d9087e62c6

View file

@ -54,6 +54,7 @@ typedef struct SwigPtrType {
char *name; /* Datatype name */
int len; /* Length (used for optimization) */
void *(*cast)(void *); /* Pointer casting function */
unsigned short tag; /* Index in SwigPtrTable */
struct SwigPtrType *next; /* Linked list pointer */
} SwigPtrType;
@ -75,7 +76,7 @@ static int SwigPtrSort = 0; /* Status flag indicating sort
static int SwigStart[256]; /* Starting positions of types */
/* Pointer table */
static SwigPtrType *SwigPtrTable = 0; /* Table containing pointer equivalences */
static SwigPtrType **SwigPtrTable = 0; /* Table containing pointer equivalences */
/* Cached values */
@ -87,16 +88,16 @@ static int SwigLastCache = 0;
/* Sort comparison function */
static int swigsort(const void *data1, const void *data2) {
SwigPtrType *d1 = (SwigPtrType *) data1;
SwigPtrType *d2 = (SwigPtrType *) data2;
return strcmp(d1->name,d2->name);
SwigPtrType **d1 = (SwigPtrType **) data1;
SwigPtrType **d2 = (SwigPtrType **) data2;
return strcmp((*d1)->name, (*d2)->name);
}
/* Binary Search function */
static int swigcmp(const void *key, const void *data) {
char *k = (char *) key;
SwigPtrType *d = (SwigPtrType *) data;
return strncmp(k,d->name,d->len);
SwigPtrType **d = (SwigPtrType **) data;
return strncmp(k,(*d)->name,(*d)->len);
}
/* Register a new datatype with the type-checker */
@ -110,21 +111,22 @@ void SWIG_RegisterMapping(char *origtype, char *newtype, void *(*cast)(void *))
/* Allocate the pointer table if necessary */
if (!SwigPtrTable) {
SwigPtrTable = (SwigPtrType *) malloc(SwigPtrMax*sizeof(SwigPtrType));
SwigPtrTable = (SwigPtrType **) malloc(SwigPtrMax*sizeof(SwigPtrType *));
SwigPtrN = 0;
}
/* Grow the table */
if (SwigPtrN >= SwigPtrMax) {
SwigPtrMax = 2*SwigPtrMax;
SwigPtrTable = (SwigPtrType *) realloc((char *) SwigPtrTable,SwigPtrMax*sizeof(SwigPtrType));
SwigPtrTable = (SwigPtrType **) realloc((char *) SwigPtrTable,
SwigPtrMax*sizeof(SwigPtrType *));
}
for (i = 0; i < SwigPtrN; i++)
if (strcmp(SwigPtrTable[i].name,origtype) == 0) {
t = &SwigPtrTable[i];
if (strcmp(SwigPtrTable[i]->name,origtype) == 0) {
t = SwigPtrTable[i];
break;
}
if (!t) {
t = &SwigPtrTable[SwigPtrN];
t = SwigPtrTable[SwigPtrN] = malloc(sizeof(SwigPtrType));
t->name = origtype;
t->len = strlen(t->name);
t->cast = 0;
@ -184,18 +186,138 @@ void SWIG_MakePtr(char *_c, const void *_ptr, char *type) {
#define _swig_make_hex SWIG_MakePtr
/* Function for getting a pointer value */
/* Sort table and make cache invalid */
SWIGSTATIC
char *SWIG_GetPtr(char *_c, void **ptr, char *_t)
SWIGSTATIC
void SWIG_SortTable()
{
int i;
qsort((void *) SwigPtrTable, SwigPtrN, sizeof(SwigPtrType *), swigsort);
for (i = 0; i < 256; i++) {
SwigStart[i] = SwigPtrN;
}
for (i = SwigPtrN-1; i >= 0; i--) {
SwigStart[(int) (SwigPtrTable[i]->name[1])] = i;
}
for (i = 255; i >= 1; i--) {
if (SwigStart[i-1] > SwigStart[i])
SwigStart[i-1] = SwigStart[i];
}
/* Invalidate cache */
for (i = 0; i < SWIG_CACHESIZE; i++)
SwigCache[i].stat = 0;
/* Put tags */
for (i = 0; i < SwigPtrN; i++)
SwigPtrTable[i]->tag = i;
/* Indicate that everything is sorted */
SwigPtrSort = 1;
}
/* Look up pointer-type entry in table */
SWIGSTATIC
SwigPtrType *SWIG_GetPtrType(char *_t)
{
int start, end;
SwigPtrType **sp;
if (!SwigPtrSort) SWIG_SortTable();
start = SwigStart[(int) _t[1]];
end = SwigStart[(int) _t[1]+1];
sp = &SwigPtrTable[start];
/* This can be improved by using binary search instead of linear search. */
while (start < end) {
if (swigcmp(_t,sp) == 0) return *sp;
sp++;
start++;
}
return NULL;
}
/* Cast a pointer if possible */
SWIGSTATIC
char *SWIG_Cast_Str(void *source, char *source_type, void **ptr, char *_t)
{
unsigned long _p;
char temp_type[256];
char *name;
int i, len;
SwigPtrType *sp,*tp;
SwigCacheType *cache;
int start, end;
if (_t) {
if (strcmp(_t,source_type)) {
if (!SwigPtrSort) SWIG_SortTable();
/* First check cache for matches. Uses last cache value as starting point */
cache = &SwigCache[SwigLastCache];
for (i = 0; i < SWIG_CACHESIZE; i++) {
if (cache->stat) {
if (strcmp(_t,cache->name) == 0) {
if (strcmp(source_type,cache->mapped) == 0) {
cache->stat++;
*ptr = source;
if (cache->tp->cast) *ptr = (*(cache->tp->cast))(*ptr);
return (char *) 0;
}
}
}
SwigLastCache = (SwigLastCache+1) & SWIG_CACHEMASK;
if (!SwigLastCache) cache = SwigCache;
else cache++;
}
/* We have a type mismatch. Will have to look through our type
mapping table to figure out whether or not we can accept this datatype */
sp = SWIG_GetPtrType(_t);
if (sp!=NULL) {
name = sp->name;
len = sp->len;
tp = sp->next;
/* Try to find entry for our given datatype */
while(tp) {
/* I don't understand this code */
if (tp->len >= 255) {
return source_type;
}
strcpy(temp_type,tp->name);
strncat(temp_type,_t+len,255-tp->len);
if (strcmp(source_type,temp_type) == 0) {
strcpy(SwigCache[SwigCacheIndex].mapped,source_type);
strcpy(SwigCache[SwigCacheIndex].name,_t);
SwigCache[SwigCacheIndex].stat = 1;
SwigCache[SwigCacheIndex].tp = tp;
SwigCacheIndex = SwigCacheIndex & SWIG_CACHEMASK;
/* Get pointer value */
*ptr = source;
if (tp->cast) *ptr = (*(tp->cast))(*ptr);
return (char *) 0;
}
tp = tp->next;
}
}
/* Didn't find any sort of match for this data.
Get the pointer value and return the received type */
*ptr = source;
return source_type;
} else {
/* Found a match on the first try. Return pointer value */
*ptr = source;
return (char *) 0;
}
} else {
/* No type specified. Good luck */
*ptr = source;
return (char *) 0;
}
}
/* Function for getting a pointer value */
SWIGSTATIC
char *SWIG_GetPtr(char *_c, void **ptr, char *_t)
{
unsigned long _p;
_p = 0;
/* Pointer values must start with leading underscore */
@ -211,101 +333,7 @@ char *SWIG_GetPtr(char *_c, void **ptr, char *_t)
break;
_c++;
}
if (_t) {
if (strcmp(_t,_c)) {
if (!SwigPtrSort) {
qsort((void *) SwigPtrTable, SwigPtrN, sizeof(SwigPtrType), swigsort);
for (i = 0; i < 256; i++) {
SwigStart[i] = SwigPtrN;
}
for (i = SwigPtrN-1; i >= 0; i--) {
SwigStart[(int) (SwigPtrTable[i].name[1])] = i;
}
for (i = 255; i >= 1; i--) {
if (SwigStart[i-1] > SwigStart[i])
SwigStart[i-1] = SwigStart[i];
}
SwigPtrSort = 1;
for (i = 0; i < SWIG_CACHESIZE; i++)
SwigCache[i].stat = 0;
}
/* First check cache for matches. Uses last cache value as starting point */
cache = &SwigCache[SwigLastCache];
for (i = 0; i < SWIG_CACHESIZE; i++) {
if (cache->stat) {
if (strcmp(_t,cache->name) == 0) {
if (strcmp(_c,cache->mapped) == 0) {
cache->stat++;
*ptr = (void *) _p;
if (cache->tp->cast) *ptr = (*(cache->tp->cast))(*ptr);
return (char *) 0;
}
}
}
SwigLastCache = (SwigLastCache+1) & SWIG_CACHEMASK;
if (!SwigLastCache) cache = SwigCache;
else cache++;
}
/* We have a type mismatch. Will have to look through our type
mapping table to figure out whether or not we can accept this datatype */
start = SwigStart[(int) _t[1]];
end = SwigStart[(int) _t[1]+1];
sp = &SwigPtrTable[start];
while (start < end) {
if (swigcmp(_t,sp) == 0) break;
sp++;
start++;
}
if (start > end) sp = 0;
/* Try to find a match for this */
while (start <= end) {
if (swigcmp(_t,sp) == 0) {
name = sp->name;
len = sp->len;
tp = sp->next;
/* Try to find entry for our given datatype */
while(tp) {
if (tp->len >= 255) {
return _c;
}
strcpy(temp_type,tp->name);
strncat(temp_type,_t+len,255-tp->len);
if (strcmp(_c,temp_type) == 0) {
strcpy(SwigCache[SwigCacheIndex].mapped,_c);
strcpy(SwigCache[SwigCacheIndex].name,_t);
SwigCache[SwigCacheIndex].stat = 1;
SwigCache[SwigCacheIndex].tp = tp;
SwigCacheIndex = SwigCacheIndex & SWIG_CACHEMASK;
/* Get pointer value */
*ptr = (void *) _p;
if (tp->cast) *ptr = (*(tp->cast))(*ptr);
return (char *) 0;
}
tp = tp->next;
}
}
sp++;
start++;
}
/* Didn't find any sort of match for this data.
Get the pointer value and return the received type */
*ptr = (void *) _p;
return _c;
} else {
/* Found a match on the first try. Return pointer value */
*ptr = (void *) _p;
return (char *) 0;
}
} else {
/* No type specified. Good luck */
*ptr = (void *) _p;
return (char *) 0;
}
return SWIG_Cast_Str((void *)_p, _c, ptr, _t);
} else {
if (strcmp (_c, "NULL") == 0) {
*ptr = (void *) 0;
@ -316,6 +344,53 @@ char *SWIG_GetPtr(char *_c, void **ptr, char *_t)
}
}
#ifdef SWIGGUILE
SWIGSTATIC int swig_tag;
SWIGSTATIC
SCM SWIG_Guile_MakePtr(void *ptr, SwigPtrType *type)
{
SCM_RETURN_NEWSMOB((((unsigned long)type->tag << 16) | swig_tag),
ptr);
}
/* Return 0 if successful. */
SWIGSTATIC
int SWIG_Guile_GetPtr(SCM s, void **result, SwigPtrType *type)
{
if (SCM_NIMP(s)
&& SCM_TYP16(s) == swig_tag) {
if (SWIG_Cast_Str((void *) SCM_CDR(s),
SwigPtrTable[SCM_CAR(s) >> 16]->name,
result, type->name) == NULL)
return 0;
}
return 1;
}
/* TRANSITIONAL */
SWIGSTATIC
SCM SWIG_Guile_MakePtr_Str(void *ptr, char *typestring)
{
return SWIG_Guile_MakePtr(ptr, SWIG_GetPtrType(typestring));
}
SWIGSTATIC
int SWIG_Guile_GetPtr_Str(SCM s, void **result, char *typestring)
{
if (SCM_NIMP(s)
&& SCM_TYP16(s) == swig_tag) {
if (SWIG_Cast_Str((void *) SCM_CDR(s),
SwigPtrTable[SCM_CAR(s) >> 16]->name,
result, typestring) == NULL)
return 0;
}
return 1;
}
#endif
/* Compatibility mode */
#define _swig_get_hex SWIG_GetPtr
@ -323,4 +398,3 @@ char *SWIG_GetPtr(char *_c, void **ptr, char *_t)
#ifdef __cplusplus
}
#endif