From 4d283f59c3315fe129d16b5d08f5d0bf7ea25460 Mon Sep 17 00:00:00 2001
From: Xavier Delacour
%module example
%{
#include "example.h"
%}
int gcd(int x, int y);
extern double Foo; To build an Octave module, run SWIG using the -octave option. The -c++ option is required (for now) as Octave itself is written in C++ and thus the wrapper code must also be.
-
$ swig -octave -c++ example.i
This creates a C/C++ source file example_wrap.cxx. The generated C++ source file contains the low-level wrappers that need to be compiled and linked with the rest of your C/C++ application (in this case, the gcd implementation) to create an extension module. @@ -110,12 +106,10 @@ Octave modules are DLLs/shared objects having the ".oct" suffix. Building an oct file is usually done with the mkoctfile command (either within Octave itself, or from the shell). For example,
-
$ swig -octave -c++ example.i -o example_wrap.cxx $ mkoctfile example_wrap.cxx example.c
where example.c is the file containing the gcd() implementation. @@ -129,22 +123,16 @@ $ mkoctfile example_wrap.cxx example.c mkoctfile will produce example.oct, which contains the compiled extension module. Loading it into Octave is then a matter of invoking
-
octave:1> example
Assuming all goes well, you will be able to do this:
$ octave -q octave:1> example octave:2> example.gcd(4,6) @@ -154,7 +142,6 @@ ans = 3 octave:4> example.cvar.Foo=4; octave:5> example.cvar.Foo ans = 4
$ octave -q
octave:1> example("global")
octave:2> gcd(4,6)
@@ -185,15 +171,12 @@ octave:4> cvar.Foo=4;
octave:5> cvar.Foo
ans = 4
It is also possible to rename the module namespace with an assignment, as in:
-
octave:1> example; octave:2> c=example; octave:3> c.gcd(10,4) ans = 2
All global variables are put into the cvar namespace object. This is accessible either as my_module.cvar, or just cvar (if the module is imported into the global namespace). @@ -201,11 +184,9 @@ All global variables are put into the cvar namespace object. This is accessible
One can also rename it by simple assignment, e.g.,
-
octave:1> some_vars = cvar; -
%module example int fact(int n);
creates a built-in function example.fact(n) that works exactly like you think it does:
-
octave:1> example.fact(4) 24
%module example
extern double Foo;
To expose variables, SWIG actually generates two functions, to get and set the value. In this case, Foo_set and Foo_set would be generated. SWIG then automatically calls these functions when you get and set the variable-- in the former case creating a local copy in the interpreter of the C variables, and in the latter case copying an interpreter variables onto the C variable.
-
octave:1> example; octave:2> c=example.cvar.Foo c = 3 @@ -255,42 +228,35 @@ octave:4> c c = 3 octave:5> example.cvar.Foo ans = 4
If a variable is marked with the %immutable directive then any attempts to set this variable will cause an Octave error. Given a global variable:
-
%module example %immutable; extern double Foo; %mutable;
SWIG will allow the the reading of Foo but when a set attempt is made, an error function will be called.
-
octave:1> example octave:2> example.Foo=4 error: attempt to set immutable member variable error: assignment failed, or no method for `swig_type = scalar' error: evaluating assignment expression near line 2, column 12
It is possible to add new functions or variables to the module. This also allows the user to rename/remove existing functions and constants (but not linked variables, mutable or immutable). Therefore users are recommended to be careful when doing so.
-
octave:1> example; octave:2> example.PI=3.142; octave:3> example.PI ans = 3.1420
%module example
%constant int ICONST=42;
#define SCONST "Hello World"
enum Days{SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY};
This is 'effectively' converted into the following Octave code:
-
example.ICONST=42 example.SCONST="Hello World" example.SUNDAY=0 ....
C/C++ pointers are fully supported by SWIG. Furthermore, SWIG has no problem working with incomplete type information. Given a wrapping of the <file.h> interface: C/C++ pointers are fully supported by SWIG. Furthermore, SWIG has no problem working with incomplete type information. Given a wrapping of the <file.h> interface:
-
%module example FILE *fopen(const char *filename, const char *mode); int fputs(const char *, FILE *); int fclose(FILE *);
When wrapped, you will be able to use the functions in a natural way from Octave. For example:
-
octave:1> example;
octave:2> f=example.fopen("w","junk");
octave:3> example.fputs("Hello world",f);
octave:4> example.fclose(f);
Simply printing the value of a wrapped C++ type will print it's typename. E.g.,
-
octave:1> example;
octave:2> f=example.fopen("junk","w");
octave:3> f
@@ -363,18 +317,15 @@ f =
{
_p_FILE, ptr = 0x9b0cd00
} As the user of the pointer, you are responsible for freeing it, or closing any resources associated with it (just as you would in a C program). This does not apply so strictly to classes and structs (see below).
-
octave:1> example;
octave:2> f=example.fopen("not there","r");
error: value on right hand side of assignment is undefined
error: evaluating assignment expression near line 2, column 2
struct Point{
int x,y;
};
is used as follows:
-
octave:1> example; octave:2> p=example.Point(); @@ -404,7 +352,6 @@ octave:5> p.x, p.y ans = 3 ans = 5
may be used naturally from Octave:
-
a=A(2), b=A(3), c=a+b assert(c.value==5);
Octave operators are mapped in the following way:
-
__brace a{args}
__brace_asgn a{args} = rhs
@@ -510,14 +453,12 @@ __el_mul a .* b
__el_div a ./ b
__el_pow a .^ b
__el_ldiv a .\ b
-__el_and a & b
+__el_and a & b
__el_or a | b
On the C++ side, the default mappings are as follows:
-
%rename(__add) *::operator+; %rename(__add) *::operator+(); @@ -530,7 +471,7 @@ On the C++ side, the default mappings are as follows: %rename(__mod) *::operator%; %rename(__lshift) *::operator<<; %rename(__rshift) *::operator>>; -%rename(__el_and) *::operator&&; +%rename(__el_and) *::operator&&; %rename(__el_or) *::operator||; %rename(__xor) *::operator^; %rename(__invert) *::operator~; @@ -556,7 +497,6 @@ The %extend directive works the same as in other modules.- +You can use it to define special behavior, like for example defining Octave operators not mapped to C++ operators, or defining certain Octave mechanisms such as how an object prints. For example, the octave_value::{is_string,string_value,print} functions are routed to a special method __str that can be defined inside an %extend.
-
-%extend A { string __str() { @@ -566,11 +506,9 @@ string __str() { } }Then in Octave one gets,
-
-octave:1> a=A(4); octave:2> a @@ -580,7 +518,6 @@ octave:3> printf("%s\n",a); octave:4> a.__str() 426.3.13 C++ templates
@@ -607,19 +544,16 @@ Octave has no direct support for object oriented programming, however the swFor example,
-
octave:1> a=subclass(); octave:2> a.my_var = 4; octave:3> a.my_method = @(self) printf("my_var = ",self.my_var); octave:4> a.my_method(); my_var = 4 -
subclass() can also be used to subclass one or more C++ types. Suppose you have an interface defined by
-
%inline {
class A {
@@ -628,16 +562,14 @@ public:
printf("c-side routine called\n");
}
};
-void call_your_method(A& a) {
+void call_your_method(A& a) {
a.my_method();
}
}
Then from Octave you can say:
-
octave:1> B=@() subclass(A(),@my_method); octave:2> function my_method(self) @@ -646,35 +578,29 @@ octave:4> end octave:5> call_your_method(B()); octave-side routine called
or more concisely,
-
octave:1> B=@() subclass(A(),'my_method',@(self) printf("octave-side routine called\n"));
octave:2> call_your_method(B());
octave-side routine called
Note that you have to enable directors via the %feature directive (see other modules for this).
subclass() will accept any number of C++ bases or other subclass()'ed objects, (string,octave_value) pairs, and function_handles. In the first case, these are taken as base classes; in the second case, as named members (either variables or functions, depending on whether the given value is a function handle); in the third case, as member functions whose name is taken from the given function handle. E.g.,
-
octave:1> B=@(some_var=2) subclass(A(),'some_var',some_var,@some_func,'another_func',@(self) do_stuff())
You can also assign non-C++ member variables and functions after construct time. There is no support for non-C++ static members.
There is limited support for explicitly referencing C++ bases. So, in the example above, we could have
-
octave:1> B=@() subclass(A(),@my_method); octave:2> function my_method(self) @@ -685,7 +611,6 @@ octave:6> call_your_method(B()); c-side routine called octave-side routine called
Would produce this behavior in Octave:
-
octave:1> a=A(); A constructing @@ -724,7 +647,6 @@ octave:3> clear a; octave:4> b=4; A destructing
In the case where one wishes for the C++ side to own an object that was created in Octave (especially a Director object), one can use the __disown() method to invert this logic. Then letting the Octave reference count go to zero will not destroy the object, but destroying the object will invalidate the Octave-side object if it still exists (and call destructors of other C++ bases in the case of multiple inheritance/subclass()'ing).
@@ -742,20 +664,16 @@ This is some skeleton support for various STL containers, but this work is not fOctave provides a rich set of classes for dealing with matrices etc. Currently there are no typemaps to deal with those, though such support will be added soon. However, these are relatively straight forward for users to add themselves (see the docs on typemaps). Without much work (a single typemap decl-- say, 5 lines of code in the interface file), it would be possible to have a function
-
double my_det(const double* mat,int m,int n); -
that is accessed from Octave as,
-
octave:1> my_det(rand(4)); ans = -0.18388 -