test virtual operators

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@9357 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2006-09-25 20:15:24 +00:00
parent d5d8f1a3ee
commit eae32a8da7
3 changed files with 52 additions and 4 deletions

View File

@ -31,7 +31,16 @@ public class runme
if (op.i != 99) throw new Exception("operator-- prefix failed (op)");
if (opNew.i != 99) throw new Exception("operator-- prefix failed (opNew)");
}
// overloaded operator class
Op k = new OpDerived(3);
int check_k = k.IntCast();
Assert(check_k == 6);
}
public static void Assert(bool b) {
if (!b)
throw new Exception("Assertion failed");
}
}

View File

@ -94,6 +94,16 @@ public class operator_overload_runme {
Op op = j.MinusMinusPostfix(0);
Assert(j.getI() == op.getI()-1);
}
// cast operators
Op k = new Op(3);
int check_k = k.IntCast();
Assert(check_k == 3);
Op l = new Op(4);
double check_l = l.DoubleCast();
Assert(check_l == 4);
}
public static void Assert(boolean b) {

View File

@ -71,6 +71,9 @@ see bottom for a set of possible tests
%rename(OrOperator) operator ||;
#endif
%rename(IntCast) operator int();
%rename(DoubleCast) operator double();
%inline %{
#if defined(_MSC_VER)
@ -79,12 +82,14 @@ see bottom for a set of possible tests
#include <assert.h>
class Op{
class Op {
public:
int i;
Op(int a=0):i(a)
Op(int a=0) : i(a)
{}
Op(const Op& o):i(o.i)
Op(const Op& o) : i(o.i)
{}
virtual ~Op()
{}
friend Op operator &&(const Op& a,const Op& b){return Op(a.i&&b.i);}
@ -134,6 +139,10 @@ public:
// TODO: <<,<<=
// cast operators
operator double() { return i; }
virtual operator int() { return i; }
// This method just checks that the operators are implemented correctly
static void sanity_check();
};
@ -211,6 +220,17 @@ __concat__ for contatenation (if language supports)
*/
%inline %{
class OpDerived : public Op {
public:
OpDerived(int a=0) : Op(a)
{}
// overloaded
virtual operator int() { return i*2; }
};
%}
%{
@ -314,6 +334,15 @@ void Op::sanity_check()
assert(j.i == original);
assert(newOp.i == newInt);
}
// cast operators
Op k=3;
int check_k = k;
assert (check_k == 3);
Op l=4;
double check_l = l;
assert (check_l == 4);
}
%}