From d0fad62e2c82b2c059127f23e70bf4a8ca909aad Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Wed, 1 Dec 2004 11:29:17 +0000 Subject: [PATCH] add support for smart pointers + %extend git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@6819 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- .../python/smart_pointer_member_runme.py | 5 ++ .../test-suite/smart_pointer_member.i | 59 +++++++++++-------- SWIG/Source/Modules/allocate.cxx | 9 +-- SWIG/Source/Modules/lang.cxx | 3 + SWIG/Source/Swig/cwrap.c | 35 +++++++++-- 5 files changed, 75 insertions(+), 36 deletions(-) diff --git a/SWIG/Examples/test-suite/python/smart_pointer_member_runme.py b/SWIG/Examples/test-suite/python/smart_pointer_member_runme.py index f8b4f2114..a18e132b8 100644 --- a/SWIG/Examples/test-suite/python/smart_pointer_member_runme.py +++ b/SWIG/Examples/test-suite/python/smart_pointer_member_runme.py @@ -10,6 +10,8 @@ b = Bar(f) b.y = 2 if f.y != 2: + print f.y + print b.y raise RuntimeError if b.x != f.x: @@ -17,3 +19,6 @@ if b.x != f.x: if b.z != f.z: raise RuntimeError + +if b.extension() != f.extension(): + raise RuntimeError diff --git a/SWIG/Examples/test-suite/smart_pointer_member.i b/SWIG/Examples/test-suite/smart_pointer_member.i index 52ca33b05..3a2c6fb2f 100644 --- a/SWIG/Examples/test-suite/smart_pointer_member.i +++ b/SWIG/Examples/test-suite/smart_pointer_member.i @@ -1,36 +1,43 @@ %module smart_pointer_member + +%extend Foo +{ + int extension(int i, int j) { return i; } + int extension(int i) { return i; } + int extension() { return 1; } +} + %inline %{ -class Foo { -public: - int x[4]; - int y; - static const int z; -}; - -class Bar { - Foo *f; -public: - Bar(Foo *f) : f(f) { } - Foo *operator->() { + class Foo { + public: + int x[4]; + int y; + static const int z; + }; + + class Bar { + Foo *f; + public: + Bar(Foo *f) : f(f) { } + Foo *operator->() { return f; - } -}; - - int get_y(Bar *b) - { - return (*b)->y; - } - - int get_z(Bar *b) - { - return (*b)->z; - } - - + } + }; + + int get_y(Bar *b) + { + return (*b)->y; + } + + int get_z(Bar *b) + { + return (*b)->z; + } %} + %{ const int Foo::z = 3; %} diff --git a/SWIG/Source/Modules/allocate.cxx b/SWIG/Source/Modules/allocate.cxx index 7d51fd635..37cd25a35 100644 --- a/SWIG/Source/Modules/allocate.cxx +++ b/SWIG/Source/Modules/allocate.cxx @@ -259,16 +259,17 @@ class Allocate : public Dispatcher { Node *c = firstChild(cls); String *kind = Getattr(cls,"kind"); - int mode; - if (Strcmp(kind,"class") == 0) mode = PRIVATE; - else mode = PUBLIC; + int mode = PUBLIC; + if (kind && (Strcmp(kind,"class") == 0)) mode = PRIVATE; while (c) { if (Getattr(c,"error") || Getattr(c,"feature:ignore")) { c = nextSibling(c); continue; } - if (Strcmp(nodeType(c),"cdecl") == 0) { + if (Strcmp(nodeType(c),"extend") == 0) { + Append(methods,c); + } else if (Strcmp(nodeType(c),"cdecl") == 0) { if (!Getattr(c,"feature:ignore")) { String *storage = Getattr(c,"storage"); if (!((Cmp(storage,"typedef") == 0))) { diff --git a/SWIG/Source/Modules/lang.cxx b/SWIG/Source/Modules/lang.cxx index b71ca2b7c..9e105301a 100644 --- a/SWIG/Source/Modules/lang.cxx +++ b/SWIG/Source/Modules/lang.cxx @@ -1055,6 +1055,9 @@ Language::memberfunctionHandler(Node *n) { } String *fname = Swig_name_member(ClassPrefix, symname); + if (Extend && SmartPointer) { + Setattr(n,"classname",Getattr(CurrentClass,"allocate:smartpointerbase")); + } /* Transformation */ Swig_MethodToFunction(n,ClassType, Getattr(n,"template") ? 0 : Extend | SmartPointer); Setattr(n,"sym:name",fname); diff --git a/SWIG/Source/Swig/cwrap.c b/SWIG/Source/Swig/cwrap.c index 1cf17c1f9..6b055dd9d 100644 --- a/SWIG/Source/Swig/cwrap.c +++ b/SWIG/Source/Swig/cwrap.c @@ -295,8 +295,12 @@ Swig_cfunction_call(String_or_char *name, ParmList *parms) { if ((SwigType_type(pt) != T_VOID)) { String *pname = Swig_cparm_name(p,i); String *rcaststr = SwigType_rcaststr(pt, pname); - if (comma) Printf(func,","); - Printf(func,"%s", rcaststr); + if (comma) { + Printf(func, ",%s", rcaststr); + } else { + Printf(func, "%s", rcaststr); + } + Delete(pname); Delete(rcaststr); comma = 1; i++; @@ -716,7 +720,8 @@ Swig_MethodToFunction(Node *n, String *classname, int flags) { String *defaultargs = Getattr(n,"defaultargs"); String *code = Getattr(n,"code"); - String *membername = Swig_name_member(classname, name); + String *cname = Getattr(n,"classname") ? Getattr(n,"classname") : classname; + String *membername = Swig_name_member(cname, name); String *mangled = Swig_name_mangle(membername); type = Getattr(n,"type"); @@ -739,9 +744,27 @@ Swig_MethodToFunction(Node *n, String *classname, int flags) { Delete(tmp); Delete(body); } - - Setattr(n,"wrap:action", Swig_cresult(Getattr(n,"type"),"result", Swig_cfunction_call(mangled,p))); - + if (flags & CWRAP_SMART_POINTER) { + int i = 0; + Parm *pp = p; + String *func = NewStringf("%s((%s*)(%s)->operator ->()", mangled, cname, + Swig_cparm_name(pp,i++)); + while ((pp = nextSibling(pp))) { + SwigType *pt = Getattr(pp,"type"); + if ((SwigType_type(pt) != T_VOID)) { + String *pname = Swig_cparm_name(pp,i++); + String *rcaststr = SwigType_rcaststr(pt, pname); + Printf(func,",%s", rcaststr); + Delete(rcaststr); + Delete(pname); + } + } + Printf(func,")"); + Setattr(n,"wrap:action", Swig_cresult(Getattr(n,"type"),"result", func)); + } else { + Setattr(n,"wrap:action", Swig_cresult(Getattr(n,"type"),"result", Swig_cfunction_call(mangled,p))); + } + Delete(membername); Delete(mangled); }