diff --git a/Examples/test-suite/template_default_cache.i b/Examples/test-suite/template_default_cache.i index 5c752ae30..fd38a441d 100644 --- a/Examples/test-suite/template_default_cache.i +++ b/Examples/test-suite/template_default_cache.i @@ -2,17 +2,22 @@ %import "amodel.i" -%{ // C++ code to make this compilable +%inline %{ namespace d { template< typename T > class d {}; } - namespace ns__a { - namespace iface1 { - class Model {}; - typedef d::d ModelPtr; - } - using iface1::ModelPtr; +%} + +%ignore ns__a::iface1::Model; + +%inline %{ +namespace ns__a { + namespace iface1 { + class Model {}; + typedef d::d ModelPtr; } + using iface1::ModelPtr; +} %} %inline %{ @@ -23,6 +28,10 @@ namespace ns__b { ns__a::ModelPtr foo() { return ns__a::ModelPtr(); }; }; typedef d::d ModelPtr; + ns__a::ModelPtr get_mp_a() { return ns__a::ModelPtr(); } + ModelPtr get_mp_b() { return ModelPtr(); } } } %} +%template(AModelPtr) d::d; +%template(BModelPtr) d::d; diff --git a/Source/Swig/symbol.c b/Source/Swig/symbol.c index 84a6f4860..c548a0670 100644 --- a/Source/Swig/symbol.c +++ b/Source/Swig/symbol.c @@ -1906,26 +1906,37 @@ SwigType *Swig_symbol_template_deftype(const SwigType *type, Symtab *tscope) { int len = Len(elements); int i; #ifdef SWIG_TEMPLATE_DEFTYPE_CACHE - static Hash *deftype_cache = 0; + static Hash *s_cache = 0; + Hash *scope_cache; /* The lookup depends on the current scope and potential namespace qualification. Looking up x in namespace y is not the same as looking up x::y in outer scope. - -> we contruct an "artificial" key. */ - String *scopetype = tscope - ? NewStringf("%s..%s::%s", Swig_symbol_qualifiedscopename(tscope), Getattr(tscope, "name"), type) - : NewStringf("%s..%s::%s", Swig_symbol_qualifiedscopename(current_symtab), Swig_symbol_getscopename(), type); - if (!deftype_cache) { - deftype_cache = NewHash(); + -> we use a 2-level hash: first scope and then symbol. */ + String *scope_name = tscope + ? Swig_symbol_qualifiedscopename(tscope) + : Swig_symbol_qualifiedscopename(current_symtab); + String *type_name = tscope + ? NewStringf("%s::%s", Getattr(tscope, "name"), type) + : NewStringf("%s::%s", Swig_symbol_getscopename(), type); + if (!scope_name) scope_name = NewString("::"); + if (!s_cache) { + s_cache = NewHash(); } - if (scopetype) { - String *cres = Getattr(deftype_cache, scopetype); + scope_cache = Getattr(s_cache, scope_name); + if (scope_cache) { + String *cres = Getattr(scope_cache, type_name); if (cres) { Append(result, cres); #ifdef SWIG_DEBUG - Printf(stderr, "cached deftype %s(%s) -> %s\n", type, scopetype, result); + Printf(stderr, "cached deftype %s(%s) -> %s\n", type, scope_name, result); #endif - Delete(scopetype); + Delete(type_name); + Delete(scope_name); return result; } + } else { + scope_cache = NewHash(); + Setattr(s_cache, scope_name, scope_cache); + Delete(scope_name); } #endif @@ -2025,8 +2036,8 @@ SwigType *Swig_symbol_template_deftype(const SwigType *type, Symtab *tscope) { } Delete(elements); #ifdef SWIG_TEMPLATE_DEFTYPE_CACHE - Setattr(deftype_cache, scopetype, result); - Delete(scopetype); + Setattr(scope_cache, type_name, result); + Delete(type_name); #endif return result;