octave-maintainers
[Top][All Lists]
Advanced

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

Re: Easy way to interpret octave_value as XXX?


From: John W. Eaton
Subject: Re: Easy way to interpret octave_value as XXX?
Date: Tue, 18 Dec 2012 10:46:48 -0500

On 17-Dec-2012, Michael Goffioul wrote:

| On Mon, Dec 17, 2012 at 5:43 PM, Rik <address@hidden> wrote:
| 
|     12/17/12
|    
|     John,
|    
|     I have an octave_value that is the input to a DEFUN function.  I have
|     verified that the object is in fact of class "octave_java".  Is there an
|     easy way to interpret the octave_value as being of class octave_java and
|     calling a method from the octave_java class?
|    
|     Or is the real, but longer way, to declare a new function in ov-base.h
|     that
|     performs the interpretation.  And then in the DEFUN function I need to
|     declare a new instance of the octave_java class.
|    
|     octave_java oct_jobj = args(0).octave_java_value ();
|     oct_jobj.method_name ();

I would not expose the representation class as a value.  If you want
to go this route, then look at the way the octave_function_value
method works to return the rep pointer as a pointer to an
octave_function object.

Which functions from the octave_java class do you want to expose?

| The java.cc code used to do something like (see TO_JAVA macro):
| 
| dynamic_cast<octave_java*> ((obj).internal_rep ())
| 
| Not very clean, but it was only used internally anyway. This also returns a
| pointer, not a plain object. Having a method octave_value::octave_java_value()
| returning a plain octave_java is technically possible: the octave_java class
| handles copy, by referencing the underlying Java object.

Right, this is a reasonable way to do the job.

If this comes up often and you would like to avoid the dynamic_cast
everywhere you need to get the rep pointer as a pointer to an
octave_java object instead of as a pointer to an octave_base_value
object then we could provide a function in the octave_value by doing
something like this:

  ov.h:

    octave_java *java_object (bool silent = false) const;

  ov.cc:

    octave_java *
    octave_value::java_object (bool silent) const
    {
      return rep->java_object (silent);
    }

  ov-base.h:

    virtual octave_java *java_object (bool silent);

  ov-base.cc:

    octave_java *
    octave_base_value::java_object (bool silent)
    {
      octave_java *retval = 0;

      if (! silent)
        gripe_wrong_type_arg ("octave_base_value::java_object ()",
                              type_name ());
      return retval;
    }

  ov-java.h:

    octave_java *java_object (bool = false) { return this; }


If there are functions that make sense for the octave_java class that
could make sense for other types of objects, or if you'd like to just
be able to call the functions on an octave_value object directly
instead of first extracting the rep pointer, then we could also
consider adding the functions to the octave_value class.

jwe


reply via email to

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