help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Swig works (Re: Octave and databases)


From: Xavier Delacour
Subject: Re: Swig works (Re: Octave and databases)
Date: Thu, 9 Sep 2010 11:42:39 -0700

On Thu, Sep 9, 2010 at 9:43 AM, John W. Eaton <address@hidden> wrote:
> On  8-Sep-2010, Xavier Delacour wrote:
> | More important is the support for handle classes (destructors +
> | copy-by-ref). That is required to use octave classes to wrap C
> | classes.
>
> Exactly what do you mean by copy-by-ref?  I've written a lot of
> wrappers for C and Fortran code and have not needed to pass variables
> by reference or to have destructors.  Can you give an example of why
> these things are needed?

Swig generates oct-file bindings from C and C++ header and "interface"
files [1]. For an interface file such as

%{
class object {
public:
object(const char* str);
void func(int n);
};
%}

>From octave you would expect to be able to do

a = object("hello"); # ptr1 = new object("hello");
b = a; # ptr2 = ptr1;
func(a, 4); # ptr1->func(4);
clear a; #
clear b; # delete ptr2

(the code in comments is what the generated wrapper is executing for you)

If there is no destructor in octave classes, then the generated m-file
class that wraps object will not be able to delete ptr{1,2} when no
more references remain.

The current approach is to make a custom octave_base_value derivative
that tracks and destroys references to C-side instantiations, but that
has various problems and is more complicated.

Support for "handle classes" [2] as in latest matlab would allow above
example to consist of single generated m-file that would define the
octave class named "object", and an oct-file with DEFUN for functions
new_object, delete_object, object_func, etc. Those would be invoked
from the generated m-file.

>
> | Hacks like making static methods and variables appear as global
> | methods and variables are okay. Eg, static methods could be made to
> | appear as class_name_static_method_name() from octave. Static
> | variables are a bit trickier since the wrapper needs to told when
> | assignment happens (so it can assign the C-side variable). xyz_set()
> | and xyz_get() functions can also be generated for static variable
> | access.
>
> Exactly what do you mean by "static methods and variables"?  In what
> language?  Can you give an example of what you are describing above?
> That might help me to better understand the issues you are facing and
> perhaps offer some suggestions.

Static methods and variables in C++ classes that are wrapped by swig.
Eg, to support an interface file like

%{
struct object {
static int n;
static void static_func(const char* str);
};
%}

Xavier

[1] http://swig.org/Doc2.0/SWIG.html#SWIG
[2] http://www.mathworks.com/help/techdoc/matlab_oop/brfylzt-1.html



reply via email to

[Prev in Thread] Current Thread [Next in Thread]