From bbcfa0b0892a3b81a0a34340ed25771fe2793546 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Thu, 30 Jul 2009 17:42:18 +0000 Subject: [PATCH] () for multi-argument typemaps git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@11481 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- .../csharp/special_variable_macros_runme.cs | 2 ++ .../java/special_variable_macros_runme.java | 2 ++ .../python/special_variable_macros_runme.py | 2 ++ Examples/test-suite/special_variable_macros.i | 26 ++++++++++++++++++- Source/Swig/typemap.c | 24 +++++++++++++---- 5 files changed, 50 insertions(+), 6 deletions(-) diff --git a/Examples/test-suite/csharp/special_variable_macros_runme.cs b/Examples/test-suite/csharp/special_variable_macros_runme.cs index abde0053b..1c0939a1f 100644 --- a/Examples/test-suite/csharp/special_variable_macros_runme.cs +++ b/Examples/test-suite/csharp/special_variable_macros_runme.cs @@ -12,6 +12,8 @@ public class runme { throw new Exception("test failed"); if (special_variable_macros.testMary(name) != "SWIGTYPE_p_NameWrap") throw new Exception("test failed"); + if (special_variable_macros.testJim(name) != "multiname num") + throw new Exception("test failed"); NewName newName = NewName.factory("factoryname"); name = newName.getStoredName(); } diff --git a/Examples/test-suite/java/special_variable_macros_runme.java b/Examples/test-suite/java/special_variable_macros_runme.java index df7aa8c84..dee629666 100644 --- a/Examples/test-suite/java/special_variable_macros_runme.java +++ b/Examples/test-suite/java/special_variable_macros_runme.java @@ -22,6 +22,8 @@ public class special_variable_macros_runme { throw new RuntimeException("test failed"); if (!special_variable_macros.testMary(name).equals("SWIGTYPE_p_NameWrap")) throw new RuntimeException("test failed"); + if (!special_variable_macros.testJim(name).equals("multiname num")) + throw new RuntimeException("test failed"); NewName newName = NewName.factory("factoryname"); name = newName.getStoredName(); } diff --git a/Examples/test-suite/python/special_variable_macros_runme.py b/Examples/test-suite/python/special_variable_macros_runme.py index f1969f704..a1d3308ed 100644 --- a/Examples/test-suite/python/special_variable_macros_runme.py +++ b/Examples/test-suite/python/special_variable_macros_runme.py @@ -9,4 +9,6 @@ if special_variable_macros.testJill(name) != "jilly": raise "test failed" if special_variable_macros.testMary(name) != "SWIGTYPE_p_NameWrap": raise "test failed" +if special_variable_macros.testJim(name) != "multiname num": + raise "test failed" diff --git a/Examples/test-suite/special_variable_macros.i b/Examples/test-suite/special_variable_macros.i index 06f5805dd..85adf7af1 100644 --- a/Examples/test-suite/special_variable_macros.i +++ b/Examples/test-suite/special_variable_macros.i @@ -83,8 +83,32 @@ const char * testMary(Name *mary) { ////////////////////////////////////////////////////////////////////////////////////// // Multi-arg typemap lookup -#warning TODO!!! +// One would never do something like this in reality, it just checks $typemap with multi-arg typemaps +%typemap(in) (Name *multiname, int num)($*1_type temp_name, $2_ltype temp_count) +%{ + /*%typemap(in) (Name *multiname, int num) start */ + temp_name = $*1_ltype("multiname num"); + temp_count = strlen(temp_name.getNamePtr()->getName()); + (void)$input; + $1 = temp_name.getNamePtr(); + $2 = temp_count + 100; + /*%typemap(in) (Name *multiname, int num) end */ +%} +%typemap(in) (Name *jim, int count) { +// %typemap(in) Name *jim start +$typemap(in, (Name *multiname, int num)) +// %typemap(in) Name *jim end +} + +%inline %{ +const char * testJim(Name *jim, int count) { + if (count != strlen(jim->getNamePtr()->getName()) + 100) + return "size check failed"; + else + return jim->getName(); +} +%} ////////////////////////////////////////////////////////////////////////////////////// // A real use case for $typemap diff --git a/Source/Swig/typemap.c b/Source/Swig/typemap.c index 3392fd64c..0fae2344e 100644 --- a/Source/Swig/typemap.c +++ b/Source/Swig/typemap.c @@ -17,7 +17,7 @@ char cvsroot_typemap_c[] = "$Id$"; #define SWIG_DEBUG #endif -static void replace_embedded_typemap(String *s, String *lname, Wrapper *f); +static void replace_embedded_typemap(String *s, ParmList *parm_sublist, Wrapper *f); /* ----------------------------------------------------------------------------- * Typemaps are stored in a collection of nested hash tables. Something like @@ -1344,7 +1344,13 @@ static String *Swig_typemap_lookup_impl(const_String_or_char_ptr tmap_method, No if (locals && f) { typemap_locals(s, locals, f, -1); } - replace_embedded_typemap(s, NewString(lname), f); + + { + ParmList *parm_sublist = NewParm(type, pname); + Setattr(parm_sublist, "lname", lname); + replace_embedded_typemap(s, parm_sublist, f); + Delete(parm_sublist); + } Replace(s, "$name", pname, DOH_REPLACE_ANY); @@ -1643,7 +1649,7 @@ void Swig_typemap_attach_parms(const_String_or_char_ptr tmap_method, ParmList *p typemap_locals(s, locals, f, argnum); } - replace_embedded_typemap(s, Getattr(firstp, "lname"), f); + replace_embedded_typemap(s, firstp, f); /* Replace the argument number */ sprintf(temp, "%d", argnum); @@ -1759,7 +1765,7 @@ static List *split_embedded_typemap(String *s) { * $typemap(in, (Foo a, int b)) # multi-argument typemap matching %typemap(in) (Foo a, int b) {...} * ----------------------------------------------------------------------------- */ -static void replace_embedded_typemap(String *s, String *lname, Wrapper *f) { +static void replace_embedded_typemap(String *s, ParmList *parm_sublist, Wrapper *f) { char *start = 0; while ((start = strstr(Char(s), "$TYPEMAP("))) { /* note $typemap capitalisation to $TYPEMAP hack */ @@ -1805,11 +1811,19 @@ static void replace_embedded_typemap(String *s, String *lname, Wrapper *f) { * typemap matching, so split these parameters apart */ to_match_parms = Swig_cparse_parms(Getitem(l, 1)); if (to_match_parms) { - Parm *p = to_match_parms;; + Parm *p = to_match_parms; + Parm *sub_p = parm_sublist; + String *empty_string = NewStringEmpty(); + String *lname = empty_string; while (p) { + if (sub_p) { + lname = Getattr(sub_p, "lname"); + sub_p = nextSibling(sub_p); + } Setattr(p, "lname", lname); p = nextSibling(p); } + Delete(empty_string); } /* process optional extra parameters - the variable replacements (undocumented) */