polymorphism patch merge

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4435 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Mark Rose 2003-03-06 19:21:58 +00:00
commit c47494cb7a
15 changed files with 2007 additions and 15 deletions

View file

@ -341,13 +341,17 @@ Swig_cconstructor_call(String_or_char *name) {
* ----------------------------------------------------------------------------- */
String *
Swig_cppconstructor_call(String_or_char *name, ParmList *parms) {
Swig_cppconstructor_base_call(String_or_char *name, ParmList *parms, int skip_self, int disown) {
String *func;
String *nname;
int i = 0;
int comma = 0;
Parm *p = parms;
SwigType *pt;
if (skip_self) {
if (p) p = nextSibling(p);
i++;
}
nname = SwigType_namestr(name);
func = NewString("");
Printf(func,"new %s(", nname);
@ -363,11 +367,29 @@ Swig_cppconstructor_call(String_or_char *name, ParmList *parms) {
}
p = nextSibling(p);
}
if (disown >= 0) {
if (comma) Printf(func, ",");
Printf(func, "%d", disown);
}
Printf(func,")");
Delete(nname);
return func;
}
String *
Swig_cppconstructor_call(String_or_char *name, ParmList *parms) {
return Swig_cppconstructor_base_call(name, parms, 0, -1);
}
String *
Swig_cppconstructor_nodirector_call(String_or_char *name, ParmList *parms) {
return Swig_cppconstructor_base_call(name, parms, 1, -1);
}
String *
Swig_cppconstructor_director_call(String_or_char *name, ParmList *parms) {
return Swig_cppconstructor_base_call(name, parms, 0, 0);
}
/* -----------------------------------------------------------------------------
* Swig_cdestructor_call()
@ -473,7 +495,7 @@ Swig_MethodToFunction(Node *n, String *classname, int flags) {
name = Getattr(n,"name");
qualifier = Getattr(n,"qualifier");
parms = CopyParmList(nonvoid_parms(Getattr(n,"parms")));
type = NewString(classname);
if (qualifier) {
SwigType_push(type,qualifier);
@ -524,6 +546,62 @@ Swig_MethodToFunction(Node *n, String *classname, int flags) {
return SWIG_OK;
}
/* -----------------------------------------------------------------------------
* Swig_methodclass()
*
* This function returns the class node for a given method or class.
* ----------------------------------------------------------------------------- */
Node*
Swig_methodclass(Node *n) {
Node* type = Getattr(n, "nodeType");
if (!Cmp(type, "class")) return n;
return Getattr(n, "parentNode");
}
int
Swig_directorbase(Node *n) {
Node *classNode = Swig_methodclass(n);
return (classNode && (Getattr(classNode, "directorBase") != 0));
}
int
Swig_directorclass(Node *n) {
Node *classNode = Swig_methodclass(n);
return (Getattr(classNode, "vtable") != 0);
}
int
Swig_directormethod(Node *n) {
Node *classNode = Swig_methodclass(n);
if (classNode) {
Node *vtable = Getattr(classNode, "vtable");
if (vtable) {
String *name = Getattr(n, "name");
String *decl = Getattr(n, "decl");
String *method_id = NewStringf("%s|%s", name, decl);
Hash *item = Getattr(vtable, method_id);
Delete(method_id);
if (item) {
return (Getattr(item, "director") != 0);
}
}
}
return 0;
}
Node *
Swig_directormap(Node *module, String *type) {
int is_void = !Cmp(type, "void");
if (!is_void && module) {
String* base = SwigType_base(type);
Node *directormap = Getattr(module, "wrap:directormap");
if (directormap) return Getattr(directormap, base);
}
return 0;
}
/* -----------------------------------------------------------------------------
* Swig_ConstructorToFunction()
*
@ -537,10 +615,17 @@ Swig_ConstructorToFunction(Node *n, String *classname, int cplus, int flags)
SwigType *type;
String *membername;
String *mangled;
Node *classNode;
int use_director;
classNode = Swig_methodclass(n);
use_director = Swig_directorclass(n);
membername = Swig_name_construct(classname);
mangled = Swig_name_mangle(membername);
parms = CopyParmList(nonvoid_parms(Getattr(n,"parms")));
type = NewString(classname);
SwigType_add_pointer(type);
@ -558,7 +643,27 @@ Swig_ConstructorToFunction(Node *n, String *classname, int cplus, int flags)
Setattr(n,"wrap:action", Swig_cresult(type,"result", Swig_cfunction_call(mangled,parms)));
} else {
if (cplus) {
Setattr(n,"wrap:action", Swig_cresult(type,"result", Swig_cppconstructor_call(classname,parms)));
/* if a C++ director class exists, create it rather than the original class */
if (use_director) {
Node *parent = Swig_methodclass(n);
String *name = Getattr(parent, "sym:name");
String* directorname = NewStringf("__DIRECTOR__%s", name);
String* action = NewString("");
/* if Python class has been subclassed, create a director instance.
* otherwise, just create a normal instance.
*/
Printv(action, "if (arg1 != Py_None) { // subclassed\n",
Swig_cresult(type, "result", Swig_cppconstructor_director_call(directorname, parms)),
"} else {\n",
Swig_cresult(type, "result", Swig_cppconstructor_nodirector_call(classname, parms)),
"}\n",
NULL);
Setattr(n, "wrap:action", action);
Delete(action);
Delete(directorname);
} else {
Setattr(n,"wrap:action", Swig_cresult(type,"result", Swig_cppconstructor_call(classname,parms)));
}
} else {
Setattr(n,"wrap:action", Swig_cresult(type,"result", Swig_cconstructor_call(classname)));
}
@ -583,7 +688,8 @@ Swig_DestructorToFunction(Node *n, String *classname, int cplus, int flags)
{
SwigType *type;
Parm *p;
Node *classNode;
type = NewString(classname);
SwigType_add_pointer(type);
p = NewParm(type,"self");
@ -606,7 +712,9 @@ Swig_DestructorToFunction(Node *n, String *classname, int cplus, int flags)
Delete(mangled);
} else {
if (cplus) {
Setattr(n,"wrap:action", NewStringf("%s;\n", Swig_cppdestructor_call()));
String* action = NewString("");
Printf(action, "%s;\n", Swig_cppdestructor_call());
Setattr(n,"wrap:action", action);
} else {
Setattr(n,"wrap:action", NewStringf("%s;\n", Swig_cdestructor_call()));
}

View file

@ -341,6 +341,40 @@ String *Swig_name_destroy(const String_or_char *classname) {
return r;
}
/* -----------------------------------------------------------------------------
* Swig_name_disown()
*
* Returns the name of the accessor function used to disown an object.
* ----------------------------------------------------------------------------- */
String *Swig_name_disown(const String_or_char *classname) {
String *r;
String *f;
String *rclassname;
char *cname;
rclassname = SwigType_namestr(classname);
r = NewString("");
if (!naming_hash) naming_hash = NewHash();
f = Getattr(naming_hash,"disown");
if (!f) {
Append(r,"disown_%c");
} else {
Append(r,f);
}
cname = Char(rclassname);
if ((strncmp(cname,"struct ", 7) == 0) ||
((strncmp(cname,"class ", 6) == 0)) ||
((strncmp(cname,"union ", 6) == 0))) {
cname = strchr(cname, ' ')+1;
}
Replace(r,"%c",cname, DOH_REPLACE_ANY);
Delete(rclassname);
return r;
}
/* -----------------------------------------------------------------------------
* Swig_name_object_set()
*

View file

@ -347,6 +347,7 @@ SwigType_function_parms(SwigType *t) {
return firstp;
}
/* -----------------------------------------------------------------------------
* SwigType_add_template()
*
@ -561,6 +562,7 @@ String *SwigType_parm(SwigType *t) {
/* -----------------------------------------------------------------------------
* SwigType_ispointer()
* SwigType_ispointer_return()
* SwigType_isarray()
* SwigType_isreference()
* SwigType_isfunction()
@ -584,6 +586,30 @@ int SwigType_ispointer(SwigType *t) {
return 0;
}
int SwigType_ispointer_return(SwigType *t) {
char* c;
int idx;
if (!t) return 0;
c = Char(t);
idx = strlen(c)-4;
if (idx >= 0) {
return (strcmp(c+idx, ").p.") == 0);
}
return 0;
}
int SwigType_isreference_return(SwigType *t) {
char* c;
int idx;
if (!t) return 0;
c = Char(t);
idx = strlen(c)-4;
if (idx >= 0) {
return (strcmp(c+idx, ").r.") == 0);
}
return 0;
}
int SwigType_ismemberpointer(SwigType *t) {
char *c;
if (!t) return 0;

View file

@ -213,8 +213,10 @@ extern String *SwigType_lcaststr(SwigType *s, const String_or_char *id);
extern String *SwigType_manglestr(SwigType *t);
extern SwigType *SwigType_ltype(SwigType *t);
extern int SwigType_ispointer(SwigType *t);
extern int SwigType_ispointer_return(SwigType *t);
extern int SwigType_ismemberpointer(SwigType *t);
extern int SwigType_isreference(SwigType *t);
extern int SwigType_isreference_return(SwigType *t);
extern int SwigType_isarray(SwigType *t);
extern int SwigType_isfunction(SwigType *t);
extern int SwigType_isqualifier(SwigType *t);
@ -378,6 +380,7 @@ extern String *Swig_name_set(const String_or_char *vname);
extern String *Swig_name_construct(const String_or_char *classname);
extern String *Swig_name_copyconstructor(const String_or_char *classname);
extern String *Swig_name_destroy(const String_or_char *classname);
extern String *Swig_name_disown(const String_or_char *classname);
/* --- parameterized rename functions --- */
@ -443,6 +446,13 @@ extern int Swig_VarsetToFunction(Node *n);
#define CWRAP_EXTEND 0x01
#define CWRAP_SMART_POINTER 0x02
/* --- Director Helpers --- */
extern Node *Swig_methodclass(Node *n);
extern int Swig_directorbase(Node *n);
extern int Swig_directorclass(Node *n);
extern int Swig_directormethod(Node *n);
extern Node *Swig_directormap(Node *n, String *type);
/* --- Legacy Typemap API (somewhat simplified, ha!) --- */
extern void Swig_typemap_init();