PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Swig | 135 lines | 93 code | 29 blank | 13 comment | 0 complexity | 29022c37d10b74e7dcd646469eb35fda MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. %module member_pointer
  2. %{
  3. #if defined(__SUNPRO_CC)
  4. #pragma error_messages (off, badargtype2w) /* Formal argument ... is being passed extern "C" ... */
  5. #pragma error_messages (off, wbadinit) /* Using extern "C" ... to initialize ... */
  6. #pragma error_messages (off, wbadasg) /* Assigning extern "C" ... */
  7. #endif
  8. %}
  9. %inline %{
  10. class Shape {
  11. public:
  12. Shape() {
  13. nshapes++;
  14. }
  15. virtual ~Shape() {
  16. nshapes--;
  17. };
  18. double x, y;
  19. double *z;
  20. void move(double dx, double dy);
  21. virtual double area(void) = 0;
  22. virtual double perimeter(void) = 0;
  23. static int nshapes;
  24. };
  25. class Circle : public Shape {
  26. private:
  27. double radius;
  28. public:
  29. Circle(double r) : radius(r) { };
  30. virtual double area(void);
  31. virtual double perimeter(void);
  32. };
  33. class Square : public Shape {
  34. private:
  35. double width;
  36. public:
  37. Square(double w) : width(w) { };
  38. virtual double area(void);
  39. virtual double perimeter(void);
  40. };
  41. extern double do_op(Shape *s, double (Shape::*m)(void));
  42. /* Functions that return member pointers */
  43. extern double (Shape::*areapt())(void);
  44. extern double (Shape::*perimeterpt())(void);
  45. /* Global variables that are member pointers */
  46. extern double (Shape::*areavar)(void);
  47. extern double (Shape::*perimetervar)(void);
  48. %}
  49. %{
  50. # define SWIG_M_PI 3.14159265358979323846
  51. /* Move the shape to a new location */
  52. void Shape::move(double dx, double dy) {
  53. x += dx;
  54. y += dy;
  55. }
  56. int Shape::nshapes = 0;
  57. double Circle::area(void) {
  58. return SWIG_M_PI*radius*radius;
  59. }
  60. double Circle::perimeter(void) {
  61. return 2*SWIG_M_PI*radius;
  62. }
  63. double Square::area(void) {
  64. return width*width;
  65. }
  66. double Square::perimeter(void) {
  67. return 4*width;
  68. }
  69. double do_op(Shape *s, double (Shape::*m)(void)) {
  70. return (s->*m)();
  71. }
  72. double (Shape::*areapt())(void) {
  73. return &Shape::area;
  74. }
  75. double (Shape::*perimeterpt())(void) {
  76. return &Shape::perimeter;
  77. }
  78. /* Member pointer variables */
  79. double (Shape::*areavar)(void) = &Shape::area;
  80. double (Shape::*perimetervar)(void) = &Shape::perimeter;
  81. %}
  82. /* Some constants */
  83. %constant double (Shape::*AREAPT)(void) = &Shape::area;
  84. %constant double (Shape::*PERIMPT)(void) = &Shape::perimeter;
  85. %constant double (Shape::*NULLPT)(void) = 0;
  86. /*
  87. %inline %{
  88. struct Funktions {
  89. void retByRef(int & (*d)(double)) {}
  90. };
  91. void byRef(int & (Funktions::*d)(double)) {}
  92. %}
  93. */
  94. %inline %{
  95. struct Funktions {
  96. int addByValue(const int &a, int b) { return a+b; }
  97. int * addByPointer(const int &a, int b) { static int val; val = a+b; return &val; }
  98. int & addByReference(const int &a, int b) { static int val; val = a+b; return val; }
  99. };
  100. int call1(int (Funktions::*d)(const int &, int), int a, int b) { Funktions f; return (f.*d)(a, b); }
  101. int call2(int * (Funktions::*d)(const int &, int), int a, int b) { Funktions f; return *(f.*d)(a, b); }
  102. int call3(int & (Funktions::*d)(const int &, int), int a, int b) { Funktions f; return (f.*d)(a, b); }
  103. %}
  104. %constant int (Funktions::*ADD_BY_VALUE)(const int &, int) = &Funktions::addByValue;
  105. %constant int * (Funktions::*ADD_BY_POINTER)(const int &, int) = &Funktions::addByPointer;
  106. %constant int & (Funktions::*ADD_BY_REFERENCE)(const int &, int) = &Funktions::addByReference;