new example
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@5435 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
9aecb608c5
commit
dc3fa7ee39
5 changed files with 185 additions and 0 deletions
20
SWIG/Examples/perl5/mpointer/Makefile
Normal file
20
SWIG/Examples/perl5/mpointer/Makefile
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
TOP = ../..
|
||||||
|
SWIG = $(TOP)/../swig
|
||||||
|
CXXSRCS = example.cxx
|
||||||
|
TARGET = example
|
||||||
|
INTERFACE = example.i
|
||||||
|
LIBS = -lm
|
||||||
|
SWIGOPT = -shadow
|
||||||
|
|
||||||
|
all::
|
||||||
|
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||||
|
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' perl5_cpp
|
||||||
|
|
||||||
|
static::
|
||||||
|
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||||
|
SWIGOPT='$(SWIGOPT)' TARGET='myperl' INTERFACE='$(INTERFACE)' perl5_cpp_static
|
||||||
|
|
||||||
|
clean::
|
||||||
|
$(MAKE) -f $(TOP)/Makefile perl5_clean
|
||||||
|
|
||||||
|
check: all
|
||||||
48
SWIG/Examples/perl5/mpointer/example.cxx
Normal file
48
SWIG/Examples/perl5/mpointer/example.cxx
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
/* File : example.c */
|
||||||
|
|
||||||
|
#include "example.h"
|
||||||
|
#include <math.h>
|
||||||
|
#ifndef M_PI
|
||||||
|
# define M_PI 3.14159265358979323846
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* 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) {
|
||||||
|
return M_PI*radius*radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
double Circle::perimeter(void) {
|
||||||
|
return 2*M_PI*radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
double Square::area(void) {
|
||||||
|
return width*width;
|
||||||
|
}
|
||||||
|
|
||||||
|
double Square::perimeter(void) {
|
||||||
|
return 4*width;
|
||||||
|
}
|
||||||
|
|
||||||
|
double do_op(Shape *s, double (Shape::*m)(void)) {
|
||||||
|
return (s->*m)();
|
||||||
|
}
|
||||||
|
|
||||||
|
double (Shape::*areapt())(void) {
|
||||||
|
return &Shape::area;
|
||||||
|
}
|
||||||
|
|
||||||
|
double (Shape::*perimeterpt())(void) {
|
||||||
|
return &Shape::perimeter;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Member pointer variables */
|
||||||
|
double (Shape::*areavar)(void) = &Shape::area;
|
||||||
|
double (Shape::*perimetervar)(void) = &Shape::perimeter;
|
||||||
|
|
||||||
50
SWIG/Examples/perl5/mpointer/example.h
Normal file
50
SWIG/Examples/perl5/mpointer/example.h
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
/* File : example.h */
|
||||||
|
|
||||||
|
class Shape {
|
||||||
|
public:
|
||||||
|
Shape() {
|
||||||
|
nshapes++;
|
||||||
|
}
|
||||||
|
virtual ~Shape() {
|
||||||
|
nshapes--;
|
||||||
|
};
|
||||||
|
double x, y;
|
||||||
|
double *z;
|
||||||
|
|
||||||
|
void move(double dx, double dy);
|
||||||
|
virtual double area(void) = 0;
|
||||||
|
virtual double perimeter(void) = 0;
|
||||||
|
static int nshapes;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Circle : public Shape {
|
||||||
|
private:
|
||||||
|
double radius;
|
||||||
|
public:
|
||||||
|
Circle(double r) : radius(r) { };
|
||||||
|
virtual double area(void);
|
||||||
|
virtual double perimeter(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
class Square : public Shape {
|
||||||
|
private:
|
||||||
|
double width;
|
||||||
|
public:
|
||||||
|
Square(double w) : width(w) { };
|
||||||
|
virtual double area(void);
|
||||||
|
virtual double perimeter(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
extern double do_op(Shape *s, double (Shape::*m)(void));
|
||||||
|
|
||||||
|
/* Functions that return member pointers */
|
||||||
|
|
||||||
|
extern double (Shape::*areapt())(void);
|
||||||
|
extern double (Shape::*perimeterpt())(void);
|
||||||
|
|
||||||
|
/* Global variables that are member pointers */
|
||||||
|
extern double (Shape::*areavar)(void);
|
||||||
|
extern double (Shape::*perimetervar)(void);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
16
SWIG/Examples/perl5/mpointer/example.i
Normal file
16
SWIG/Examples/perl5/mpointer/example.i
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
/* File : example.i */
|
||||||
|
%module example
|
||||||
|
|
||||||
|
%{
|
||||||
|
#include "example.h"
|
||||||
|
%}
|
||||||
|
|
||||||
|
/* Let's just grab the original header file here */
|
||||||
|
%include "example.h"
|
||||||
|
|
||||||
|
/* Some constants */
|
||||||
|
|
||||||
|
%constant double (Shape::*AREAPT)(void) = &Shape::area;
|
||||||
|
%constant double (Shape::*PERIMPT)(void) = &Shape::perimeter;
|
||||||
|
%constant double (Shape::*NULLPT)(void) = 0;
|
||||||
|
|
||||||
51
SWIG/Examples/perl5/mpointer/runme.pl
Normal file
51
SWIG/Examples/perl5/mpointer/runme.pl
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
# Example using pointers to member functions
|
||||||
|
|
||||||
|
use example;
|
||||||
|
|
||||||
|
# Get the pointers
|
||||||
|
|
||||||
|
$area_pt = example::areapt();
|
||||||
|
$perim_pt = example::perimeterpt();
|
||||||
|
|
||||||
|
print "area_pt =", $area_pt, "\n";
|
||||||
|
print "perim_pt = ", $perim_pt, "\n";
|
||||||
|
|
||||||
|
# Create some objects
|
||||||
|
|
||||||
|
$c = new example::Circle(4);
|
||||||
|
$s = new example::Square(10);
|
||||||
|
|
||||||
|
# Do some calculations
|
||||||
|
|
||||||
|
print "Circle area = ", example::do_op($c,$area_pt), "\n";
|
||||||
|
print "Circle perim = ", example::do_op($c,$perim_pt), "\n";
|
||||||
|
print "Square area = ", example::do_op($s,$area_pt), "\n";
|
||||||
|
print "Square perim = ", example::do_op($s,$perim_pt), "\n";
|
||||||
|
|
||||||
|
print "areavar =", $example::areavar, "\n";
|
||||||
|
print "perimetervar =", $example::perimetervar, "\n";
|
||||||
|
|
||||||
|
# Try the variables
|
||||||
|
print "Circle area = ", example::do_op($c,$example::areavar), "\n";
|
||||||
|
print "Circle perim = ", example::do_op($c,$example::perimetervar), "\n";
|
||||||
|
print "Square area = ", example::do_op($s,$example::areavar), "\n";
|
||||||
|
print "Square perim = ", example::do_op($s,$example::perimetervar), "\n";
|
||||||
|
|
||||||
|
# Modify one of the variables
|
||||||
|
$example::areavar = $perim_pt;
|
||||||
|
|
||||||
|
print "Circle perimeter = ", example::do_op($c,$example::areavar), "\n";
|
||||||
|
|
||||||
|
# Try the constants
|
||||||
|
|
||||||
|
print "example.AREAPT =", $example::AREAPT, "\n";
|
||||||
|
print "example.PERIMPT=", $example::PERIMPT, "\n";
|
||||||
|
print "example.NULLPT =", $example::NULLPT, "\n";
|
||||||
|
|
||||||
|
print "Circle area = ", example::do_op($c,$example::AREAPT), "\n";
|
||||||
|
print "Circle perim = ", example::do_op($c,$example::PERIMPT), "\n";
|
||||||
|
print "Square area = ", example::do_op($s,$example::AREAPT), "\n";
|
||||||
|
print "Square perim = ", example::do_op($s,$example::PERIMPT), "\n";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue