Updated Guile runtime code.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@717 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Matthias Köppe 2000-08-29 17:30:50 +00:00
commit 597c3f8ebe
3 changed files with 86 additions and 101 deletions

View file

@ -7,12 +7,17 @@
/* SWIG pointer structure */ /* SWIG pointer structure */
struct SwigCast {
unsigned short type; /* Index into SwigPtrTbl */
void *(*cast)(void *); /* Pointer casting function */
struct SwigCast *next; /* Linked list pointer */
};
struct SwigPtrType { struct SwigPtrType {
char *name; /* Datatype name */ char *name; /* Datatype name */
char *prettyname; /* Pretty datatype name */ char *prettyname; /* Pretty datatype name */
void *(*cast)(void *); /* Pointer casting function */
unsigned short tag; /* Index in SwigPtrTable */ unsigned short tag; /* Index in SwigPtrTable */
struct SwigPtrType *next; /* Linked list pointer */ struct SwigCast *cast; /* List of compatible types */
}; };
/* Some variables */ /* Some variables */
@ -39,21 +44,18 @@ swigsort (const void *data1, const void *data2)
} }
/* Register a new datatype with the type-checker */ /* Register a new datatype with the type-checker */
SWIGSTATIC size_t
SWIGSTATIC void SWIG_RegisterType (char *type, char *prettyname)
SWIG_RegisterMapping (char *origtype, char *newtype, void *(*cast)(void *))
{ {
int i; int i;
SwigPtrType *t = 0,*t1;
/* Allocate the pointer table if necessary */ /* Allocate the pointer table if necessary */
if (!SwigPtrList) { if (!SwigPtrList) {
SwigPtrList = (SwigPtrType *) malloc(SwigPtrMax*sizeof(SwigPtrType)); SwigPtrList = (SwigPtrType *) malloc(SwigPtrMax*sizeof(SwigPtrType));
SwigPtrTbl = (size_t *) malloc(SwigPtrMax*sizeof(size_t)); SwigPtrTbl = (size_t *) malloc(SwigPtrMax*sizeof(size_t));
SwigPtrN = 0; SwigPtrN = 0;
} }
/* Grow the table */ /* Grow the table if necessary */
if (SwigPtrN >= SwigPtrMax) { if (SwigPtrN >= SwigPtrMax) {
SwigPtrMax = 2*SwigPtrMax; SwigPtrMax = 2*SwigPtrMax;
SwigPtrList = (SwigPtrType *) realloc((char *) SwigPtrList, SwigPtrList = (SwigPtrType *) realloc((char *) SwigPtrList,
@ -61,46 +63,53 @@ SWIG_RegisterMapping (char *origtype, char *newtype, void *(*cast)(void *))
SwigPtrTbl = (size_t *) realloc((char *) SwigPtrTbl, SwigPtrTbl = (size_t *) realloc((char *) SwigPtrTbl,
SwigPtrMax*sizeof(size_t)); SwigPtrMax*sizeof(size_t));
} }
/* Look up type */
for (i = 0; i < SwigPtrN; i++) for (i = 0; i < SwigPtrN; i++)
if (strcmp(SwigPtrList[i].name,origtype) == 0) { if (strcmp(SwigPtrList[i].name,type) == 0) {
t = &SwigPtrList[i]; if (prettyname!=NULL)
break; SwigPtrList[i].prettyname = prettyname;
return i;
} }
if (!t) { {
struct SwigPtrType *t;
size_t tag;
#if 0 #if 0
fprintf(stderr, "New type: %s\n", origtype); fprintf(stderr, "New type: %s\n", type);
#endif #endif
SwigPtrTbl[SwigPtrN] = SwigPtrN; tag = SwigPtrTbl[SwigPtrN] = SwigPtrN;
t = &SwigPtrList[SwigPtrN]; t = &SwigPtrList[tag];
t->name = origtype; t->name = type;
t->prettyname = NULL; t->prettyname = prettyname;
t->cast = 0;
t->next = 0;
t->tag = SwigPtrN; t->tag = SwigPtrN;
t->cast = NULL;
SwigPtrN++; SwigPtrN++;
SwigPtrSort = 0; SwigPtrSort = 0;
return tag;
} }
}
/* Register two data types and their mapping with the type checker. */
SWIGSTATIC void
SWIG_RegisterMapping (char *origtype, char *newtype, void *(*cast)(void *))
{
int i;
size_t t = SWIG_RegisterType(origtype, NULL);
if (newtype!=NULL) { if (newtype!=NULL) {
/* Check for existing entry */ size_t t1 = SWIG_RegisterType(newtype, NULL);
struct SwigCast *c;
while (t->next) { /* Check for existing cast */
if ((strcmp(t->name,newtype) == 0)) { for (c = SwigPtrList[t].cast; c && c->type!=t1; c=c->next) /* nothing */;
if (cast) t->cast = cast; if (c) {
return; if (cast) c->cast = cast;
} }
t = t->next; else {
c = (struct SwigCast *) malloc(sizeof(struct SwigCast));
c->type = t1;
c->cast = cast;
c->next = SwigPtrList[t].cast;
SwigPtrList[t].cast = c;
} }
/* FIXME: List of compatible types subject to change */
/* Now place entry (in sorted order) */
t1 = (SwigPtrType *) malloc(sizeof(SwigPtrType));
t1->name = newtype;
t1->prettyname = NULL;
t1->cast = cast;
t1->next = 0;
t->next = t1;
} }
} }
@ -136,52 +145,33 @@ SWIG_GetPtrType (const char *_t)
else return NULL; else return NULL;
} }
/* Cast a pointer if possible */ /* Cast a pointer if possible; returns 1 if successful */
static char * static int
SWIG_Cast_Str (void *source, char *source_type, void **ptr, char *_t) SWIG_Cast (void *source, size_t source_type,
void **ptr, size_t dest_type)
{ {
char temp_type[256]; if (dest_type != source_type) {
char *name; /* We have a type mismatch. Will have to look through our type
int i, len; mapping table to figure out whether or not we can accept this
SwigPtrType *sp,*tp; datatype. */
int start, end; struct SwigCast *c;
if (_t) { for (c = SwigPtrList[source_type].cast;
if (strcmp(_t,source_type)) { c && c->type!=dest_type; c = c->next) /* nothing */;
if (!SwigPtrSort) SWIG_SortTable(); if (c) {
/* Get pointer value. */
/* We have a type mismatch. Will have to look through our type if (c->cast) *ptr = (*(c->cast))(source);
mapping table to figure out whether or not we can accept this else *ptr = source;
datatype. */ return 1;
sp = SWIG_GetPtrType(_t);
if (sp!=NULL) {
name = sp->name;
tp = sp->next;
/* Try to find entry for our given datatype. */
while(tp) {
if (strcmp(source_type, tp->name) == 0) {
/* 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 { /* Didn't find any sort of match for this data.
/* No type specified. Good luck! */ Get the pointer value and return false. */
*ptr = source; *ptr = source;
return (char *) 0; return 0;
} else {
/* Found a match on the first try. Return pointer value. */
*ptr = source;
return 1;
} }
} }
@ -193,12 +183,13 @@ SWIGSTATIC SCM
SWIG_Guile_MakePtr (void *ptr, _swig_type_info *type) SWIG_Guile_MakePtr (void *ptr, _swig_type_info *type)
{ {
if (ptr==NULL) return SCM_EOL; if (ptr==NULL) return SCM_EOL;
SCM_RETURN_NEWSMOB((((unsigned long)type->ptrtype->tag << 16) | swig_tag), SCM_RETURN_NEWSMOB((((unsigned long)type->tag << 16) | swig_tag),
ptr); ptr);
} }
static int /* Return 0 if successful. */
SWIG_Guile_GetPtr_Str (SCM s, void **result, char *typestring) SWIGSTATIC int
SWIG_Guile_GetPtr(SCM s, void **result, _swig_type_info *type)
{ {
if (SCM_NULLP(s)) { if (SCM_NULLP(s)) {
*result = NULL; *result = NULL;
@ -206,22 +197,18 @@ SWIG_Guile_GetPtr_Str (SCM s, void **result, char *typestring)
} }
else if (SCM_NIMP(s) else if (SCM_NIMP(s)
&& (unsigned long) SCM_TYP16(s) == swig_tag) { && (unsigned long) SCM_TYP16(s) == swig_tag) {
if (SWIG_Cast_Str((void *) SCM_CDR(s), if (type)
SwigPtrList[SCM_CAR(s) >> 16].name, return !SWIG_Cast((void *) SCM_CDR(s),
result, typestring) == NULL) SCM_CAR(s) >> 16,
result, type->tag);
else {
*result = (void *) SCM_CDR(s);
return 0; return 0;
}
} }
return 1; return 1;
} }
/* Return 0 if successful. */
SWIGSTATIC int
SWIG_Guile_GetPtr(SCM s, void **result, _swig_type_info *type)
{
/* FIXME: Use native typechecking w/o string-compares */
return SWIG_Guile_GetPtr_Str(s, result, type->name);
}
/* Init */ /* Init */
static int static int
@ -264,10 +251,8 @@ void SWIG_Guile_RegisterTypes(_swig_type_info **table)
for (; *table; table++) { for (; *table; table++) {
_swig_type_info *type = *table; _swig_type_info *type = *table;
char *origname = type->name; char *origname = type->name;
/* Register datatype itself */ /* Register datatype itself and store pointer back */
SWIG_RegisterMapping(origname, 0, 0); type->tag = SWIG_RegisterType(origname, NULL /* FIXME: prettyname */);
/* Store pointer to actual type */
type->ptrtype = SWIG_GetPtrType(origname);
/* Register compatible types */ /* Register compatible types */
for (type++; type->name; type++) for (type++; type->name; type++)
SWIG_RegisterMapping(origname, type->name, type->converter); SWIG_RegisterMapping(origname, type->name, type->converter);

View file

@ -50,7 +50,7 @@ typedef struct SwigPtrType SwigPtrType;
typedef struct _swig_type_info { typedef struct _swig_type_info {
char *name; char *name;
void *(*converter)(void *); void *(*converter)(void *);
SwigPtrType *ptrtype; size_t tag;
} _swig_type_info; } _swig_type_info;
#define _swig_types_initial _swig_types #define _swig_types_initial _swig_types

View file

@ -13,10 +13,10 @@
%typemap (guile, indoc) C_NAME "($arg <SCM_NAME>)"; \ %typemap (guile, indoc) C_NAME "($arg <SCM_NAME>)"; \
%typemap (guile, outdoc) C_NAME "<SCM_NAME>"; \ %typemap (guile, outdoc) C_NAME "<SCM_NAME>"; \
%typemap (guile, in) C_NAME *INPUT (C_NAME temp) \ %typemap (guile, in) C_NAME *INPUT (C_NAME temp) \
{ temp = (C_NAME) C_TO_SCM($source); $target = &temp; } \ "temp = (C_NAME) C_TO_SCM($source); $target = &temp;" \
%typemap (guile, indoc) C_NAME *INPUT "($arg <SCM_NAME>)"; \ %typemap (guile, indoc) C_NAME *INPUT "($arg <SCM_NAME>)"; \
%typemap (guile, ignore) C_NAME *OUTPUT (C_NAME temp) \ %typemap (guile, ignore) C_NAME *OUTPUT (C_NAME temp) \
{ $target = &temp; } \ "$target = &temp;" \
%typemap (guile, argout) C_NAME *OUTPUT \ %typemap (guile, argout) C_NAME *OUTPUT \
"GUILE_APPEND_RESULT(C_TO_SCM(*$target));"; \ "GUILE_APPEND_RESULT(C_TO_SCM(*$target));"; \
%typemap (guile, argoutdoc) C_NAME *OUTPUT "($arg <SCM_NAME>)"; \ %typemap (guile, argoutdoc) C_NAME *OUTPUT "($arg <SCM_NAME>)"; \