help-octave
[Top][All Lists]
Advanced

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

Re: Are functional objects (functors/closures) possible?


From: Brett Viren
Subject: Re: Are functional objects (functors/closures) possible?
Date: Fri, 22 Nov 2002 12:55:57 -0500

John W. Eaton writes:
 > On 21-Nov-2002, Brett Viren <address@hidden> wrote:
 > 
 > | I am using Octave 2.1.36 for the "lsode" ODE solver.  The ODE I want
 > | to solve is a function of many parameters.  Is there some way to pass
 > | in these parameters to the function to be used durring the "lsode"
 > | running?
 > | 
 > | The only thing I can think of is to use globals.
 > 
 > To me, that seems to be a reasonable way to solve this problem.  If
 > many global variables bother you, you can pack all the data in a
 > structure or cell array (in recent snapshot versions of Octave, at
 > least).

Yes, I agree.  And, in the end, this is what I have done.  

Globals are indeed okay to use in my case as my function doesn't need
to recursively call itself during the "lsode" solving.  I guess I just
shy away from globals too much....


 > | If octave doesn't support this directly, is there a way to use C++
 > | functional objects (ie, objects with an operator()()) through writing
 > | a dynamically loaded .oct file?
 > 
 > Sorry, I'm not following what you want.  Can you be more specific
 > about what it is you want and how it would help you?

Sorry for being unclear.  I'm happy with the simple solution of using
globals, but for the sake of conversation, I'll try to explain:


I was asking if there was a way to access arbitrary C++ object's
methods as an octave functions.  I guess, in a sense, asking if there
is a way to represent C++ objects as octave objects (if there are even
such thing(?)).

I've learned more about the octave/C++ interface since my first
posting, and I think the answer is probably "no".  

As I see things now, I can access a method from a single C++ object by
holding it in a global pointer and arrange to have the method called
in association with an octave function (via DEFUN_DLD).  This,
essentially, uses the scope of a C++ file to create a singleton octave
object.

However, to be able to create multiple instances of this object and
have their methods associate with octave functions would need the
ability to "call" the CPP macro DEFUN_DLD() from a C++ function.
Maybe by unraveling what DEFUN_DLD() does this is possible.


BTW, there is one addition to this specific problem that would allow
for people to use object-oriented code and that is to add the ability
to pass arbitrary data to "lsode" that is inturn passed on to the
function.  The interface might look like:


my_obj = MyClass_new();
MyClass_set_parameter1(my_obj,42);
y = lsode ("MyClass_some_method", x0, t, my_obj);

Where "MyClass_some_method" is then allowed to be defined in C++ as
something like:
DEFUN_DLD(MyClass_some_method,args,nargout,"MyClass::some_method(double,double)")
{
        MyClass* mc = 0;
        if (args.length() ==  3) {
                mc = (MyClass)args(2).int_value();
        }               
        else return octave_value();

        return mc->some_method(args(0).double_value(),
                               args(1).double_value());
}

And "MyClass_new" and "MyClass_set_parameter1" have similar glue code.

-Brett.



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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