Committing R-SWIG
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9175 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
bf06e16ce4
commit
3a821460fe
51 changed files with 5154 additions and 9 deletions
18
SWIG/Examples/r/class/Makefile
Normal file
18
SWIG/Examples/r/class/Makefile
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
CXXSRCS = example.cxx
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS = -lm
|
||||
ARGS = SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)'
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' r_cpp
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile $(ARGS) r_clean
|
||||
|
||||
check: all
|
||||
R CMD BATCH runme.R
|
||||
28
SWIG/Examples/r/class/example.cxx
Normal file
28
SWIG/Examples/r/class/example.cxx
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
#define 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) {
|
||||
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;
|
||||
}
|
||||
39
SWIG/Examples/r/class/example.h
Normal file
39
SWIG/Examples/r/class/example.h
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/* File : example.h */
|
||||
|
||||
class Shape {
|
||||
public:
|
||||
Shape() {
|
||||
nshapes++;
|
||||
}
|
||||
virtual ~Shape() {
|
||||
nshapes--;
|
||||
};
|
||||
double x, y;
|
||||
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);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
9
SWIG/Examples/r/class/example.i
Normal file
9
SWIG/Examples/r/class/example.i
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%inline %{
|
||||
#include "example.h"
|
||||
%}
|
||||
%include "example.h"
|
||||
|
||||
|
||||
49
SWIG/Examples/r/class/runme.R
Normal file
49
SWIG/Examples/r/class/runme.R
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
# This file illustrates the shadow-class C++ interface generated
|
||||
# by SWIG.
|
||||
|
||||
dyn.load('example_wrap.so')
|
||||
source('example_wrap.R')
|
||||
cacheMetaData(1)
|
||||
|
||||
# ----- Object creation -----
|
||||
|
||||
print("Creating some objects:")
|
||||
circle <- Circle(10)
|
||||
print (" Created circle")
|
||||
square <- Square(10)
|
||||
print (" Created square")
|
||||
|
||||
# ----- Access a static member -----
|
||||
|
||||
sprintf("A total of %d shapes were created", Shape_nshapes())
|
||||
|
||||
# ----- Member data access -----
|
||||
|
||||
# Set the location of the object
|
||||
|
||||
circle$x <- 20
|
||||
circle$y <- 30
|
||||
|
||||
square$x <- -10
|
||||
square$y <- 5
|
||||
|
||||
print("Here is their current position:")
|
||||
sprintf(" Circle = (%f, %f)", circle$x,circle$y)
|
||||
sprintf(" Square = (%f, %f)", square$x,square$y)
|
||||
|
||||
# ----- Call some methods -----
|
||||
|
||||
print ("Here are some properties of the shapes:")
|
||||
|
||||
sapply(c(circle, square),
|
||||
function(o) {
|
||||
sprintf(" area = %f perimeter = %f", o$area(), o$perimeter())
|
||||
})
|
||||
|
||||
print("Guess I'll clean up now")
|
||||
delete(circle)
|
||||
delete(square)
|
||||
|
||||
sprintf("%d shapes remain", Shape_nshapes())
|
||||
print ("Goodbye");
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue