new example
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@5444 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
313501d146
commit
307cc5ea90
5 changed files with 182 additions and 0 deletions
19
SWIG/Examples/tcl/mpointer/Makefile
Normal file
19
SWIG/Examples/tcl/mpointer/Makefile
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
CXXSRCS = example.cxx
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS = -lm
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' tcl_cpp
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='mytclsh' INTERFACE='$(INTERFACE)' tclsh_cpp_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile tcl_clean
|
||||
|
||||
check: all
|
||||
48
SWIG/Examples/tcl/mpointer/example.cxx
Normal file
48
SWIG/Examples/tcl/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/tcl/mpointer/example.h
Normal file
50
SWIG/Examples/tcl/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/tcl/mpointer/example.i
Normal file
16
SWIG/Examples/tcl/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;
|
||||
|
||||
49
SWIG/Examples/tcl/mpointer/runme.tcl
Normal file
49
SWIG/Examples/tcl/mpointer/runme.tcl
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
# Example using pointers to member functions
|
||||
|
||||
catch { load ./example[info sharedlibextension] example}
|
||||
|
||||
# Get the pointers
|
||||
|
||||
set area_pt [ areapt ]
|
||||
set perim_pt [ perimeterpt ]
|
||||
|
||||
puts "area_pt = $area_pt"
|
||||
puts "perim_pt = $perim_pt"
|
||||
|
||||
# Create some objects
|
||||
|
||||
set c [Circle -args 4]
|
||||
set s [Square -args 10]
|
||||
|
||||
# Do some calculations
|
||||
|
||||
puts "Circle area = [do_op $c $area_pt]"
|
||||
puts "Circle perim = [do_op $c $perim_pt]"
|
||||
puts "Square area = [do_op $s $area_pt]"
|
||||
puts "Square perim = [do_op $s $perim_pt]"
|
||||
|
||||
puts "areavar = $areavar";
|
||||
puts "perimetervar = $perimetervar";
|
||||
|
||||
# Try the variables
|
||||
puts "Circle area = [do_op $c $areavar]"
|
||||
puts "Circle perim = [do_op $c $perimetervar]"
|
||||
puts "Square area = [do_op $s $areavar]"
|
||||
puts "Square perim = [do_op $s $perimetervar]"
|
||||
|
||||
# Modify one of the variables
|
||||
set areavar $perim_pt
|
||||
|
||||
puts "Circle perimeter = [do_op $c $areavar]"
|
||||
|
||||
# Try the constants
|
||||
|
||||
puts "example.AREAPT = $AREAPT"
|
||||
puts "example.PERIMPT= $PERIMPT"
|
||||
puts "example.NULLPT = $NULLPT"
|
||||
|
||||
puts "Circle area = [do_op $c $AREAPT]"
|
||||
puts "Circle perim = [do_op $c $PERIMPT]"
|
||||
puts "Square area = [do_op $s $AREAPT]"
|
||||
puts "Square perim = [do_op $s $PERIMPT]"
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue