Committing R-SWIG
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@9175 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
b4626ccbac
commit
ef80a4f59a
51 changed files with 5154 additions and 9 deletions
18
Examples/r/class/Makefile
Normal file
18
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
Examples/r/class/example.cxx
Normal file
28
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
Examples/r/class/example.h
Normal file
39
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
Examples/r/class/example.i
Normal file
9
Examples/r/class/example.i
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%inline %{
|
||||
#include "example.h"
|
||||
%}
|
||||
%include "example.h"
|
||||
|
||||
|
||||
49
Examples/r/class/runme.R
Normal file
49
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");
|
||||
|
||||
16
Examples/r/simple/Makefile
Normal file
16
Examples/r/simple/Makefile
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
ARGS = SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)'
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile $(ARGS) r
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile $(ARGS) r_clean
|
||||
|
||||
check: all
|
||||
R CMD BATCH runme.R
|
||||
18
Examples/r/simple/example.c
Normal file
18
Examples/r/simple/example.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/* File : example.c */
|
||||
|
||||
/* A global variable */
|
||||
double Foo = 3.0;
|
||||
|
||||
/* Compute the greatest common divisor of positive integers */
|
||||
int gcd(int x, int y) {
|
||||
int g;
|
||||
g = y;
|
||||
while (x > 0) {
|
||||
g = x;
|
||||
x = y % x;
|
||||
y = g;
|
||||
}
|
||||
return g;
|
||||
}
|
||||
|
||||
|
||||
7
Examples/r/simple/example.i
Normal file
7
Examples/r/simple/example.i
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%inline %{
|
||||
extern int gcd(int x, int y);
|
||||
extern double Foo;
|
||||
%}
|
||||
24
Examples/r/simple/runme.R
Normal file
24
Examples/r/simple/runme.R
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# file: runme.R
|
||||
|
||||
dyn.load('example_wrap.so')
|
||||
source('example_wrap.R')
|
||||
cacheMetaData(1)
|
||||
|
||||
# Call our gcd() function
|
||||
|
||||
x <- 42
|
||||
y <- 105
|
||||
g <- gcd(x,y)
|
||||
sprintf("The gcd of %d and %d is %d", x, y, g)
|
||||
|
||||
# Manipulate the Foo global variable
|
||||
|
||||
# Output its current value
|
||||
Foo()
|
||||
|
||||
# Change its value
|
||||
Foo(3.1415926)
|
||||
|
||||
# See if the change took effect
|
||||
Foo()
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue