PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. %module types_directive
  2. #if defined(SWIGR)
  3. // Avoid conflict with Date class in R
  4. #define Date DateSwig
  5. %inline %{
  6. #define Date DateSwig
  7. %}
  8. #endif
  9. %ignore Time2::operator Date *;
  10. %inline %{
  11. struct Date {
  12. Date(unsigned int year, unsigned int month, unsigned int day) : year(year), month(month), day(day) {}
  13. unsigned int year;
  14. unsigned int month;
  15. unsigned int day;
  16. };
  17. struct Time1 {
  18. Time1(unsigned int year, unsigned int month, unsigned int day, unsigned int seconds) : date(year, month, day), seconds(seconds) {}
  19. Date &dateFromTime() {
  20. return date;
  21. }
  22. Date date;
  23. unsigned int seconds;
  24. };
  25. struct Time2 {
  26. Time2(unsigned int year, unsigned int month, unsigned int day, unsigned int seconds) : date(year, month, day), seconds(seconds) {}
  27. operator Date *() {
  28. return &date;
  29. }
  30. Date date;
  31. unsigned int seconds;
  32. };
  33. Date add(const Date &date, unsigned int days) {
  34. Date newDate = date;
  35. newDate.day += days;
  36. return newDate;
  37. }
  38. %}
  39. // allow conversion from Date -> Time1 using the following code
  40. %types(Time1 = Date) %{
  41. Time1 *t = (Time1 *)$from;
  42. Date &d = t->dateFromTime();
  43. return (void *) &d;
  44. %}
  45. // allow conversion from Date -> Time2 using conversion operator (cast) in Time2
  46. %types(Time2 = Date);