summaryrefslogtreecommitdiff
path: root/Examples/octave/reference
diff options
context:
space:
mode:
Diffstat (limited to 'Examples/octave/reference')
-rw-r--r--Examples/octave/reference/Makefile21
-rw-r--r--Examples/octave/reference/example.cxx46
-rw-r--r--Examples/octave/reference/example.h26
-rw-r--r--Examples/octave/reference/example.i48
-rw-r--r--Examples/octave/reference/runme.m63
5 files changed, 204 insertions, 0 deletions
diff --git a/Examples/octave/reference/Makefile b/Examples/octave/reference/Makefile
new file mode 100644
index 0000000..2bce9df
--- /dev/null
+++ b/Examples/octave/reference/Makefile
@@ -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
diff --git a/Examples/octave/reference/example.cxx b/Examples/octave/reference/example.cxx
new file mode 100644
index 0000000..8a513bf
--- /dev/null
+++ b/Examples/octave/reference/example.cxx
@@ -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;
+}
+
diff --git a/Examples/octave/reference/example.h b/Examples/octave/reference/example.h
new file mode 100644
index 0000000..4915adb
--- /dev/null
+++ b/Examples/octave/reference/example.h
@@ -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();
+};
+
+
+
+
diff --git a/Examples/octave/reference/example.i b/Examples/octave/reference/example.i
new file mode 100644
index 0000000..8c95b32
--- /dev/null
+++ b/Examples/octave/reference/example.i
@@ -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;
+ }
+ }
+};
+
+
+
+
diff --git a/Examples/octave/reference/runme.m b/Examples/octave/reference/runme.m
new file mode 100644
index 0000000..f59c8eb
--- /dev/null
+++ b/Examples/octave/reference/runme.m
@@ -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
+