Additions to %types so that a user can specify the code to go into the casting / conversion function

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@10223 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2008-01-31 22:48:24 +00:00
commit 22e3a7b009
3 changed files with 60 additions and 0 deletions

View file

@ -244,6 +244,7 @@ CPP_TEST_CASES += \
smart_pointer_protected \
smart_pointer_rename \
smart_pointer_templatemethods \
smart_pointer_templatevariables \
smart_pointer_simple \
smart_pointer_static \
smart_pointer_typedef \
@ -331,6 +332,7 @@ CPP_TEST_CASES += \
typemap_variables \
typemap_various \
typename \
types_directive \
union_scope \
using1 \
using2 \

View file

@ -0,0 +1,12 @@
from types_directive import *
d1 = Time1(2001, 2, 3, 60)
newDate = add(d1, 7) # check that a Time1 instance is accepted where Date is expected
if newDate.day != 10:
raise RuntimeError
d2 = Time2(1999, 8, 7, 60)
newDate = add(d2, 7) # check that a Time2 instance is accepted where Date is expected
if newDate.day != 14:
raise RuntimeError

View file

@ -0,0 +1,46 @@
%module types_directive
%ignore Time2::operator Date *;
%inline %{
struct Date {
Date(unsigned int year, unsigned int month, unsigned int day) : year(year), month(month), day(day) {}
unsigned int year;
unsigned int month;
unsigned int day;
};
struct Time1 {
Time1(unsigned int year, unsigned int month, unsigned int day, unsigned int seconds) : date(year, month, day), seconds(seconds) {}
Date &dateFromTime() {
return date;
}
Date date;
unsigned int seconds;
};
struct Time2 {
Time2(unsigned int year, unsigned int month, unsigned int day, unsigned int seconds) : date(year, month, day), seconds(seconds) {}
operator Date *() {
return &date;
}
Date date;
unsigned int seconds;
};
Date add(const Date &date, unsigned int days) {
Date newDate = date;
newDate.day += days;
return newDate;
}
%}
// allow conversion from Date -> Time1 using the following code
%types(Time1 = Date) %{
Time1 *t = (Time1 *)$from;
Date &d = t->dateFromTime();
return (void *) &d;
%}
// allow conversion from Date -> Time2 using conversion operator (cast) in Time2
%types(Time2 = Date);