Suppress warnings about covariant return types in Java and C#.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@5598 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
9e35a6e442
commit
fd5f106b2e
2 changed files with 15 additions and 34 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
%module(directors="1") director_detect
|
%module(directors="1") director_detect
|
||||||
|
|
||||||
|
%warnfilter(822, 842) cloner; /* Java, C# covariant return types */
|
||||||
|
|
||||||
%{
|
%{
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
%module(directors="1") virtual_poly
|
%module(directors="1") virtual_poly
|
||||||
|
|
||||||
|
%warnfilter(822, 842) copy; /* Java, C# covariant return types */
|
||||||
|
%warnfilter(822, 842) ref_this; /* Java, C# covariant return types */
|
||||||
|
|
||||||
//
|
//
|
||||||
// Check this example with directors wherever is possible. Python and
|
// Check this example with directors wherever possible.
|
||||||
// ruby work fine with and without directors. In theory, Java may
|
|
||||||
// start to work with directors, but this is not tested yet (my Java
|
|
||||||
// installation is broken).
|
|
||||||
//
|
//
|
||||||
//%feature("director");
|
//%feature("director");
|
||||||
|
|
||||||
|
|
@ -31,21 +31,19 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
NInt and NDouble are both NNumber derivated classes, but they
|
NInt and NDouble are both NNumber derived classes, but they
|
||||||
have more different than common attributes.
|
have more different than common attributes.
|
||||||
|
|
||||||
In particular the function 'get', that is type dependent, can't
|
In particular the function 'get', that is type dependent, can't
|
||||||
be included in the NNumber abstract interface.
|
be included in the NNumber abstract interface.
|
||||||
|
|
||||||
For this reason, the virtual 'copy' function has a polymorphic
|
For this reason, the virtual 'copy' method has a polymorphic (covariant)
|
||||||
return type, since in most of the cases we don't want to lost the
|
return type, since in most of the cases we don't want to lose the
|
||||||
original object type, which is very very important.
|
original object type, which is very very important.
|
||||||
|
|
||||||
Using the polymorphic return type reduced greatly the need of
|
Using the polymorphic return type reduced greatly the need of
|
||||||
using 'dynamic_cast' at the C++ side, and at the target languages
|
using 'dynamic_cast' at the C++ side, and at the target languages
|
||||||
that support it. Python is a target language that support
|
that support it.
|
||||||
this feature, Java and Csharp don't.
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
struct NInt : NNumber
|
struct NInt : NNumber
|
||||||
{
|
{
|
||||||
|
|
@ -110,23 +108,17 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Java (and csharp) can not support the polymorphic return type for
|
Java and C# do not support the polymorphic (covariant) return types used
|
||||||
'copy'. So, it just emit 'plain' copy functions for all the cases:
|
in the copy method. So, they just emit 'plain' copy functions as if this is
|
||||||
|
being wrapped instead:
|
||||||
|
|
||||||
NNumber* NNumber::copy() const;
|
NNumber* NNumber::copy() const;
|
||||||
NNumber* NInt::copy() const;
|
NNumber* NInt::copy() const;
|
||||||
NNumber* NDouble::copy() const;
|
NNumber* NDouble::copy() const;
|
||||||
|
|
||||||
In the last two cases, the original 'NInt' and 'NDouble' return
|
|
||||||
types are lost in the target side. This seems to be a restriction
|
|
||||||
of the language (strongly typed and 'by value' oriented), and
|
|
||||||
there is not much that can be done to work it around.
|
|
||||||
|
|
||||||
However, since the objects provide their own downcasting
|
However, since the objects provide their own downcasting
|
||||||
mechanim, the narrow methods similar to the CORBA mechanism,
|
mechanism, the narrow methods similar to the CORBA mechanism,
|
||||||
in theory you should be able to recover the original object
|
could be used, otherwise use the Java/C# downcasts.
|
||||||
types, just as you can do it in the C++ side or in other
|
|
||||||
languages.
|
|
||||||
*/
|
*/
|
||||||
inline NInt* NInt::narrow(NNumber* n) {
|
inline NInt* NInt::narrow(NNumber* n) {
|
||||||
// this is just a plain C++ dynamic_cast, but in theory the user
|
// this is just a plain C++ dynamic_cast, but in theory the user
|
||||||
|
|
@ -137,17 +129,4 @@
|
||||||
inline NDouble* NDouble::narrow(NNumber* n) {
|
inline NDouble* NDouble::narrow(NNumber* n) {
|
||||||
return dynamic_cast<NDouble*>(n);
|
return dynamic_cast<NDouble*>(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
but the narrow methods don't work either in Java (see the
|
|
||||||
java/virtual_poly_runme.java file), because of the current way
|
|
||||||
polymorphic classes are wrapped, the user cannot downcast the
|
|
||||||
Java temporary object when the C++ class (and associated Java
|
|
||||||
class) is a base class in a class hierarchy.
|
|
||||||
|
|
||||||
The 'narrow' methods work currently fine in languages like
|
|
||||||
python, but in there the polymorphic return type also works, so,
|
|
||||||
you are not forced to use them everytime (see the
|
|
||||||
python/virtual_poly_runme.py file).
|
|
||||||
*/
|
|
||||||
%}
|
%}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue