help-octave
[Top][All Lists]
Advanced

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

struct() handling in C++


From: John W. Eaton
Subject: struct() handling in C++
Date: Wed, 1 Feb 2006 11:00:11 -0500

On  1-Feb-2006, Jan Trmal wrote:

| can anyone tell me, or show me some C++ code, where the DLD function have 
| struct as input argument?

Look at src/DLD-FUNCTIONS/time.cc in the Octave sources.

| for example, when I create in octave
| struct("svm_type", "c_svc", "kernel_type", "linear")
| 
| I use this code for parsing (just example)
| 
| DEFUN_DLD(svmpredict,args, nargout, 
|       "Trains the Support Vector Machine")
| {
|       
|       if (!args.is_map()) {
|               throw eValueConversionError("model parameter must be struct");
|       }

I'm not sure you really want to throw an error here.  Where will it be
caught?  Unless there is a catch block inside the same function where
the error is thrown, you could be bypassing some of Octave's error
handling code which could lead to internal inconsistencies in Octave's
state.

It is generally better to just try to extract the Octave_map object,
then check error_state.

|       Octave_map map = args.map_value();
| 
|       octave_value o1 = (map.contents("kernel_type"));
|       octave_value q1 = o1.list_value().operator()(0);
| 
|       cout << q1.string_value();
| 
|       return octave_value_list() 
| }

I think you need something like

        Octave_map m = args(0).map_value ();

        Cell c = m.contents (k);

        if (! c.is_empty ())
          {
            val = c(0).string_value ();

            if (! error_state)
              std::cout << val;
          }

| But It seems very complicated to get such as simple datatype as string.  
| Is here any less complicated way than the one I've found?  :)

The Octave_map class also includes a "stringfield" convenience
function that allows extracting a single character string value from a
struct.  You could write the above function as:

DEFUN_DLD (svmpredict, args, ,
  "Trains the Support Vector Machine")
{
  if (args.length () == 1)
    {
      Octave_map m = args(0).map_value ();

      if (! error_state)
        {
           std::string val = m.stringfield ("kernel_type");

           if (! error_state)
             std::cout << val;
           else
             error ("svmpredict: expecting kernel_type field to be a character 
string");
        }
      else
        error ("svmpredict: model parameter must be struct");
    }
  else
    print_usage ("svmpredict");

  return octave_value_list ();
}



-------------------------------------------------------------
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]