New typemaps: javadestruct_base and javadestruct_derived

for the C++ destructor wrapper. The _base version gets used by
classes at the top of an inheritance chain and the _derived
version gets used by other classes.


git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4876 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2003-06-10 21:40:26 +00:00
commit 9cccf88afa
2 changed files with 48 additions and 17 deletions

View file

@ -685,6 +685,23 @@
} }
%} %}
%typemap(javadestruct_base) SWIGTYPE {
if(swigCPtr != 0 && swigCMemOwn) {
$jnicall;
swigCMemOwn = false;
}
swigCPtr = 0;
}
%typemap(javadestruct_derived) SWIGTYPE {
if(swigCPtr != 0 && swigCMemOwn) {
$jnicall;
swigCMemOwn = false;
}
swigCPtr = 0;
super.delete();
}
%typemap(javagetcptr) SWIGTYPE, SWIGTYPE *, SWIGTYPE &, SWIGTYPE [], SWIGTYPE (CLASS::*) %{ %typemap(javagetcptr) SWIGTYPE, SWIGTYPE *, SWIGTYPE &, SWIGTYPE [], SWIGTYPE (CLASS::*) %{
protected static long getCPtr($javaclassname obj) { protected static long getCPtr($javaclassname obj) {
return (obj == null) ? 0 : obj.swigCPtr; return (obj == null) ? 0 : obj.swigCPtr;

View file

@ -1120,22 +1120,36 @@ class JAVA : public Language {
NIL); NIL);
} }
// C++ destructor is wrapped by the delete method
String *destruct = NewString("");
const String *tm = NULL;
if (derived)
tm = javaTypemapLookup("javadestruct_derived", classDeclarationName, WARN_NONE);
else
tm = javaTypemapLookup("javadestruct_base", classDeclarationName, WARN_NONE);
// Emit the finalize and delete methods
if (*Char(destructor_call) && tm) {
Printv(destruct, tm, NIL);
Replaceall(destruct, "$jnicall", destructor_call);
// finalize method
Printv(shadow_classdef, Printv(shadow_classdef,
javaTypemapLookup("javafinalize", classDeclarationName, WARN_NONE), // finalize method javaTypemapLookup("javafinalize", classDeclarationName, WARN_NONE),
"\n", "\n",
*Char(destructor_call) ? NIL);
" public void delete() {\n" : // delete method
" protected void delete() {\n", if (*Char(destruct)) {
" if(swigCPtr != 0 && swigCMemOwn) {\n", Printv(shadow_classdef, " public void delete() ", destruct, "\n", NIL);
destructor_call, }
"", } else {
" swigCMemOwn = false;\n", // Ensure method exists for derived class to call. Don't bother if the typemap doesn't exist.
derived ? // Zero all pointers up any inheritance hierarchy if (tm && *Char(tm))
" super.delete();\n" : Printv(shadow_classdef, "\n protected void delete() {\n }\n", NIL);
"", }
" }\n", Delete(destruct);
" swigCPtr = 0;\n",
" }\n", // Emit various other methods
Printv(shadow_classdef,
javaTypemapLookup("javagetcptr", classDeclarationName, WARN_JAVA_TYPEMAP_GETCPTR_UNDEF), // getCPtr method javaTypemapLookup("javagetcptr", classDeclarationName, WARN_JAVA_TYPEMAP_GETCPTR_UNDEF), // getCPtr method
javaTypemapLookup("javacode", classDeclarationName, WARN_NONE), // extra Java code javaTypemapLookup("javacode", classDeclarationName, WARN_NONE), // extra Java code
"\n", "\n",
@ -1543,7 +1557,7 @@ class JAVA : public Language {
String *symname = Getattr(n,"sym:name"); String *symname = Getattr(n,"sym:name");
if(proxy_flag) { if(proxy_flag) {
Printv(destructor_call, " ", jniclass_name, ".", Swig_name_destroy(symname), "(swigCPtr);\n", NIL); Printv(destructor_call, jniclass_name, ".", Swig_name_destroy(symname), "(swigCPtr)", NIL);
} }
return SWIG_OK; return SWIG_OK;
} }