diff options
author | Xavier Delacour <xavier.delacour@gmail.com> | 2008-03-01 23:35:44 +0000 |
---|---|---|
committer | Xavier Delacour <xavier.delacour@gmail.com> | 2008-03-01 23:35:44 +0000 |
commit | 393391965c4583923d23958a483dabd316d425f6 (patch) | |
tree | a4b0d0884436413c9b36be2e01dc7672813baad9 /Examples/octave | |
parent | 50b1578e19c1fe821697c75ffac87f3624f810a8 (diff) | |
download | swig-393391965c4583923d23958a483dabd316d425f6.tar.gz |
Initial commit of Octave module.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@10290 626c5289-ae23-0410-ae9c-e8d60b6d4f22
Diffstat (limited to 'Examples/octave')
67 files changed, 1822 insertions, 0 deletions
diff --git a/Examples/octave/callback/Makefile b/Examples/octave/callback/Makefile new file mode 100644 index 000000000..2bce9df5d --- /dev/null +++ b/Examples/octave/callback/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/callback/example.cxx b/Examples/octave/callback/example.cxx new file mode 100644 index 000000000..450d75608 --- /dev/null +++ b/Examples/octave/callback/example.cxx @@ -0,0 +1,4 @@ +/* File : example.cxx */ + +#include "example.h" + diff --git a/Examples/octave/callback/example.h b/Examples/octave/callback/example.h new file mode 100644 index 000000000..1a0e8c432 --- /dev/null +++ b/Examples/octave/callback/example.h @@ -0,0 +1,23 @@ +/* File : example.h */ + +#include <cstdio> +#include <iostream> + +class Callback { +public: + virtual ~Callback() { std::cout << "Callback::~Callback()" << std:: endl; } + virtual void run() { std::cout << "Callback::run()" << std::endl; } +}; + + +class Caller { +private: + Callback *_callback; +public: + Caller(): _callback(0) {} + ~Caller() { delCallback(); } + void delCallback() { delete _callback; _callback = 0; } + void setCallback(Callback *cb) { delCallback(); _callback = cb; } + void call() { if (_callback) _callback->run(); } +}; + diff --git a/Examples/octave/callback/example.i b/Examples/octave/callback/example.i new file mode 100644 index 000000000..90beda01a --- /dev/null +++ b/Examples/octave/callback/example.i @@ -0,0 +1,13 @@ +/* File : example.i */ +%module(directors="1") example +%{ +#include "example.h" +%} + +%include "std_string.i" + +/* turn on director wrapping Callback */ +%feature("director") Callback; + +%include "example.h" + diff --git a/Examples/octave/callback/runme.m b/Examples/octave/callback/runme.m new file mode 100644 index 000000000..b87925e3d --- /dev/null +++ b/Examples/octave/callback/runme.m @@ -0,0 +1,63 @@ +# file: runme.m + +# This file illustrates the cross language polymorphism using directors. + +example + +OctCallback=@() subclass(example.Callback(), \ + 'run',@(self) printf("OctCallback.run()\n")); + +# Create an Caller instance + +caller = example.Caller(); + +# Add a simple C++ callback (caller owns the callback, so +# we disown it first) + +printf("Adding and calling a normal C++ callback\n"); +printf("----------------------------------------\n"); + +callback = example.Callback().__disown(); +caller.setCallback(callback); +caller.call(); +caller.delCallback(); + +printf("Adding and calling a Octave callback\n"); +printf("------------------------------------\n"); + +# Add a Octave callback (caller owns the callback, so we +# disown it first by calling __disown). + +caller.setCallback(OctCallback().__disown()) +caller.call(); +caller.delCallback(); + +printf("Adding and calling another Octave callback\n"); +printf("------------------------------------------\n"); + +# Let's do the same but use the weak reference this time. + +callback = OctCallback().__disown(); +caller.setCallback(callback); +caller.call(); +caller.delCallback(); + +# careful-- using callback here may cause problems; octave_swig_type still +# exists, but is holding a destroyed object (the C++ example.Callback). +# to manually drop the octave-side reference, you can use +clear callback; + +# Let's call them directly now + +printf("Calling Octave and C++ callbacks directly\n"); +printf("------------------------------------------\n"); + +a = OctCallback(); +a.run(); +a.Callback.run(); + + +# All done. + +printf("octave exit\n"); + diff --git a/Examples/octave/check.list b/Examples/octave/check.list new file mode 100644 index 000000000..1e6608151 --- /dev/null +++ b/Examples/octave/check.list @@ -0,0 +1,16 @@ +# see top-level Makefile.in +callback +class +constants +contract +enum +extend +funcptr +funcptr2 +functor +operator +pointer +reference +simple +template +variables diff --git a/Examples/octave/class/Makefile b/Examples/octave/class/Makefile new file mode 100644 index 000000000..2bce9df5d --- /dev/null +++ b/Examples/octave/class/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/class/example.cxx b/Examples/octave/class/example.cxx new file mode 100644 index 000000000..1e8e203dd --- /dev/null +++ b/Examples/octave/class/example.cxx @@ -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; +} diff --git a/Examples/octave/class/example.h b/Examples/octave/class/example.h new file mode 100644 index 000000000..46d901361 --- /dev/null +++ b/Examples/octave/class/example.h @@ -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); +}; + + + + + diff --git a/Examples/octave/class/example.i b/Examples/octave/class/example.i new file mode 100644 index 000000000..75700b305 --- /dev/null +++ b/Examples/octave/class/example.i @@ -0,0 +1,10 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +/* Let's just grab the original header file here */ +%include "example.h" + diff --git a/Examples/octave/class/runme.m b/Examples/octave/class/runme.m new file mode 100644 index 000000000..e3c6c0dca --- /dev/null +++ b/Examples/octave/class/runme.m @@ -0,0 +1,52 @@ +# file: runme.m + +# This file illustrates the shadow-class C++ interface generated +# by SWIG. + +example + +# ----- Object creation ----- + +printf("Creating some objects:\n"); +c = example.Circle(10) +s = example.Square(10) + +# ----- Access a static member ----- + +printf("\nA total of %i shapes were created\n", example.Shape.nshapes); + +# ----- Member data access ----- + +# Set the location of the object + +c.x = 20 +c.y = 30 + +s.x = -10 +s.y = 5 + +printf("\nHere is their current position:\n"); +printf(" Circle = (%f, %f)\n",c.x,c.y); +printf(" Square = (%f, %f)\n",s.x,s.y); + +# ----- Call some methods ----- + +printf("\nHere are some properties of the shapes:\n"); +function print_shape(o) + o + printf(" area = %f\n", o.area()); + printf(" perimeter = %f\n", o.perimeter()); +end; +print_shape(c); +print_shape(s); + +printf("\nGuess I'll clean up now\n"); + +# Note: this invokes the virtual destructor +clear c +clear s + +printf("%i shapes remain\n", example.Shape.nshapes); +printf("Goodbye\n"); + + diff --git a/Examples/octave/constants/Makefile b/Examples/octave/constants/Makefile new file mode 100644 index 000000000..4d80c6f19 --- /dev/null +++ b/Examples/octave/constants/Makefile @@ -0,0 +1,21 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = +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/constants/example.i b/Examples/octave/constants/example.i new file mode 100644 index 000000000..4f7b1a4d7 --- /dev/null +++ b/Examples/octave/constants/example.i @@ -0,0 +1,27 @@ +/* File : example.i */ +%module example + +/* A few preprocessor macros */ + +#define ICONST 42 +#define FCONST 2.1828 +#define CCONST 'x' +#define CCONST2 '\n' +#define SCONST "Hello World" +#define SCONST2 "\"Hello World\"" + +/* This should work just fine */ +#define EXPR ICONST + 3*(FCONST) + +/* This shouldn't do anything */ +#define EXTERN extern + +/* Neither should this (BAR isn't defined) */ +#define FOO (ICONST + BAR) + +/* The following directives also produce constants */ + +%constant int iconst = 37; +%constant double fconst = 3.14; + + diff --git a/Examples/octave/constants/runme.m b/Examples/octave/constants/runme.m new file mode 100644 index 000000000..322858734 --- /dev/null +++ b/Examples/octave/constants/runme.m @@ -0,0 +1,29 @@ +# file: runme.m + +example + +printf("ICONST = %i (should be 42)\n", example.ICONST); +printf("FCONST = %f (should be 2.1828)\n", example.FCONST); +printf("CCONST = %s (should be 'x')\n", example.CCONST); +printf("CCONST2 = %s (this should be on a new line)\n", example.CCONST2); +printf("SCONST = %s (should be 'Hello World')\n", example.SCONST); +printf("SCONST2 = %s (should be '\"Hello World\"')\n", example.SCONST2); +printf("EXPR = %f (should be 48.5484)\n", example.EXPR); +printf("iconst = %i (should be 37)\n", example.iconst); +printf("fconst = %f (should be 3.14)\n", example.fconst); + +try + printf("EXTERN = %s (Arg! This shouldn't printf(anything)\n", example.EXTERN); +catch + printf("EXTERN isn't defined (good)\n"); +end_try_catch + +try + printf("FOO = %i (Arg! This shouldn't printf(anything)\n", example.FOO); +catch + printf("FOO isn't defined (good)\n"); +end_try_catch + + + + diff --git a/Examples/octave/contract/Makefile b/Examples/octave/contract/Makefile new file mode 100644 index 000000000..bc73e43e7 --- /dev/null +++ b/Examples/octave/contract/Makefile @@ -0,0 +1,21 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.c +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/contract/example.c b/Examples/octave/contract/example.c new file mode 100644 index 000000000..1a644543f --- /dev/null +++ b/Examples/octave/contract/example.c @@ -0,0 +1,23 @@ +/* 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; +} + +int fact(int n) { + if (n <= 0) return 1; + return n*fact(n-1); +} + + diff --git a/Examples/octave/contract/example.i b/Examples/octave/contract/example.i new file mode 100644 index 000000000..8fd1a80af --- /dev/null +++ b/Examples/octave/contract/example.i @@ -0,0 +1,21 @@ +/* File : example.i */ +%module example + +%contract gcd(int x, int y) { +require: + x >= 0; + y >= 0; +} + +%contract fact(int n) { +require: + n >= 0; +ensure: + fact >= 1; +} + +%inline %{ +extern int gcd(int x, int y); +extern int fact(int n); +extern double Foo; +%} diff --git a/Examples/octave/contract/runme.m b/Examples/octave/contract/runme.m new file mode 100644 index 000000000..62b72320b --- /dev/null +++ b/Examples/octave/contract/runme.m @@ -0,0 +1,22 @@ +# file: runme.m + +example + +# Call our gcd() function + +x = 42; +y = 105; +g = example.gcd(x,y); +printf("The gcd of %d and %d is %d\n",x,y,g); + +# Manipulate the Foo global variable + +# Output its current value +printf("Foo = %f\n", example.cvar.Foo); + +# Change its value +example.cvar.Foo = 3.1415926; + +# See if the change took effect +printf("Foo = %f\n", example.cvar.Foo); + diff --git a/Examples/octave/enum/Makefile b/Examples/octave/enum/Makefile new file mode 100644 index 000000000..2bce9df5d --- /dev/null +++ b/Examples/octave/enum/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/enum/example.cxx b/Examples/octave/enum/example.cxx new file mode 100644 index 000000000..6785e57ac --- /dev/null +++ b/Examples/octave/enum/example.cxx @@ -0,0 +1,37 @@ +/* File : example.c */ + +#include "example.h" +#include <stdio.h> + +void Foo::enum_test(speed s) { + if (s == IMPULSE) { + printf("IMPULSE speed\n"); + } else if (s == WARP) { + printf("WARP speed\n"); + } else if (s == LUDICROUS) { + printf("LUDICROUS speed\n"); + } else { + printf("Unknown speed\n"); + } +} + +void enum_test(color c, Foo::speed s) { + if (c == RED) { + printf("color = RED, "); + } else if (c == BLUE) { + printf("color = BLUE, "); + } else if (c == GREEN) { + printf("color = GREEN, "); + } else { + printf("color = Unknown color!, "); + } + if (s == Foo::IMPULSE) { + printf("speed = IMPULSE speed\n"); + } else if (s == Foo::WARP) { + printf("speed = WARP speed\n"); + } else if (s == Foo::LUDICROUS) { + printf("speed = LUDICROUS speed\n"); + } else { + printf("speed = Unknown speed!\n"); + } +} diff --git a/Examples/octave/enum/example.h b/Examples/octave/enum/example.h new file mode 100644 index 000000000..525d62afc --- /dev/null +++ b/Examples/octave/enum/example.h @@ -0,0 +1,13 @@ +/* File : example.h */ + +enum color { RED, BLUE, GREEN }; + +class Foo { + public: + Foo() { } + enum speed { IMPULSE, WARP, LUDICROUS }; + void enum_test(speed s); +}; + +void enum_test(color c, Foo::speed s); + diff --git a/Examples/octave/enum/example.i b/Examples/octave/enum/example.i new file mode 100644 index 000000000..23ee8a822 --- /dev/null +++ b/Examples/octave/enum/example.i @@ -0,0 +1,11 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +/* Let's just grab the original header file here */ + +%include "example.h" + diff --git a/Examples/octave/enum/runme.m b/Examples/octave/enum/runme.m new file mode 100644 index 000000000..a749e81db --- /dev/null +++ b/Examples/octave/enum/runme.m @@ -0,0 +1,32 @@ +# file: runme.m + +example + +# ----- Object creation ----- + +# Print out the value of some enums +printf("*** color ***\n"); +printf(" RED = %i\n", example.RED); +printf(" BLUE = %i\n", example.BLUE); +printf(" GREEN = %i\n", example.GREEN); + +printf("\n*** Foo::speed ***\n"); +printf(" Foo_IMPULSE = %i\n", example.Foo_IMPULSE); +printf(" Foo_WARP = %i\n", example.Foo_WARP); +printf(" Foo_LUDICROUS = %i\n", example.Foo_LUDICROUS); + +printf("\nTesting use of enums with functions\n"); + +example.enum_test(example.RED, example.Foo_IMPULSE); +example.enum_test(example.BLUE, example.Foo_WARP); +example.enum_test(example.GREEN, example.Foo_LUDICROUS); +example.enum_test(1234,5678) + +printf("\nTesting use of enum with class method\n"); +f = example.Foo(); + +f.enum_test(example.Foo_IMPULSE); +f.enum_test(example.Foo_WARP); +f.enum_test(example.Foo_LUDICROUS); + + diff --git a/Examples/octave/extend/Makefile b/Examples/octave/extend/Makefile new file mode 100644 index 000000000..2bce9df5d --- /dev/null +++ b/Examples/octave/extend/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/extend/example.cxx b/Examples/octave/extend/example.cxx new file mode 100644 index 000000000..450d75608 --- /dev/null +++ b/Examples/octave/extend/example.cxx @@ -0,0 +1,4 @@ +/* File : example.cxx */ + +#include "example.h" + diff --git a/Examples/octave/extend/example.h b/Examples/octave/extend/example.h new file mode 100644 index 000000000..b27ab9711 --- /dev/null +++ b/Examples/octave/extend/example.h @@ -0,0 +1,56 @@ +/* File : example.h */ + +#include <cstdio> +#include <iostream> +#include <vector> +#include <string> +#include <cmath> + +class Employee { +private: + std::string name; +public: + Employee(const char* n): name(n) {} + virtual std::string getTitle() { return getPosition() + " " + getName(); } + virtual std::string getName() { return name; } + virtual std::string getPosition() const { return "Employee"; } + virtual ~Employee() { printf("~Employee() @ %p\n", this); } +}; + + +class Manager: public Employee { +public: + Manager(const char* n): Employee(n) {} + virtual std::string getPosition() const { return "Manager"; } +}; + + +class EmployeeList { + std::vector<Employee*> list; +public: + EmployeeList() { + list.push_back(new Employee("Bob")); + list.push_back(new Employee("Jane")); + list.push_back(new Manager("Ted")); + } + void addEmployee(Employee *p) { + list.push_back(p); + std::cout << "New employee added. Current employees are:" << std::endl; + std::vector<Employee*>::iterator i; + for (i=list.begin(); i!=list.end(); i++) { + std::cout << " " << (*i)->getTitle() << std::endl; + } + } + const Employee *get_item(int i) { + return list[i]; + } + ~EmployeeList() { + std::vector<Employee*>::iterator i; + std::cout << "~EmployeeList, deleting " << list.size() << " employees." << std::endl; + for (i=list.begin(); i!=list.end(); i++) { + delete *i; + } + std::cout << "~EmployeeList empty." << std::endl; + } +}; + diff --git a/Examples/octave/extend/example.i b/Examples/octave/extend/example.i new file mode 100644 index 000000000..c8ec32e09 --- /dev/null +++ b/Examples/octave/extend/example.i @@ -0,0 +1,15 @@ +/* File : example.i */ +%module(directors="1") example +%{ +#include "example.h" +%} + +%include "std_vector.i" +%include "std_string.i" + +/* turn on director wrapping for Manager */ +%feature("director") Employee; +%feature("director") Manager; + +%include "example.h" + diff --git a/Examples/octave/extend/runme.m b/Examples/octave/extend/runme.m new file mode 100644 index 000000000..8301c2282 --- /dev/null +++ b/Examples/octave/extend/runme.m @@ -0,0 +1,74 @@ +# file: runme.m + +# This file illustrates the cross language polymorphism using directors. + +example + + +# CEO class, which overrides Employee::getPosition(). + +CEO=@(name) subclass(example.Manager(name),'getPosition',@(self) "CEO"); + +# Create an instance of our employee extension class, CEO. The calls to +# getName() and getPosition() are standard, the call to getTitle() uses +# the director wrappers to call CEO.getPosition. e = CEO("Alice") + +e = CEO("Alice"); +printf("%s is a %s\n",e.getName(),e.getPosition()); +printf("Just call her \"%s\"\n",e.getTitle()); +printf("----------------------\n"); + + +# Create a new EmployeeList instance. This class does not have a C++ +# director wrapper, but can be used freely with other classes that do. + +list = example.EmployeeList(); + +# EmployeeList owns its items, so we must surrender ownership of objects +# we add. This involves first calling the __disown__ method to tell the +# C++ director to start reference counting. We reassign the resulting +# weakref.proxy to e so that no hard references remain. This can also be +# done when the object is constructed, as in: e = +# CEO("Alice").__disown() + +e = e.__disown(); +list.addEmployee(e); +printf("----------------------\n"); + +# Now we access the first four items in list (three are C++ objects that +# EmployeeList's constructor adds, the last is our CEO). The virtual +# methods of all these instances are treated the same. For items 0, 1, and +# 2, both all methods resolve in C++. For item 3, our CEO, getTitle calls +# getPosition which resolves in Octave. The call to getPosition is +# slightly different, however, from the e.getPosition() call above, since +# now the object reference has been "laundered" by passing through +# EmployeeList as an Employee*. Previously, Octave resolved the call +# immediately in CEO, but now Octave thinks the object is an instance of +# class Employee (actually EmployeePtr). So the call passes through the +# Employee shadow class and on to the C wrappers and C++ director, +# eventually ending up back at the CEO implementation of getPosition(). +# The call to getTitle() for item 3 runs the C++ Employee::getTitle() +# method, which in turn calls getPosition(). This virtual method call +# passes down through the C++ director class to the Octave implementation +# in CEO. All this routing takes place transparently. + +printf("(position, title) for items 0-3:\n"); +for i=0:3, + printf(" %s, \"%s\"\n",list.get_item(i).getPosition(), list.get_item(i).getTitle()); +endfor +printf("----------------------\n"); + +# Time to delete the EmployeeList, which will delete all the Employee* +# items it contains. The last item is our CEO, which gets destroyed as its +# reference count goes to zero. The Octave destructor runs, and is still +# able to call self.getName() since the underlying C++ object still +# exists. After this destructor runs the remaining C++ destructors run as +# usual to destroy the object. + +clear list; +printf("----------------------\n"); + +# All done. + +printf("octave exit\n"); + diff --git a/Examples/octave/funcptr/Makefile b/Examples/octave/funcptr/Makefile new file mode 100644 index 000000000..bc73e43e7 --- /dev/null +++ b/Examples/octave/funcptr/Makefile @@ -0,0 +1,21 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.c +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/funcptr/example.c b/Examples/octave/funcptr/example.c new file mode 100644 index 000000000..5c4a3dabf --- /dev/null +++ b/Examples/octave/funcptr/example.c @@ -0,0 +1,19 @@ +/* File : example.c */ + +int do_op(int a, int b, int (*op)(int,int)) { + return (*op)(a,b); +} + +int add(int a, int b) { + return a+b; +} + +int sub(int a, int b) { + return a-b; +} + +int mul(int a, int b) { + return a*b; +} + +int (*funcvar)(int,int) = add; diff --git a/Examples/octave/funcptr/example.h b/Examples/octave/funcptr/example.h new file mode 100644 index 000000000..9936e24fc --- /dev/null +++ b/Examples/octave/funcptr/example.h @@ -0,0 +1,9 @@ +/* file: example.h */ + +extern int do_op(int,int, int (*op)(int,int)); +extern int add(int,int); +extern int sub(int,int); +extern int mul(int,int); + +extern int (*funcvar)(int,int); + diff --git a/Examples/octave/funcptr/example.i b/Examples/octave/funcptr/example.i new file mode 100644 index 000000000..8b3bef678 --- /dev/null +++ b/Examples/octave/funcptr/example.i @@ -0,0 +1,16 @@ +/* File : example.i */ +%module example +%{ +#include "example.h" +%} + +/* Wrap a function taking a pointer to a function */ +extern int do_op(int a, int b, int (*op)(int, int)); + +/* Now install a bunch of "ops" as constants */ +%constant int (*ADD)(int,int) = add; +%constant int (*SUB)(int,int) = sub; +%constant int (*MUL)(int,int) = mul; + +extern int (*funcvar)(int,int); + diff --git a/Examples/octave/funcptr/runme.m b/Examples/octave/funcptr/runme.m new file mode 100644 index 000000000..455311c16 --- /dev/null +++ b/Examples/octave/funcptr/runme.m @@ -0,0 +1,21 @@ +# file: runme.m + +example + +a = 37 +b = 42 + +# Now call our C function with a bunch of callbacks + +printf("Trying some C callback functions\n"); +printf(" a = %i\n", a); +printf(" b = %i\n", b); +printf(" ADD(a,b) = %i\n", example.do_op(a,b,example.ADD)); +printf(" SUB(a,b) = %i\n", example.do_op(a,b,example.SUB)); +printf(" MUL(a,b) = %i\n", example.do_op(a,b,example.MUL)); + +printf("Here is what the C callback function objects look like in Octave\n"); +example.ADD +example.SUB +example.MUL + diff --git a/Examples/octave/funcptr2/Makefile b/Examples/octave/funcptr2/Makefile new file mode 100644 index 000000000..bc73e43e7 --- /dev/null +++ b/Examples/octave/funcptr2/Makefile @@ -0,0 +1,21 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.c +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/funcptr2/example.c b/Examples/octave/funcptr2/example.c new file mode 100644 index 000000000..5c4a3dabf --- /dev/null +++ b/Examples/octave/funcptr2/example.c @@ -0,0 +1,19 @@ +/* File : example.c */ + +int do_op(int a, int b, int (*op)(int,int)) { + return (*op)(a,b); +} + +int add(int a, int b) { + return a+b; +} + +int sub(int a, int b) { + return a-b; +} + +int mul(int a, int b) { + return a*b; +} + +int (*funcvar)(int,int) = add; diff --git a/Examples/octave/funcptr2/example.h b/Examples/octave/funcptr2/example.h new file mode 100644 index 000000000..9936e24fc --- /dev/null +++ b/Examples/octave/funcptr2/example.h @@ -0,0 +1,9 @@ +/* file: example.h */ + +extern int do_op(int,int, int (*op)(int,int)); +extern int add(int,int); +extern int sub(int,int); +extern int mul(int,int); + +extern int (*funcvar)(int,int); + diff --git a/Examples/octave/funcptr2/example.i b/Examples/octave/funcptr2/example.i new file mode 100644 index 000000000..681775a3e --- /dev/null +++ b/Examples/octave/funcptr2/example.i @@ -0,0 +1,18 @@ +/* File : example.i */ +%module example +%{ +#include "example.h" +%} + +/* Wrap a function taking a pointer to a function */ +extern int do_op(int a, int b, int (*op)(int, int)); + +/* Now install a bunch of "ops" as constants */ +%callback("%(upper)s"); +int add(int, int); +int sub(int, int); +int mul(int, int); +%nocallback; + +extern int (*funcvar)(int,int); + diff --git a/Examples/octave/funcptr2/runme.m b/Examples/octave/funcptr2/runme.m new file mode 100644 index 000000000..1d3d8f73a --- /dev/null +++ b/Examples/octave/funcptr2/runme.m @@ -0,0 +1,24 @@ +# file: runme.m + +example + +a = 37 +b = 42 + +# Now call our C function with a bunch of callbacks + +printf("Trying some C callback functions\n"); +printf(" a = %i\n", a); +printf(" b = %i\n", b); +printf(" ADD(a,b) = %i\n", example.do_op(a,b,example.ADD)); +printf(" SUB(a,b) = %i\n", example.do_op(a,b,example.SUB)); +printf(" MUL(a,b) = %i\n", example.do_op(a,b,example.MUL)); + +printf("Here is what the C callback function objects look like in Octave\n"); +example.ADD +example.SUB +example.MUL + +printf("Call the functions directly...\n"); +printf(" add(a,b) = %i\n", example.add(a,b)); +printf(" sub(a,b) = %i\n", example.sub(a,b)); diff --git a/Examples/octave/functor/Makefile b/Examples/octave/functor/Makefile new file mode 100644 index 000000000..d910de530 --- /dev/null +++ b/Examples/octave/functor/Makefile @@ -0,0 +1,21 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = +TARGET = example +INTERFACE = example.i +LIBS = -lm +SWIGOPT = + +all:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' octave_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + 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/functor/example.i b/Examples/octave/functor/example.i new file mode 100644 index 000000000..2fd38176f --- /dev/null +++ b/Examples/octave/functor/example.i @@ -0,0 +1,29 @@ +/* File : example.i */ +%module example + + +%inline %{ +// From B. Strousjoup, "The C++ Programming Language, Third Edition", p. 514 +template<class T> class Sum { + T res; +public: + Sum(T i = 0) : res(i) { } + void operator() (T x) { res += x; } + T result() const { return res; } +}; + +%} + +// Rename the application operator to __call__ for python. +// Note: this is normally automatic, but if you had to do it yourself +// you would use this directive: +// +// %rename(__call__) *::operator(); + +// Instantiate a few versions +%template(intSum) Sum<int>; +%template(doubleSum) Sum<double>; + + + + diff --git a/Examples/octave/functor/runme.m b/Examples/octave/functor/runme.m new file mode 100644 index 000000000..65dabcc91 --- /dev/null +++ b/Examples/octave/functor/runme.m @@ -0,0 +1,18 @@ +# Operator overloading example +example + +a = example.intSum(0); +b = example.doubleSum(100.0); + +# Use the objects. They should be callable just like a normal +# python function. + +for i=0:100-1, + a(i); # Note: function call + b(sqrt(i)); # Note: function call +endfor + +a.result() +b.result() + + diff --git a/Examples/octave/operator/Makefile b/Examples/octave/operator/Makefile new file mode 100644 index 000000000..6e625d538 --- /dev/null +++ b/Examples/octave/operator/Makefile @@ -0,0 +1,21 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = +TARGET = example +INTERFACE = example.i +LIBS = -lm +SWIGOPT = + +all:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' octave_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='myoctave' INTERFACE='$(INTERFACE)' octave_cpp_static + +clean:: + $(MAKE) -f $(TOP)/Makefile octave_clean + rm -f $(TARGET).m + +check: all diff --git a/Examples/octave/operator/example.h b/Examples/octave/operator/example.h new file mode 100644 index 000000000..526f262b9 --- /dev/null +++ b/Examples/octave/operator/example.h @@ -0,0 +1,36 @@ +/* File : example.h */ +#include <math.h> + +class ComplexVal { +private: + double rpart, ipart; +public: + ComplexVal(double r = 0, double i = 0) : rpart(r), ipart(i) { } + ComplexVal(const ComplexVal &c) : rpart(c.rpart), ipart(c.ipart) { } + ComplexVal &operator=(const ComplexVal &c) { + rpart = c.rpart; + ipart = c.ipart; + return *this; + } + ComplexVal operator+(const ComplexVal &c) const { + return ComplexVal(rpart+c.rpart, ipart+c.ipart); + } + ComplexVal operator-(const ComplexVal &c) const { + return ComplexVal(rpart-c.rpart, ipart-c.ipart); + } + ComplexVal operator*(const ComplexVal &c) const { + return ComplexVal(rpart*c.rpart - ipart*c.ipart, + rpart*c.ipart + c.rpart*ipart); + } + ComplexVal operator-() const { + return ComplexVal(-rpart, -ipart); + } + + double re() const { return rpart; } + double im() const { return ipart; } +}; + + + + + diff --git a/Examples/octave/operator/example.i b/Examples/octave/operator/example.i new file mode 100644 index 000000000..d4bba5c53 --- /dev/null +++ b/Examples/octave/operator/example.i @@ -0,0 +1,24 @@ +/* File : example.i */ +%module example +#pragma SWIG nowarn=SWIGWARN_IGNORE_OPERATOR_EQ +%{ +#include "example.h" +%} + +/* Now grab the original header file */ +%include "example.h" + +/* An output method that turns a complex into a short string */ +%extend ComplexVal { + char *__str() { + static char temp[512]; + sprintf(temp,"(%g,%g)", $self->re(), $self->im()); + return temp; + } + + ComplexVal __paren(int j) { + return ComplexVal($self->re()*j,$self->im()*j); + } +}; + + diff --git a/Examples/octave/operator/runme.m b/Examples/octave/operator/runme.m new file mode 100644 index 000000000..e0f590694 --- /dev/null +++ b/Examples/octave/operator/runme.m @@ -0,0 +1,24 @@ +# Operator overloading example +example + +a = example.ComplexVal(2,3); +b = example.ComplexVal(-5,10); + +printf("a = %s\n",a); +printf("b = %s\n",b); + +c = a + b; +printf("c = %s\n",c); +printf("a*b = %s\n",a*b); +printf("a-c = %s\n",a-c); + +e = example.ComplexVal(a-c); +printf("e = %s\n",e); + +# Big expression +f = ((a+b)*(c+b*e)) + (-a); +printf("f = %s\n",f); + +# paren overloading +printf("a(3)= %s\n",a(3)); + diff --git a/Examples/octave/pointer/Makefile b/Examples/octave/pointer/Makefile new file mode 100644 index 000000000..bc73e43e7 --- /dev/null +++ b/Examples/octave/pointer/Makefile @@ -0,0 +1,21 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.c +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/pointer/example.c b/Examples/octave/pointer/example.c new file mode 100644 index 000000000..b877d9a5b --- /dev/null +++ b/Examples/octave/pointer/example.c @@ -0,0 +1,16 @@ +/* File : example.c */ + +void add(int *x, int *y, int *result) { + *result = *x + *y; +} + +void sub(int *x, int *y, int *result) { + *result = *x - *y; +} + +int divide(int n, int d, int *r) { + int q; + q = n/d; + *r = n - q*d; + return q; +} diff --git a/Examples/octave/pointer/example.i b/Examples/octave/pointer/example.i new file mode 100644 index 000000000..a8ac79499 --- /dev/null +++ b/Examples/octave/pointer/example.i @@ -0,0 +1,30 @@ +/* File : example.i */ +%module example + +%{ +extern void add(int *, int *, int *); +extern void sub(int *, int *, int *); +extern int divide(int, int, int *); +%} + +/* This example illustrates a couple of different techniques + for manipulating C pointers */ + +/* First we'll use the pointer library */ +extern void add(int *x, int *y, int *result); +%include cpointer.i +%pointer_functions(int, intp); + +/* Next we'll use some typemaps */ + +%include typemaps.i +extern void sub(int *INPUT, int *INPUT, int *OUTPUT); + +/* Next we'll use typemaps and the %apply directive */ + +%apply int *OUTPUT { int *r }; +extern int divide(int n, int d, int *r); + + + + diff --git a/Examples/octave/pointer/runme.m b/Examples/octave/pointer/runme.m new file mode 100644 index 000000000..c36df7270 --- /dev/null +++ b/Examples/octave/pointer/runme.m @@ -0,0 +1,42 @@ +# file: runme.m + +example; + +# First create some objects using the pointer library. +printf("Testing the pointer library\n"); +a = example.new_intp(); +b = example.new_intp(); +c = example.new_intp(); +example.intp_assign(a,37); +example.intp_assign(b,42); + +a,b,c + +# Call the add() function with some pointers +example.add(a,b,c); + +# Now get the result +r = example.intp_value(c); +printf(" 37 + 42 = %i\n",r); + +# Clean up the pointers +example.delete_intp(a); +example.delete_intp(b); +example.delete_intp(c); + +# Now try the typemap library +# This should be much easier. Now how it is no longer +# necessary to manufacture pointers. + +printf("Trying the typemap library\n"); +r = example.sub(37,42); +printf(" 37 - 42 = %i\n",r); + +# Now try the version with multiple return values + +printf("Testing multiple return values\n"); +[q,r] = example.divide(42,37); +printf(" 42/37 = %d remainder %d\n",q,r); + + + diff --git a/Examples/octave/reference/Makefile b/Examples/octave/reference/Makefile new file mode 100644 index 000000000..2bce9df5d --- /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 000000000..8a513bf49 --- /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 000000000..4915adb1b --- /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 000000000..8c95b3213 --- /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 000000000..f59c8eb7d --- /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 + diff --git a/Examples/octave/simple/Makefile b/Examples/octave/simple/Makefile new file mode 100644 index 000000000..bc73e43e7 --- /dev/null +++ b/Examples/octave/simple/Makefile @@ -0,0 +1,21 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.c +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/simple/example.c b/Examples/octave/simple/example.c new file mode 100644 index 000000000..1c2af789c --- /dev/null +++ b/Examples/octave/simple/example.c @@ -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; +} + + diff --git a/Examples/octave/simple/example.i b/Examples/octave/simple/example.i new file mode 100644 index 000000000..24093b9bf --- /dev/null +++ b/Examples/octave/simple/example.i @@ -0,0 +1,7 @@ +/* File : example.i */ +%module example + +%inline %{ +extern int gcd(int x, int y); +extern double Foo; +%} diff --git a/Examples/octave/simple/runme.m b/Examples/octave/simple/runme.m new file mode 100644 index 000000000..8dc5eaa58 --- /dev/null +++ b/Examples/octave/simple/runme.m @@ -0,0 +1,22 @@ +# file: runme.m + +example + +# Call our gcd() function + +x = 42 +y = 105 +g = example.gcd(x,y) +printf("The gcd of %d and %d is %d\n",x,y,g); + +# Manipulate the Foo global variable + +# Output its current value +example.cvar.Foo + +# Change its value +example.cvar.Foo = 3.1415926 + +# See if the change took effect +printf("Foo = %f\n", example.cvar.Foo); + diff --git a/Examples/octave/template/Makefile b/Examples/octave/template/Makefile new file mode 100644 index 000000000..3d7445a66 --- /dev/null +++ b/Examples/octave/template/Makefile @@ -0,0 +1,21 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = +TARGET = example +INTERFACE = example.i +LIBS = -lm +SWIGOPT = + +all:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' octave_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile 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/template/example.h b/Examples/octave/template/example.h new file mode 100644 index 000000000..7401df650 --- /dev/null +++ b/Examples/octave/template/example.h @@ -0,0 +1,32 @@ +/* File : example.h */ + +// Some template definitions + +template<class T> T max(T a, T b) { return a>b ? a : b; } + +template<class T> class vector { + T *v; + int sz; + public: + vector(int _sz) { + v = new T[_sz]; + sz = _sz; + } + T &get(int index) { + return v[index]; + } + void set(int index, T &val) { + v[index] = val; + } +#ifdef SWIG + %extend { + T getitem(int index) { + return $self->get(index); + } + void setitem(int index, T val) { + $self->set(index,val); + } + } +#endif +}; + diff --git a/Examples/octave/template/example.i b/Examples/octave/template/example.i new file mode 100644 index 000000000..8f94c4da1 --- /dev/null +++ b/Examples/octave/template/example.i @@ -0,0 +1,17 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +/* Let's just grab the original header file here */ +%include "example.h" + +/* Now instantiate some specific template declarations */ + +%template(maxint) max<int>; +%template(maxdouble) max<double>; +%template(vecint) vector<int>; +%template(vecdouble) vector<double>; + diff --git a/Examples/octave/template/runme.m b/Examples/octave/template/runme.m new file mode 100644 index 000000000..a9891d459 --- /dev/null +++ b/Examples/octave/template/runme.m @@ -0,0 +1,37 @@ +# file: runme.m + +example + +# Call some templated functions +example.maxint(3,7) +example.maxdouble(3.14,2.18) + +# Create some class + +iv = example.vecint(100) +dv = example.vecdouble(1000) + +for i=0:99, + iv.setitem(i,2*i); +end + +for i=0:999, + dv.setitem(i, 1.0/(i+1)); +end; + +sum = 0; +for i=0:99 + sum = sum + iv.getitem(i); +end +sum + +sum = 0.0; +for i=0:999, + sum = sum + dv.getitem(i); +end +sum + +clear iv +clear dv + + diff --git a/Examples/octave/variables/Makefile b/Examples/octave/variables/Makefile new file mode 100644 index 000000000..9164a1ca5 --- /dev/null +++ b/Examples/octave/variables/Makefile @@ -0,0 +1,21 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.c +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).m + +check: all diff --git a/Examples/octave/variables/example.c b/Examples/octave/variables/example.c new file mode 100644 index 000000000..aa4ffe9b3 --- /dev/null +++ b/Examples/octave/variables/example.c @@ -0,0 +1,91 @@ +/* File : example.c */ + +/* I'm a file containing some C global variables */ + +/* 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 <stdio.h> +#include <stdlib.h> +#include "example.h" + +int ivar = 0; +short svar = 0; +long lvar = 0; +unsigned int uivar = 0; +unsigned short usvar = 0; +unsigned long ulvar = 0; +signed char scvar = 0; +unsigned char ucvar = 0; +char cvar = 0; +float fvar = 0; +double dvar = 0; +char *strvar = 0; +const char cstrvar[] = "Goodbye"; +int *iptrvar = 0; +char name[256] = "Dave"; +char path[256] = "/home/beazley"; + + +/* Global variables involving a structure */ +Point *ptptr = 0; +Point pt = { 10, 20 }; + +/* A variable that we will make read-only in the interface */ +int status = 1; + +/* A debugging function to print out their values */ + +void print_vars() { + printf("ivar = %d\n", ivar); + printf("svar = %d\n", svar); + printf("lvar = %ld\n", lvar); + printf("uivar = %u\n", uivar); + printf("usvar = %u\n", usvar); + printf("ulvar = %lu\n", ulvar); + printf("scvar = %d\n", scvar); + printf("ucvar = %u\n", ucvar); + printf("fvar = %g\n", fvar); + printf("dvar = %g\n", dvar); + printf("cvar = %c\n", cvar); + printf("strvar = %s\n", strvar ? strvar : "(null)"); + printf("cstrvar = %s\n", cstrvar ? cstrvar : "(null)"); + printf("iptrvar = %p\n", iptrvar); + printf("name = %s\n", name); + printf("ptptr = %p (%d, %d)\n", ptptr, ptptr ? ptptr->x : 0, ptptr ? ptptr->y : 0); + printf("pt = (%d, %d)\n", pt.x, pt.y); + printf("status = %d\n", status); +} + +/* A function to create an integer (to test iptrvar) */ + +int *new_int(int value) { + int *ip = (int *) malloc(sizeof(int)); + *ip = value; + return ip; +} + +/* A function to create a point */ + +Point *new_Point(int x, int y) { + Point *p = (Point *) malloc(sizeof(Point)); + p->x = x; + p->y = y; + return p; +} + +char * Point_print(Point *p) { + static char buffer[256]; + if (p) { + sprintf(buffer,"(%d,%d)", p->x,p->y); + } else { + sprintf(buffer,"null"); + } + return buffer; +} + +void pt_print() { + printf("(%d, %d)\n", pt.x, pt.y); +} diff --git a/Examples/octave/variables/example.h b/Examples/octave/variables/example.h new file mode 100644 index 000000000..0f7e89594 --- /dev/null +++ b/Examples/octave/variables/example.h @@ -0,0 +1,6 @@ +/* File: example.h */ + +typedef struct { + int x,y; +} Point; + diff --git a/Examples/octave/variables/example.i b/Examples/octave/variables/example.i new file mode 100644 index 000000000..639b6c704 --- /dev/null +++ b/Examples/octave/variables/example.i @@ -0,0 +1,53 @@ +/* File : example.i */ +%module example +%{ +#include "example.h" +%} + +#pragma SWIG nowarn=SWIGWARN_TYPEMAP_SWIGTYPELEAK + +/* Some global variable declarations */ +%inline %{ + extern "C" { + extern int ivar; + extern short svar; + extern long lvar; + extern unsigned int uivar; + extern unsigned short usvar; + extern unsigned long ulvar; + extern signed char scvar; + extern unsigned char ucvar; + extern char cvar; + extern float fvar; + extern double dvar; + extern char *strvar; + // extern const char cstrvar[]; + extern int *iptrvar; + extern char name[256]; + + extern Point *ptptr; + extern Point pt; + } +%} + + +/* Some read-only variables */ + +%immutable; + +%inline %{ +extern int status; +extern char path[256]; +%} + +%mutable; + +/* Some helper functions to make it easier to test */ +%inline %{ +extern void print_vars(); +extern int *new_int(int value); +extern Point *new_Point(int x, int y); +extern char *Point_print(Point *p); +extern void pt_print(); +%} + diff --git a/Examples/octave/variables/runme.m b/Examples/octave/variables/runme.m new file mode 100644 index 000000000..db88b18b0 --- /dev/null +++ b/Examples/octave/variables/runme.m @@ -0,0 +1,75 @@ +# file: runme.m + +example + +# Try to set the values of some global variables + +example.cvar.ivar = 42; +example.cvar.svar = -31000; +example.cvar.lvar = 65537; +example.cvar.uivar = 123456; +example.cvar.usvar = 61000; +example.cvar.ulvar = 654321; +example.cvar.scvar = -13; +example.cvar.ucvar = 251; +example.cvar.cvar = "S"; +example.cvar.fvar = 3.14159; +example.cvar.dvar = 2.1828; +example.cvar.strvar = "Hello World"; +example.cvar.iptrvar= example.new_int(37); +example.cvar.ptptr = example.new_Point(37,42); +example.cvar.name = "Bill"; + +# Now print out the values of the variables + +printf("Variables (values printed from Octave)\n"); + +printf("ivar = %i\n", example.cvar.ivar); +printf("svar = %i\n", example.cvar.svar); +printf("lvar = %i\n", example.cvar.lvar); +printf("uivar = %i\n", example.cvar.uivar); +printf("usvar = %i\n", example.cvar.usvar); +printf("ulvar = %i\n", example.cvar.ulvar); +printf("scvar = %i\n", example.cvar.scvar); +printf("ucvar = %i\n", example.cvar.ucvar); +printf("fvar = %i\n", example.cvar.fvar); +printf("dvar = %i\n", example.cvar.dvar); +printf("cvar = %s\n", example.cvar.cvar); +printf("strvar = %s\n", example.cvar.strvar); +#printf("cstrvar = %s\n", example.cvar.cstrvar); +example.cvar.iptrvar +printf("name = %i\n", example.cvar.name); +printf("ptptr = %s\n", example.Point_print(example.cvar.ptptr)); +#printf("pt = %s\n", example.cvar.Point_print(example.cvar.pt)); + +printf("\nVariables (values printed from C)\n"); + +example.print_vars(); + +printf("\nNow I'm going to try and modify some read only variables\n"); + +printf(" Tring to set 'path'\n"); +try + example.cvar.path = "Whoa!"; + printf("Hey, what's going on?!?! This shouldn't work\n"); +catch + printf("Good.\n"); +end_try_catch + +printf(" Trying to set 'status'\n"); +try + example.cvar.status = 0; + printf("Hey, what's going on?!?! This shouldn't work\n"); +catch + printf("Good.\n"); +end_try_catch + + +printf("\nI'm going to try and update a structure variable.\n"); + +example.cvar.pt = example.cvar.ptptr; + +printf("The new value is %s\n", example.Point_print(example.cvar.pt)); + + + |