PageRenderTime 24ms CodeModel.GetById 19ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/types_directive.i

#
Swig | 54 lines | 46 code | 8 blank | 0 comment | 0 complexity | 3a7d8212503c6b2565aa07bd65237549 MD5 | raw file
 1%module types_directive
 2
 3#if defined(SWIGR)
 4// Avoid conflict with Date class in R
 5#define Date DateSwig
 6%inline %{
 7#define Date DateSwig
 8%}
 9#endif
10
11%ignore Time2::operator Date *;
12
13%inline %{
14struct Date {
15  Date(unsigned int year, unsigned int month, unsigned int day) : year(year), month(month), day(day) {}
16  unsigned int year;
17  unsigned int month;
18  unsigned int day;
19};
20
21struct Time1 {
22  Time1(unsigned int year, unsigned int month, unsigned int day, unsigned int seconds) : date(year, month, day), seconds(seconds) {}
23  Date &dateFromTime() {
24    return date;
25  }
26  Date date;
27  unsigned int seconds;
28};
29
30struct Time2 {
31  Time2(unsigned int year, unsigned int month, unsigned int day, unsigned int seconds) : date(year, month, day), seconds(seconds) {}
32  operator Date *() {
33    return &date;
34  }
35  Date date;
36  unsigned int seconds;
37};
38Date add(const Date &date, unsigned int days) {
39  Date newDate = date;
40  newDate.day += days;
41  return newDate;
42}
43%}
44
45// allow conversion from Date -> Time1 using the following code
46%types(Time1 = Date) %{
47  Time1 *t = (Time1 *)$from;
48  Date &d = t->dateFromTime();
49  return (void *) &d;
50%}
51
52// allow conversion from Date -> Time2 using conversion operator (cast) in Time2
53%types(Time2 = Date);
54