1 %module member_pointer_const
2 // Same as member_pointer.i but using member pointer const functions
3
4 %{
5 #if defined(__SUNPRO_CC)
6 #pragma error_messages (off, badargtype2w) /* Formal argument ... is being passed extern "C" ... */
7 #pragma error_messages (off, wbadinit) /* Using extern "C" ... to initialize ... */
8 #pragma error_messages (off, wbadasg) /* Assigning extern "C" ... */
9 #endif
10 %}
11
12 %inline %{
13 class Shape {
14 public:
Shape()15 Shape() {
16 nshapes++;
17 }
~Shape()18 virtual ~Shape() {
19 nshapes--;
20 };
21 double x, y;
22 double *z;
23
24 void move(double dx, double dy);
25 virtual double area(void) const = 0;
26 virtual double perimeter(void) const = 0;
27 static int nshapes;
28 };
29
30 class Circle : public Shape {
31 private:
32 double radius;
33 public:
Circle(double r)34 Circle(double r) : radius(r) { };
35 virtual double area(void) const;
36 virtual double perimeter(void) const;
37 };
38
39 class Square : public Shape {
40 private:
41 double width;
42 public:
Square(double w)43 Square(double w) : width(w) { };
44 virtual double area(void) const;
45 virtual double perimeter(void) const;
46 };
47
48 /* Typedef */
49 typedef double (Shape::*PerimeterFunc_td)(void) const;
50
51 extern double do_op(Shape *s, double (Shape::*m)(void) const);
52 extern double do_op_td(Shape *s, PerimeterFunc_td m);
53
54 /* Functions that return member pointers */
55
56 extern double (Shape::*areapt())(void) const;
57 extern double (Shape::*perimeterpt())(void) const;
58 extern PerimeterFunc_td perimeterpt_td();
59
60 /* Global variables that are member pointers */
61 extern double (Shape::*areavar)(void) const;
62 extern double (Shape::*perimetervar)(void) const;
63 extern PerimeterFunc_td perimetervar_td;
64 %}
65
66 %{
67 # define SWIG_M_PI 3.14159265358979323846
68
69 /* Move the shape to a new location */
move(double dx,double dy)70 void Shape::move(double dx, double dy) {
71 x += dx;
72 y += dy;
73 }
74
75 int Shape::nshapes = 0;
76
area(void)77 double Circle::area(void) const {
78 return SWIG_M_PI*radius*radius;
79 }
80
perimeter(void)81 double Circle::perimeter(void) const {
82 return 2*SWIG_M_PI*radius;
83 }
84
area(void)85 double Square::area(void) const {
86 return width*width;
87 }
88
perimeter(void)89 double Square::perimeter(void) const {
90 return 4*width;
91 }
92
do_op(Shape * s,double (Shape::* m)(void)const)93 double do_op(Shape *s, double (Shape::*m)(void) const) {
94 return (s->*m)();
95 }
96
do_op_td(Shape * s,PerimeterFunc_td m)97 double do_op_td(Shape *s, PerimeterFunc_td m) {
98 return (s->*m)();
99 }
100
101 double (Shape::*areapt())(void) const {
102 return &Shape::area;
103 }
104
105 double (Shape::*perimeterpt())(void) const {
106 return &Shape::perimeter;
107 }
108
perimeterpt_td()109 PerimeterFunc_td perimeterpt_td() {
110 return &Shape::perimeter;
111 }
112
113 /* Member pointer variables */
114 double (Shape::*areavar)(void) const = &Shape::area;
115 double (Shape::*perimetervar)(void) const = &Shape::perimeter;
116 PerimeterFunc_td perimetervar_td = &Shape::perimeter;
117 %}
118
119
120 /* Some constants */
121 %constant double (Shape::*AREAPT)(void) const = &Shape::area;
122 %constant double (Shape::*PERIMPT)(void) const = &Shape::perimeter;
123 %constant double (Shape::*NULLPT)(void) const = 0;
124
125 /*
126 %inline %{
127 struct Funktions {
128 void retByRef(int & (*d)(double)) {}
129 };
130 void byRef(int & (Funktions::*d)(double)) {}
131 %}
132 */
133
134 %inline %{
135
136 struct Funktions {
addByValueFunktions137 int addByValue(const int &a, int b) const { return a+b; }
addByPointerFunktions138 int * addByPointer(const int &a, int b) const { static int val; val = a+b; return &val; }
addByReferenceFunktions139 int & addByReference(const int &a, int b) const { static int val; val = a+b; return val; }
140 };
141
call1(int (Funktions::* d)(const int &,int)const,int a,int b)142 int call1(int (Funktions::*d)(const int &, int) const, int a, int b) { Funktions f; return (f.*d)(a, b); }
143 //int call2(int * (Funktions::*d)(const int &, int) const, int a, int b) { Funktions f; return *(f.*d)(a, b); }
144 //int call3(int & (Funktions::*d)(const int &, int) const, int a, int b) { Funktions f; return (f.*d)(a, b); }
145 %}
146
147 %constant int (Funktions::*ADD_BY_VALUE)(const int &, int) const = &Funktions::addByValue;
148 //%constant int * (Funktions::*ADD_BY_POINTER)(const int &, int) const = &Funktions::addByPointer;
149 //%constant int & (Funktions::*ADD_BY_REFERENCE)(const int &, int) const = &Funktions::addByReference;
150