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/SWIG@9454 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
a49c872579
commit
e1eb8a266c
4 changed files with 141 additions and 48 deletions
|
|
@ -184,6 +184,7 @@ CPP_TEST_CASES += \
|
||||||
long_long_apply \
|
long_long_apply \
|
||||||
member_template \
|
member_template \
|
||||||
minherit \
|
minherit \
|
||||||
|
minherit2 \
|
||||||
mixed_types \
|
mixed_types \
|
||||||
multiple_inheritance \
|
multiple_inheritance \
|
||||||
name_cxx \
|
name_cxx \
|
||||||
|
|
|
||||||
94
Examples/test-suite/minherit2.i
Normal file
94
Examples/test-suite/minherit2.i
Normal 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() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
%}
|
||||||
|
|
||||||
|
|
@ -1492,22 +1492,20 @@ class CSHARP : public Language {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool derived = baseclass && getProxyName(c_baseclassname);
|
|
||||||
if (!baseclass)
|
|
||||||
baseclass = NewString("");
|
|
||||||
|
|
||||||
// Inheritance from pure C# classes
|
// Inheritance from pure C# classes
|
||||||
Node *attributes = NewHash();
|
Node *attributes = NewHash();
|
||||||
const String *pure_baseclass = typemapLookup("csbase", typemap_lookup_type, WARN_NONE, attributes);
|
const String *pure_baseclass = typemapLookup("csbase", typemap_lookup_type, WARN_NONE, attributes);
|
||||||
if (Len(pure_baseclass) > 0 && Len(baseclass) > 0) {
|
const String *wanted_base = baseclass ? baseclass : pure_baseclass;
|
||||||
if (GetFlag(attributes, "tmap:csbase:replace")) {
|
bool derived = baseclass && getProxyName(c_baseclassname);
|
||||||
Delete(baseclass);
|
|
||||||
baseclass = NewString("");
|
if (GetFlag(attributes, "tmap:csbase:replace")) {
|
||||||
} else {
|
wanted_base = pure_baseclass;
|
||||||
Swig_warning(WARN_CSHARP_MULTIPLE_INHERITANCE, input_file, line_number,
|
derived = *Char(wanted_base);
|
||||||
"Warning for %s proxy: Base %s ignored. Multiple inheritance is not supported in C#.\n", typemap_lookup_type, pure_baseclass);
|
Delete(baseclass);
|
||||||
pure_baseclass = empty_string;
|
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
|
// Pure C# interfaces
|
||||||
|
|
@ -1526,12 +1524,11 @@ class CSHARP : public Language {
|
||||||
Printv(proxy_class_def,
|
Printv(proxy_class_def,
|
||||||
typemapLookup("csclassmodifiers", typemap_lookup_type, WARN_CSHARP_TYPEMAP_CLASSMOD_UNDEF), // Class modifiers
|
typemapLookup("csclassmodifiers", typemap_lookup_type, WARN_CSHARP_TYPEMAP_CLASSMOD_UNDEF), // Class modifiers
|
||||||
" $csclassname", // Class name and base class
|
" $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
|
wanted_base,
|
||||||
pure_baseclass,
|
(*Char(wanted_base) && *Char(pure_interfaces)) ? // Interfaces
|
||||||
((derived || *Char(pure_baseclass)) && *Char(pure_interfaces)) ? // Interfaces
|
|
||||||
", " :
|
", " :
|
||||||
"",
|
"",
|
||||||
pure_interfaces,
|
pure_interfaces,
|
||||||
|
|
@ -1557,13 +1554,15 @@ class CSHARP : public Language {
|
||||||
destruct_methodname = Getattr(attributes, "tmap:csdestruct:methodname");
|
destruct_methodname = Getattr(attributes, "tmap:csdestruct:methodname");
|
||||||
destruct_methodmodifiers = Getattr(attributes, "tmap:csdestruct:methodmodifiers");
|
destruct_methodmodifiers = Getattr(attributes, "tmap:csdestruct:methodmodifiers");
|
||||||
}
|
}
|
||||||
if (!destruct_methodname) {
|
if (*Char(tm)) {
|
||||||
Swig_error(input_file, line_number,
|
if (!destruct_methodname) {
|
||||||
"No methodname attribute defined in csdestruct%s typemap for %s\n", (derived ? "_derived" : ""), proxy_class_name);
|
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,
|
if (!destruct_methodmodifiers) {
|
||||||
"No methodmodifier attribute defined in csdestruct%s typemap for %s.\n", (derived ? "_derived" : ""), proxy_class_name);
|
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
|
// Emit the Finalize and Dispose methods
|
||||||
|
|
@ -2792,10 +2791,10 @@ class CSHARP : public Language {
|
||||||
Replaceall(swigtype, "$imclassname", imclass_name);
|
Replaceall(swigtype, "$imclassname", imclass_name);
|
||||||
Replaceall(swigtype, "$dllimport", dllimport);
|
Replaceall(swigtype, "$dllimport", dllimport);
|
||||||
|
|
||||||
addCloseNamespace(namespce, f_swigtype);
|
|
||||||
|
|
||||||
Printv(f_swigtype, swigtype, NIL);
|
Printv(f_swigtype, swigtype, NIL);
|
||||||
|
|
||||||
|
addCloseNamespace(namespce, f_swigtype);
|
||||||
|
|
||||||
Close(f_swigtype);
|
Close(f_swigtype);
|
||||||
Delete(swigtype);
|
Delete(swigtype);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1554,21 +1554,19 @@ class JAVA : public Language {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool derived = baseclass && getProxyName(c_baseclassname);
|
bool derived = baseclass && getProxyName(c_baseclassname);
|
||||||
if (!baseclass)
|
|
||||||
baseclass = NewString("");
|
|
||||||
|
|
||||||
// Inheritance from pure Java classes
|
// Inheritance from pure Java classes
|
||||||
Node *attributes = NewHash();
|
Node *attributes = NewHash();
|
||||||
const String *pure_baseclass = typemapLookup("javabase", typemap_lookup_type, WARN_NONE, attributes);
|
const String *pure_baseclass = typemapLookup("javabase", typemap_lookup_type, WARN_NONE, attributes);
|
||||||
if (Len(pure_baseclass) > 0 && Len(baseclass) > 0) {
|
const String *wanted_base = baseclass ? baseclass : pure_baseclass;
|
||||||
if (GetFlag(attributes, "tmap:javabase:replace")) {
|
|
||||||
Delete(baseclass);
|
if (GetFlag(attributes, "tmap:javabase:replace")) {
|
||||||
baseclass = NewString("");
|
wanted_base = pure_baseclass;
|
||||||
} else {
|
derived = *Char(wanted_base);
|
||||||
Swig_warning(WARN_JAVA_MULTIPLE_INHERITANCE, input_file, line_number,
|
Delete(baseclass);
|
||||||
"Warning for %s proxy: Base %s ignored. Multiple inheritance is not supported in Java.\n", typemap_lookup_type, pure_baseclass);
|
baseclass = NULL;
|
||||||
pure_baseclass = empty_string;
|
} 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);
|
Delete(attributes);
|
||||||
|
|
||||||
|
|
@ -1581,11 +1579,10 @@ class JAVA : public Language {
|
||||||
"\n",
|
"\n",
|
||||||
typemapLookup("javaclassmodifiers", typemap_lookup_type, WARN_JAVA_TYPEMAP_CLASSMOD_UNDEF), // Class modifiers
|
typemapLookup("javaclassmodifiers", typemap_lookup_type, WARN_JAVA_TYPEMAP_CLASSMOD_UNDEF), // Class modifiers
|
||||||
" $javaclassname", // Class name and bases
|
" $javaclassname", // Class name and bases
|
||||||
(derived || *Char(pure_baseclass)) ?
|
(*Char(wanted_base)) ?
|
||||||
" extends " :
|
" extends " :
|
||||||
"",
|
"",
|
||||||
baseclass, // Note only one of these base classes should ever be set as multiple inheritance is not permissible
|
wanted_base,
|
||||||
pure_baseclass,
|
|
||||||
*Char(pure_interfaces) ? // Pure Java interfaces
|
*Char(pure_interfaces) ? // Pure Java interfaces
|
||||||
" implements " :
|
" implements " :
|
||||||
"",
|
"",
|
||||||
|
|
@ -1612,13 +1609,15 @@ class JAVA : public Language {
|
||||||
destruct_methodname = Getattr(attributes, "tmap:javadestruct:methodname");
|
destruct_methodname = Getattr(attributes, "tmap:javadestruct:methodname");
|
||||||
destruct_methodmodifiers = Getattr(attributes, "tmap:javadestruct:methodmodifiers");
|
destruct_methodmodifiers = Getattr(attributes, "tmap:javadestruct:methodmodifiers");
|
||||||
}
|
}
|
||||||
if (!destruct_methodname) {
|
if (*Char(tm)) {
|
||||||
Swig_error(input_file, line_number,
|
if (!destruct_methodname) {
|
||||||
"No methodname attribute defined in javadestruct%s typemap for %s\n", (derived ? "_derived" : ""), proxy_class_name);
|
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,
|
if (!destruct_methodmodifiers) {
|
||||||
"No methodmodifier attribute defined in javadestruct%s typemap for %s.\n", (derived ? "_derived" : ""), proxy_class_name);
|
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
|
// Emit the finalize and delete methods
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue