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