Initial commit of Octave module.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@10290 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
50b1578e19
commit
393391965c
275 changed files with 14004 additions and 5 deletions
21
Examples/octave/reference/Makefile
Normal file
21
Examples/octave/reference/Makefile
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
CXXSRCS = example.cxx
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS = -lm
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile $(SWIGLIB) CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' octave_cpp
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile $(SWIGLIB) CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myoctave' INTERFACE='$(INTERFACE)' octave_cpp_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile octave_clean
|
||||
rm -f $(TARGET).py
|
||||
|
||||
check: all
|
||||
46
Examples/octave/reference/example.cxx
Normal file
46
Examples/octave/reference/example.cxx
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
/* File : example.cxx */
|
||||
|
||||
/* Deal with Microsoft's attempt at deprecating C standard runtime functions */
|
||||
#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
|
||||
# define _CRT_SECURE_NO_DEPRECATE
|
||||
#endif
|
||||
|
||||
#include "example.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
Vector operator+(const Vector &a, const Vector &b) {
|
||||
Vector r;
|
||||
r.x = a.x + b.x;
|
||||
r.y = a.y + b.y;
|
||||
r.z = a.z + b.z;
|
||||
return r;
|
||||
}
|
||||
|
||||
char *Vector::print() {
|
||||
static char temp[512];
|
||||
sprintf(temp,"Vector %p (%g,%g,%g)", this, x,y,z);
|
||||
return temp;
|
||||
}
|
||||
|
||||
VectorArray::VectorArray(int size) {
|
||||
items = new Vector[size];
|
||||
maxsize = size;
|
||||
}
|
||||
|
||||
VectorArray::~VectorArray() {
|
||||
delete [] items;
|
||||
}
|
||||
|
||||
Vector &VectorArray::operator[](int index) {
|
||||
if ((index < 0) || (index >= maxsize)) {
|
||||
printf("Panic! Array index out of bounds.\n");
|
||||
exit(1);
|
||||
}
|
||||
return items[index];
|
||||
}
|
||||
|
||||
int VectorArray::size() {
|
||||
return maxsize;
|
||||
}
|
||||
|
||||
26
Examples/octave/reference/example.h
Normal file
26
Examples/octave/reference/example.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/* File : example.h */
|
||||
|
||||
class Vector {
|
||||
private:
|
||||
double x,y,z;
|
||||
public:
|
||||
Vector() : x(0), y(0), z(0) { };
|
||||
Vector(double x, double y, double z) : x(x), y(y), z(z) { };
|
||||
friend Vector operator+(const Vector &a, const Vector &b);
|
||||
char *print();
|
||||
};
|
||||
|
||||
class VectorArray {
|
||||
private:
|
||||
Vector *items;
|
||||
int maxsize;
|
||||
public:
|
||||
VectorArray(int maxsize);
|
||||
~VectorArray();
|
||||
Vector &operator[](int);
|
||||
int size();
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
48
Examples/octave/reference/example.i
Normal file
48
Examples/octave/reference/example.i
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
/* File : example.i */
|
||||
|
||||
/* This file has a few "typical" uses of C++ references. */
|
||||
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
%rename(cprint) print;
|
||||
|
||||
class Vector {
|
||||
public:
|
||||
Vector(double x, double y, double z);
|
||||
~Vector();
|
||||
char *print();
|
||||
};
|
||||
|
||||
/* This helper function calls an overloaded operator */
|
||||
%inline %{
|
||||
Vector addv(Vector &a, Vector &b) {
|
||||
return a+b;
|
||||
}
|
||||
%}
|
||||
|
||||
/* Wrapper around an array of vectors class */
|
||||
|
||||
class VectorArray {
|
||||
public:
|
||||
VectorArray(int maxsize);
|
||||
~VectorArray();
|
||||
int size();
|
||||
|
||||
/* This wrapper provides an alternative to the [] operator */
|
||||
%extend {
|
||||
Vector &get(int index) {
|
||||
return (*$self)[index];
|
||||
}
|
||||
void set(int index, Vector &a) {
|
||||
(*$self)[index] = a;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
63
Examples/octave/reference/runme.m
Normal file
63
Examples/octave/reference/runme.m
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
# file: runme.m
|
||||
|
||||
# This file illustrates the manipulation of C++ references in Octave
|
||||
|
||||
example
|
||||
|
||||
# ----- Object creation -----
|
||||
|
||||
printf("Creating some objects:\n");
|
||||
a = example.Vector(3,4,5)
|
||||
b = example.Vector(10,11,12)
|
||||
|
||||
printf(" Created %s\n",a.cprint());
|
||||
printf(" Created %s\n",b.cprint());
|
||||
|
||||
# ----- Call an overloaded operator -----
|
||||
|
||||
# This calls the wrapper we placed around
|
||||
#
|
||||
# operator+(const Vector &a, const Vector &)
|
||||
#
|
||||
# It returns a new allocated object.
|
||||
|
||||
printf("Adding a+b\n");
|
||||
c = example.addv(a,b);
|
||||
printf(" a+b = %s\n", c.cprint());
|
||||
|
||||
clear c
|
||||
|
||||
# ----- Create a vector array -----
|
||||
|
||||
# Note: Using the high-level interface here
|
||||
printf("Creating an array of vectors\n");
|
||||
va = example.VectorArray(10)
|
||||
|
||||
# ----- Set some values in the array -----
|
||||
|
||||
# These operators copy the value of $a and $b to the vector array
|
||||
va.set(0,a);
|
||||
va.set(1,b);
|
||||
|
||||
va.set(2,example.addv(a,b))
|
||||
|
||||
# Get some values from the array
|
||||
|
||||
printf("Getting some array values\n");
|
||||
for i=0:4,
|
||||
printf(" va(%d) = %s\n",i,va.get(i).cprint());
|
||||
end;
|
||||
|
||||
# Watch under resource meter to check on this
|
||||
printf("Making sure we don't leak memory.\n");
|
||||
for i=0:1000000-1,
|
||||
c = va.get(mod(i,10));
|
||||
end
|
||||
|
||||
# ----- Clean up -----
|
||||
printf("Cleaning up\n");
|
||||
|
||||
clear va
|
||||
clear a
|
||||
clear b
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue