Add support for parsing and wrapping member const function pointers
This commit is contained in:
parent
67edda3bf5
commit
5aff26fcb5
6 changed files with 383 additions and 21 deletions
|
|
@ -278,6 +278,7 @@ CPP_TEST_CASES += \
|
|||
memberin_extend \
|
||||
member_funcptr_galore \
|
||||
member_pointer \
|
||||
member_pointer_const \
|
||||
member_template \
|
||||
minherit \
|
||||
minherit2 \
|
||||
|
|
|
|||
58
Examples/test-suite/java/member_pointer_const_runme.java
Normal file
58
Examples/test-suite/java/member_pointer_const_runme.java
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import member_pointer_const.*;
|
||||
|
||||
public class member_pointer_const_runme {
|
||||
|
||||
static {
|
||||
try {
|
||||
System.loadLibrary("member_pointer_const");
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
public static SWIGTYPE_m_Shape__f_void__double memberPtr = null;
|
||||
|
||||
public static void main(String argv[]) {
|
||||
// Get the pointers
|
||||
|
||||
SWIGTYPE_m_Shape__f_void__double area_pt = member_pointer_const.areapt();
|
||||
SWIGTYPE_m_Shape__f_void__double perim_pt = member_pointer_const.perimeterpt();
|
||||
|
||||
// Create some objects
|
||||
|
||||
Square s = new Square(10);
|
||||
|
||||
// Do some calculations
|
||||
|
||||
check( "Square area ", 100.0, member_pointer_const.do_op(s,area_pt) );
|
||||
check( "Square perim", 40.0, member_pointer_const.do_op(s,perim_pt) );
|
||||
|
||||
memberPtr = member_pointer_const.getAreavar();
|
||||
memberPtr = member_pointer_const.getPerimetervar();
|
||||
|
||||
// Try the variables
|
||||
check( "Square area ", 100.0, member_pointer_const.do_op(s,member_pointer_const.getAreavar()) );
|
||||
check( "Square perim", 40.0, member_pointer_const.do_op(s,member_pointer_const.getPerimetervar()) );
|
||||
|
||||
// Modify one of the variables
|
||||
member_pointer_const.setAreavar(perim_pt);
|
||||
|
||||
check( "Square perimeter", 40.0, member_pointer_const.do_op(s,member_pointer_const.getAreavar()) );
|
||||
|
||||
// Try the constants
|
||||
|
||||
memberPtr = member_pointer_const.AREAPT;
|
||||
memberPtr = member_pointer_const.PERIMPT;
|
||||
memberPtr = member_pointer_const.NULLPT;
|
||||
|
||||
check( "Square area ", 100.0, member_pointer_const.do_op(s,member_pointer_const.AREAPT) );
|
||||
check( "Square perim", 40.0, member_pointer_const.do_op(s,member_pointer_const.PERIMPT) );
|
||||
|
||||
}
|
||||
|
||||
private static void check(String what, double expected, double actual) {
|
||||
if (expected != actual)
|
||||
throw new RuntimeException("Failed: " + what + " Expected: " + expected + " Actual: " + actual);
|
||||
}
|
||||
}
|
||||
135
Examples/test-suite/member_pointer_const.i
Normal file
135
Examples/test-suite/member_pointer_const.i
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
%module member_pointer_const
|
||||
// Same as member_pointer.i but using member pointer const functions
|
||||
|
||||
%{
|
||||
#if defined(__SUNPRO_CC)
|
||||
#pragma error_messages (off, badargtype2w) /* Formal argument ... is being passed extern "C" ... */
|
||||
#pragma error_messages (off, wbadinit) /* Using extern "C" ... to initialize ... */
|
||||
#pragma error_messages (off, wbadasg) /* Assigning extern "C" ... */
|
||||
#endif
|
||||
%}
|
||||
|
||||
%inline %{
|
||||
class Shape {
|
||||
public:
|
||||
Shape() {
|
||||
nshapes++;
|
||||
}
|
||||
virtual ~Shape() {
|
||||
nshapes--;
|
||||
};
|
||||
double x, y;
|
||||
double *z;
|
||||
|
||||
void move(double dx, double dy);
|
||||
virtual double area(void) const = 0;
|
||||
virtual double perimeter(void) const = 0;
|
||||
static int nshapes;
|
||||
};
|
||||
|
||||
class Circle : public Shape {
|
||||
private:
|
||||
double radius;
|
||||
public:
|
||||
Circle(double r) : radius(r) { };
|
||||
virtual double area(void) const;
|
||||
virtual double perimeter(void) const;
|
||||
};
|
||||
|
||||
class Square : public Shape {
|
||||
private:
|
||||
double width;
|
||||
public:
|
||||
Square(double w) : width(w) { };
|
||||
virtual double area(void) const;
|
||||
virtual double perimeter(void) const;
|
||||
};
|
||||
|
||||
extern double do_op(Shape *s, double (Shape::*m)(void) const);
|
||||
|
||||
/* Functions that return member pointers */
|
||||
|
||||
extern double (Shape::*areapt())(void) const;
|
||||
extern double (Shape::*perimeterpt())(void) const;
|
||||
|
||||
/* Global variables that are member pointers */
|
||||
extern double (Shape::*areavar)(void) const;
|
||||
extern double (Shape::*perimetervar)(void) const;
|
||||
|
||||
%}
|
||||
|
||||
%{
|
||||
# define SWIG_M_PI 3.14159265358979323846
|
||||
|
||||
/* Move the shape to a new location */
|
||||
void Shape::move(double dx, double dy) {
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
|
||||
int Shape::nshapes = 0;
|
||||
|
||||
double Circle::area(void) const {
|
||||
return SWIG_M_PI*radius*radius;
|
||||
}
|
||||
|
||||
double Circle::perimeter(void) const {
|
||||
return 2*SWIG_M_PI*radius;
|
||||
}
|
||||
|
||||
double Square::area(void) const {
|
||||
return width*width;
|
||||
}
|
||||
|
||||
double Square::perimeter(void) const {
|
||||
return 4*width;
|
||||
}
|
||||
|
||||
double do_op(Shape *s, double (Shape::*m)(void) const) {
|
||||
return (s->*m)();
|
||||
}
|
||||
|
||||
double (Shape::*areapt())(void) const {
|
||||
return &Shape::area;
|
||||
}
|
||||
|
||||
double (Shape::*perimeterpt())(void) const {
|
||||
return &Shape::perimeter;
|
||||
}
|
||||
|
||||
/* Member pointer variables */
|
||||
double (Shape::*areavar)(void) const = &Shape::area;
|
||||
double (Shape::*perimetervar)(void) const = &Shape::perimeter;
|
||||
%}
|
||||
|
||||
|
||||
/* Some constants */
|
||||
%constant double (Shape::*AREAPT)(void) const = &Shape::area;
|
||||
%constant double (Shape::*PERIMPT)(void) const = &Shape::perimeter;
|
||||
%constant double (Shape::*NULLPT)(void) const = 0;
|
||||
|
||||
/*
|
||||
%inline %{
|
||||
struct Funktions {
|
||||
void retByRef(int & (*d)(double)) {}
|
||||
};
|
||||
void byRef(int & (Funktions::*d)(double)) {}
|
||||
%}
|
||||
*/
|
||||
|
||||
%inline %{
|
||||
|
||||
struct Funktions {
|
||||
int addByValue(const int &a, int b) const { return a+b; }
|
||||
int * addByPointer(const int &a, int b) const { static int val; val = a+b; return &val; }
|
||||
int & addByReference(const int &a, int b) const { static int val; val = a+b; return val; }
|
||||
};
|
||||
|
||||
int call1(int (Funktions::*d)(const int &, int) const, int a, int b) { Funktions f; return (f.*d)(a, b); }
|
||||
//int call2(int * (Funktions::*d)(const int &, int) const, int a, int b) { Funktions f; return *(f.*d)(a, b); }
|
||||
//int call3(int & (Funktions::*d)(const int &, int) const, int a, int b) { Funktions f; return (f.*d)(a, b); }
|
||||
%}
|
||||
|
||||
%constant int (Funktions::*ADD_BY_VALUE)(const int &, int) const = &Funktions::addByValue;
|
||||
//%constant int * (Funktions::*ADD_BY_POINTER)(const int &, int) const = &Funktions::addByPointer;
|
||||
//%constant int & (Funktions::*ADD_BY_REFERENCE)(const int &, int) const = &Funktions::addByReference;
|
||||
48
Examples/test-suite/python/member_pointer_const_runme.py
Normal file
48
Examples/test-suite/python/member_pointer_const_runme.py
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
# Example using pointers to member functions
|
||||
|
||||
from member_pointer_const import *
|
||||
|
||||
|
||||
def check(what, expected, actual):
|
||||
if expected != actual:
|
||||
raise RuntimeError(
|
||||
"Failed: ", what, " Expected: ", expected, " Actual: ", actual)
|
||||
|
||||
# Get the pointers
|
||||
|
||||
area_pt = areapt()
|
||||
perim_pt = perimeterpt()
|
||||
|
||||
# Create some objects
|
||||
|
||||
s = Square(10)
|
||||
|
||||
# Do some calculations
|
||||
|
||||
check("Square area ", 100.0, do_op(s, area_pt))
|
||||
check("Square perim", 40.0, do_op(s, perim_pt))
|
||||
|
||||
memberPtr = cvar.areavar
|
||||
memberPtr = cvar.perimetervar
|
||||
|
||||
# Try the variables
|
||||
check("Square area ", 100.0, do_op(s, cvar.areavar))
|
||||
check("Square perim", 40.0, do_op(s, cvar.perimetervar))
|
||||
|
||||
# Modify one of the variables
|
||||
cvar.areavar = perim_pt
|
||||
|
||||
check("Square perimeter", 40.0, do_op(s, cvar.areavar))
|
||||
|
||||
# Try the constants
|
||||
|
||||
memberPtr = AREAPT
|
||||
memberPtr = PERIMPT
|
||||
memberPtr = NULLPT
|
||||
|
||||
check("Square area ", 100.0, do_op(s, AREAPT))
|
||||
check("Square perim", 40.0, do_op(s, PERIMPT))
|
||||
|
||||
check("Add by value", 3, call1(ADD_BY_VALUE, 1, 2))
|
||||
#check("Add by pointer", 7, call2(ADD_BY_POINTER, 3, 4))
|
||||
#check("Add by reference", 11, call3(ADD_BY_REFERENCE, 5, 6))
|
||||
Loading…
Add table
Add a link
Reference in a new issue