javabase/csbase typemaps mods to support morphing C++ abstract base classes into Java/C# interfaces

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9454 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2006-10-16 20:11:30 +00:00
commit cc56520503
4 changed files with 141 additions and 48 deletions

View file

@ -184,6 +184,7 @@ CPP_TEST_CASES += \
long_long_apply \
member_template \
minherit \
minherit2 \
mixed_types \
multiple_inheritance \
name_cxx \

View file

@ -0,0 +1,94 @@
%module minherit
// A multiple inheritance example, mainly for Java and C#.
// The example shows how it is possible to turn C++ abstract base classes into Java/C# interface.
// In the future, all this trouble might be more automated.
%warnfilter(SWIGWARN_JAVA_MULTIPLE_INHERITANCE,
SWIGWARN_CSHARP_MULTIPLE_INHERITANCE,
SWIGWARN_RUBY_MULTIPLE_INHERITANCE,
SWIGWARN_PHP4_MULTIPLE_INHERITANCE) RemoteMpe;
#if defined(SWIGJAVA) || defined(SWIGCSHARP)
#if defined(SWIGCSHARP)
#define javaclassmodifiers csclassmodifiers
#define javabody csbody
#define javafinalize csfinalize
#define javadestruct csdestruct
#define javaout csout
#define javainterfaces csinterfaces
#define javabase csbase
#endif
// Modify multiple inherited base classes into inheriting interfaces
%typemap(javainterfaces) RemoteMpe "IRemoteSyncIO, IRemoteAsyncIO";
%typemap(javabase, replace="1") RemoteMpe "";
// Turn the proxy class into an interface
%typemap(javaclassmodifiers) IRemoteSyncIO "public interface";
%typemap(javaclassmodifiers) IRemoteAsyncIO "public interface";
%typemap(javabody) IRemoteSyncIO "";
%typemap(javabody) IRemoteAsyncIO "";
%typemap(javafinalize) IRemoteSyncIO "";
%typemap(javafinalize) IRemoteAsyncIO "";
%typemap(javadestruct) IRemoteSyncIO "";
%typemap(javadestruct) IRemoteAsyncIO "";
// Turn the methods into abstract methods
%typemap(javaout) void IRemoteSyncIO::syncmethod ";"
%typemap(javaout) void IRemoteAsyncIO::asyncmethod ";"
#if defined(SWIGJAVA)
%javamethodmodifiers IRemoteSyncIO::syncmethod "abstract public";
%javamethodmodifiers IRemoteAsyncIO::asyncmethod "abstract public";
// Features are inherited by derived classes, so override this
%javamethodmodifiers RemoteMpe::syncmethod "public"
%javamethodmodifiers RemoteMpe::asyncmethod "public"
#elif defined(SWIGCSHARP)
%csmethodmodifiers IRemoteSyncIO::syncmethod "";
%csmethodmodifiers IRemoteAsyncIO::asyncmethod "";
// Features are inherited by derived classes, so override this
%csmethodmodifiers RemoteMpe::syncmethod "public"
%csmethodmodifiers RemoteMpe::asyncmethod "public"
#endif
#endif
%inline %{
class IRemoteSyncIO
{
public:
virtual ~IRemoteSyncIO () {}
virtual void syncmethod() = 0;
protected:
IRemoteSyncIO () {}
private:
IRemoteSyncIO (const IRemoteSyncIO&);
IRemoteSyncIO& operator= (const IRemoteSyncIO&);
};
class IRemoteAsyncIO
{
public:
virtual ~IRemoteAsyncIO () {}
virtual void asyncmethod() = 0;
protected:
IRemoteAsyncIO () {}
private:
IRemoteAsyncIO (const IRemoteAsyncIO&);
IRemoteAsyncIO& operator= (const IRemoteAsyncIO&);
};
class RemoteMpe : public IRemoteSyncIO, public IRemoteAsyncIO
{
public:
virtual void syncmethod() {}
virtual void asyncmethod() {}
};
%}

View file

@ -1492,22 +1492,20 @@ class CSHARP : public Language {
}
}
bool derived = baseclass && getProxyName(c_baseclassname);
if (!baseclass)
baseclass = NewString("");
// Inheritance from pure C# classes
Node *attributes = NewHash();
const String *pure_baseclass = typemapLookup("csbase", typemap_lookup_type, WARN_NONE, attributes);
if (Len(pure_baseclass) > 0 && Len(baseclass) > 0) {
if (GetFlag(attributes, "tmap:csbase:replace")) {
Delete(baseclass);
baseclass = NewString("");
} else {
Swig_warning(WARN_CSHARP_MULTIPLE_INHERITANCE, input_file, line_number,
"Warning for %s proxy: Base %s ignored. Multiple inheritance is not supported in C#.\n", typemap_lookup_type, pure_baseclass);
pure_baseclass = empty_string;
}
const String *wanted_base = baseclass ? baseclass : pure_baseclass;
bool derived = baseclass && getProxyName(c_baseclassname);
if (GetFlag(attributes, "tmap:csbase:replace")) {
wanted_base = pure_baseclass;
derived = *Char(wanted_base);
Delete(baseclass);
baseclass = NULL;
} else if (Len(pure_baseclass) > 0 && Len(baseclass) > 0) {
Swig_warning(WARN_CSHARP_MULTIPLE_INHERITANCE, input_file, line_number,
"Warning for %s proxy: Base %s ignored. Multiple inheritance is not supported in C#.\n", typemap_lookup_type, pure_baseclass);
}
// Pure C# interfaces
@ -1526,12 +1524,11 @@ class CSHARP : public Language {
Printv(proxy_class_def,
typemapLookup("csclassmodifiers", typemap_lookup_type, WARN_CSHARP_TYPEMAP_CLASSMOD_UNDEF), // Class modifiers
" $csclassname", // Class name and base class
(derived || *Char(pure_baseclass) || *Char(pure_interfaces)) ?
(*Char(wanted_base) || *Char(pure_interfaces)) ?
" : " :
"",
baseclass, // Note only one of these base classes should ever be set as multiple inheritance is not permissible
pure_baseclass,
((derived || *Char(pure_baseclass)) && *Char(pure_interfaces)) ? // Interfaces
wanted_base,
(*Char(wanted_base) && *Char(pure_interfaces)) ? // Interfaces
", " :
"",
pure_interfaces,
@ -1557,13 +1554,15 @@ class CSHARP : public Language {
destruct_methodname = Getattr(attributes, "tmap:csdestruct:methodname");
destruct_methodmodifiers = Getattr(attributes, "tmap:csdestruct:methodmodifiers");
}
if (!destruct_methodname) {
Swig_error(input_file, line_number,
"No methodname attribute defined in csdestruct%s typemap for %s\n", (derived ? "_derived" : ""), proxy_class_name);
}
if (!destruct_methodmodifiers) {
Swig_error(input_file, line_number,
"No methodmodifier attribute defined in csdestruct%s typemap for %s.\n", (derived ? "_derived" : ""), proxy_class_name);
if (*Char(tm)) {
if (!destruct_methodname) {
Swig_error(input_file, line_number,
"No methodname attribute defined in csdestruct%s typemap for %s\n", (derived ? "_derived" : ""), proxy_class_name);
}
if (!destruct_methodmodifiers) {
Swig_error(input_file, line_number,
"No methodmodifiers attribute defined in csdestruct%s typemap for %s.\n", (derived ? "_derived" : ""), proxy_class_name);
}
}
// Emit the Finalize and Dispose methods
@ -2792,10 +2791,10 @@ class CSHARP : public Language {
Replaceall(swigtype, "$imclassname", imclass_name);
Replaceall(swigtype, "$dllimport", dllimport);
addCloseNamespace(namespce, f_swigtype);
Printv(f_swigtype, swigtype, NIL);
addCloseNamespace(namespce, f_swigtype);
Close(f_swigtype);
Delete(swigtype);
}

View file

@ -1554,21 +1554,19 @@ class JAVA : public Language {
}
bool derived = baseclass && getProxyName(c_baseclassname);
if (!baseclass)
baseclass = NewString("");
// Inheritance from pure Java classes
Node *attributes = NewHash();
const String *pure_baseclass = typemapLookup("javabase", typemap_lookup_type, WARN_NONE, attributes);
if (Len(pure_baseclass) > 0 && Len(baseclass) > 0) {
if (GetFlag(attributes, "tmap:javabase:replace")) {
Delete(baseclass);
baseclass = NewString("");
} else {
Swig_warning(WARN_JAVA_MULTIPLE_INHERITANCE, input_file, line_number,
"Warning for %s proxy: Base %s ignored. Multiple inheritance is not supported in Java.\n", typemap_lookup_type, pure_baseclass);
pure_baseclass = empty_string;
}
const String *wanted_base = baseclass ? baseclass : pure_baseclass;
if (GetFlag(attributes, "tmap:javabase:replace")) {
wanted_base = pure_baseclass;
derived = *Char(wanted_base);
Delete(baseclass);
baseclass = NULL;
} else if (Len(pure_baseclass) > 0 && Len(baseclass) > 0) {
Swig_warning(WARN_JAVA_MULTIPLE_INHERITANCE, input_file, line_number,
"Warning for %s proxy: Base %s ignored. Multiple inheritance is not supported in Java.\n", typemap_lookup_type, pure_baseclass);
}
Delete(attributes);
@ -1581,11 +1579,10 @@ class JAVA : public Language {
"\n",
typemapLookup("javaclassmodifiers", typemap_lookup_type, WARN_JAVA_TYPEMAP_CLASSMOD_UNDEF), // Class modifiers
" $javaclassname", // Class name and bases
(derived || *Char(pure_baseclass)) ?
(*Char(wanted_base)) ?
" extends " :
"",
baseclass, // Note only one of these base classes should ever be set as multiple inheritance is not permissible
pure_baseclass,
wanted_base,
*Char(pure_interfaces) ? // Pure Java interfaces
" implements " :
"",
@ -1612,13 +1609,15 @@ class JAVA : public Language {
destruct_methodname = Getattr(attributes, "tmap:javadestruct:methodname");
destruct_methodmodifiers = Getattr(attributes, "tmap:javadestruct:methodmodifiers");
}
if (!destruct_methodname) {
Swig_error(input_file, line_number,
"No methodname attribute defined in javadestruct%s typemap for %s\n", (derived ? "_derived" : ""), proxy_class_name);
}
if (!destruct_methodmodifiers) {
Swig_error(input_file, line_number,
"No methodmodifier attribute defined in javadestruct%s typemap for %s.\n", (derived ? "_derived" : ""), proxy_class_name);
if (*Char(tm)) {
if (!destruct_methodname) {
Swig_error(input_file, line_number,
"No methodname attribute defined in javadestruct%s typemap for %s\n", (derived ? "_derived" : ""), proxy_class_name);
}
if (!destruct_methodmodifiers) {
Swig_error(input_file, line_number,
"No methodmodifiers attribute defined in javadestruct%s typemap for %s.\n", (derived ? "_derived" : ""), proxy_class_name);
}
}
// Emit the finalize and delete methods