diff --git a/Examples/test-suite/java/template_template_parameters_runme.java b/Examples/test-suite/java/template_template_parameters_runme.java index 42135b982..6ccc858ac 100644 --- a/Examples/test-suite/java/template_template_parameters_runme.java +++ b/Examples/test-suite/java/template_template_parameters_runme.java @@ -14,15 +14,28 @@ public class template_template_parameters_runme { } public static void main(String argv[]) { + // Test first part ListFastBool listBool = new ListFastBool(); listBool.setItem(true); + boolean x_boolean = listBool.getAllotype(); if (listBool.getItem() != true) throw new RuntimeException("Failed"); ListDefaultDouble listDouble = new ListDefaultDouble(); listDouble.setItem(10.2); + double x_double = listDouble.getAllotype(); if (listDouble.getItem() != 10.2) throw new RuntimeException("Failed"); + + // Test second part + FloatTestStruct floatTestStruct = new FloatTestStruct(); + FloatContainer2 floatContainer2 = floatTestStruct.getX(); + floatContainer2.setX(8.1f); + IntTestStruct intTestStruct = new IntTestStruct(); + IntContainer1 intContainer1 = intTestStruct.getX(); + intContainer1.setX(91); + if (intContainer1.getX()!=91) + throw new RuntimeException("Failed"); } } diff --git a/Examples/test-suite/python/template_template_parameters_runme.py b/Examples/test-suite/python/template_template_parameters_runme.py new file mode 100644 index 000000000..4c018176e --- /dev/null +++ b/Examples/test-suite/python/template_template_parameters_runme.py @@ -0,0 +1,14 @@ +from template_template_parameters import * + +# Test second part +floatTestStruct = FloatTestStruct() +floatContainer2 = floatTestStruct.x +floatContainer2.x = 8.1 +intTestStruct = IntTestStruct() +intContainer1 = intTestStruct.x +intContainer1.x = 91 +if intContainer1.x!=91: + raise RuntimeError("Failed") +if intTestStruct.x.x!=91: + raise RuntimeError("Failed") + diff --git a/Examples/test-suite/template_template_parameters.i b/Examples/test-suite/template_template_parameters.i index 89197229e..3d8825697 100644 --- a/Examples/test-suite/template_template_parameters.i +++ b/Examples/test-suite/template_template_parameters.i @@ -2,6 +2,9 @@ %inline %{ + +/* part 1 */ + namespace pfc { template class t_alloc> class array_t {}; template class alloc_fast { @@ -16,7 +19,7 @@ class list_tt : public list_impl_t > { public: t_item item; -// typename t_alloc::alloc_type allotype; // SWIG can't handle this yet + typename t_alloc::alloc_type allotype; // SWIG can handle this now void xx() { typename t_alloc::alloc_type atype; // this type is the same as t_item type atype = true; @@ -29,11 +32,37 @@ void TestInstantiations() { (void) myArrayInt; (void) myListImplInt; } + +/* part 2 */ + +template +struct Container1 { + T x; +}; +template +struct Container2 { + U x; +}; +template class TemplateTemplateT> +struct TestStruct { + TemplateTemplateT x; +}; + %} +/* part 1 */ %template(ListImplFastBool) list_impl_t >; %template(ListFastBool) list_tt; %template(ListImplFastDouble) list_impl_t >; %template(ListDefaultDouble) list_tt; +%template(BoolAllocFast) pfc::alloc_fast; +%template(DoubleAllocFast) pfc::alloc_fast; + +/* part 2 */ +%template(IntTestStruct) TestStruct; +%template(FloatTestStruct) TestStruct; +%template(IntContainer1) Container1; +%template(FloatContainer2) Container2; + diff --git a/Source/Swig/stype.c b/Source/Swig/stype.c index fbf02bb1f..1c7f6cc00 100644 --- a/Source/Swig/stype.c +++ b/Source/Swig/stype.c @@ -1290,12 +1290,31 @@ void SwigType_typename_replace(SwigType *t, String *pat, String *rep) { /* Replaces a type of the form 'pat' with 'rep' */ Replace(e, pat, rep, DOH_REPLACE_ANY); } else if (SwigType_istemplate(e)) { - /* Replaces a type of the form 'pat' with 'rep' */ - if (Equal(e, pat)) { - String *repbase = SwigType_templateprefix(rep); - Replace(e, pat, repbase, DOH_REPLACE_ID | DOH_REPLACE_FIRST); - Delete(repbase); - } + /* Replaces a type of the form 'pat' with 'rep' */ + { + /* To match "e=TemplateTemplateT<(float)>" + * with "pat=TemplateTemplateT" + * we need to compare only the first part of the string e. + */ + int len = DohLen(pat); + + /* DohLen(e) > len, not >= (because we expecte at least a + * character '<' following the template typename) + */ + if (DohLen(e)>len) { + String *firstPartOfType = NewStringWithSize(e, len); + const char* e_as_char = DohData(e); + + if (Equal(firstPartOfType, pat) && e_as_char[len]=='<') { + String *repbase = SwigType_templateprefix(rep); + Replace(e, pat, repbase, DOH_REPLACE_ID | DOH_REPLACE_FIRST); + Delete(repbase); + } + Delete(firstPartOfType); + + } + } + { String *tsuffix; List *tparms = SwigType_parmlist(e);