added the . See lextype.i example.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@6370 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
e88bf78833
commit
d35c5a7a32
2 changed files with 64 additions and 8 deletions
49
SWIG/Examples/test-suite/lextype.i
Normal file
49
SWIG/Examples/test-suite/lextype.i
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
This module tests whether SWIG sets the '$lextype' variable
|
||||
correctly. This variable maintains the literal base name of the
|
||||
type in the wrapper code - it's therefore usually the same
|
||||
as '$basetype', but NOT ALWAYS.
|
||||
|
||||
In the example below, the typemap definitions are written
|
||||
for any type of 'Animal', but are parameterized through
|
||||
preprocessor definitions. So when wrapping functions which
|
||||
explicitly reference Giraffes, the wrapper code can
|
||||
behave appropriately for that particular species.
|
||||
|
||||
For this to work correctly however, it is critical that
|
||||
there is a variable which strictly preserves the name
|
||||
of the type. '$lextype' doesn't currently do this -
|
||||
it sometimes contains 'Giraffe' and sometimes (specifically
|
||||
the case of arrays) contains 'Animal'. Since existing
|
||||
code may rely on that behaviour, we create a new variable
|
||||
'$lextype' which does what we need.
|
||||
|
||||
There is no need for any runtime test here, since if the
|
||||
code is not functioning properly it will fail to compile.
|
||||
*/
|
||||
|
||||
%module lextype
|
||||
|
||||
%typemap(in) Animal ()
|
||||
{
|
||||
void *space_needed = malloc(HEIGHT_$1_lextype * WIDTH_$1_lextype);
|
||||
}
|
||||
|
||||
%typemap(in) Animal[2] ()
|
||||
{
|
||||
void *space_needed = malloc(2 * HEIGHT_$1_lextype * WIDTH_$1_lextype);
|
||||
}
|
||||
|
||||
%inline %{
|
||||
|
||||
#define HEIGHT_Giraffe 100
|
||||
#define WIDTH_Giraffe 5
|
||||
|
||||
typedef void * Animal;
|
||||
typedef Animal Giraffe;
|
||||
|
||||
void eat(Giraffe g);
|
||||
void drink(Giraffe *g);
|
||||
Giraffe mate(Giraffe g[2]);
|
||||
|
||||
%}
|
||||
|
|
@ -703,7 +703,7 @@ int check_locals(ParmList *p, const char *s) {
|
|||
}
|
||||
|
||||
static
|
||||
void typemap_replace_vars(String *s, ParmList *locals, SwigType *type, String *pname, String *lname, int index)
|
||||
void typemap_replace_vars(String *s, ParmList *locals, SwigType *type, SwigType *rtype, String *pname, String *lname, int index)
|
||||
{
|
||||
char var[512];
|
||||
char *varname;
|
||||
|
|
@ -768,7 +768,7 @@ void typemap_replace_vars(String *s, ParmList *locals, SwigType *type, String *p
|
|||
|
||||
/* Type-related stuff */
|
||||
{
|
||||
SwigType *star_type, *amp_type, *base_type;
|
||||
SwigType *star_type, *amp_type, *base_type, *lex_type;
|
||||
SwigType *ltype, *star_ltype, *amp_ltype;
|
||||
String *mangle, *star_mangle, *amp_mangle, *base_mangle, *base_name;
|
||||
String *descriptor, *star_descriptor, *amp_descriptor;
|
||||
|
|
@ -967,6 +967,13 @@ void typemap_replace_vars(String *s, ParmList *locals, SwigType *type, String *p
|
|||
Delete(base_mangle);
|
||||
Delete(base_type);
|
||||
Delete(base_name);
|
||||
|
||||
lex_type = SwigType_base(rtype);
|
||||
if (index == 1)
|
||||
Replace(s,"$lextype", lex_type, DOH_REPLACE_ANY);
|
||||
strcpy(varname,"lextype");
|
||||
Replace(s,var,lex_type,DOH_REPLACE_ANY);
|
||||
Delete(lex_type);
|
||||
}
|
||||
|
||||
/* Replace any $n. with (&n)-> */
|
||||
|
|
@ -1067,9 +1074,9 @@ String *Swig_typemap_lookup(const String_or_char *op, SwigType *type, String_or_
|
|||
|
||||
/* This is wrong. It replaces locals in place. Need to fix this */
|
||||
if (mtype && SwigType_isarray(mtype)) {
|
||||
typemap_replace_vars(s,locals,mtype,pname,lname,1);
|
||||
typemap_replace_vars(s,locals,mtype,type,pname,lname,1);
|
||||
} else {
|
||||
typemap_replace_vars(s,locals,type,pname,lname,1);
|
||||
typemap_replace_vars(s,locals,type,type,pname,lname,1);
|
||||
}
|
||||
|
||||
if (locals && f) {
|
||||
|
|
@ -1156,9 +1163,9 @@ String *Swig_typemap_lookup_new(const String_or_char *op, Node *node, const Stri
|
|||
}
|
||||
|
||||
if (mtype && SwigType_isarray(mtype)) {
|
||||
typemap_replace_vars(s,locals,mtype,pname,(char *) lname,1);
|
||||
typemap_replace_vars(s,locals,mtype,type,pname,(char *) lname,1);
|
||||
} else {
|
||||
typemap_replace_vars(s,locals,type,pname,(char *) lname,1);
|
||||
typemap_replace_vars(s,locals,type,type,pname,(char *) lname,1);
|
||||
}
|
||||
|
||||
if (locals && f) {
|
||||
|
|
@ -1352,10 +1359,10 @@ Swig_typemap_attach_parms(const String_or_char *op, ParmList *parms, Wrapper *f)
|
|||
mtype = Getattr(p,"tmap:match");
|
||||
|
||||
if (mtype) {
|
||||
typemap_replace_vars(s,locals, mtype,pname,lname,i+1);
|
||||
typemap_replace_vars(s,locals,mtype,type,pname,lname,i+1);
|
||||
Delattr(p,"tmap:match");
|
||||
} else {
|
||||
typemap_replace_vars(s,locals, type,pname,lname,i+1);
|
||||
typemap_replace_vars(s,locals,type,type,pname,lname,i+1);
|
||||
}
|
||||
|
||||
if (checkAttribute(tm,"type","SWIGTYPE")) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue