octave-maintainers
[Top][All Lists]
Advanced

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

Yet another OOP update


From: Robert T. Short
Subject: Yet another OOP update
Date: Fri, 08 May 2009 09:55:18 -0700
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.21) Gecko/20090402 SeaMonkey/1.1.16

Attached is a patch that will fix object load to call the class constructor. I also fixed the load_ascii function and fixed "struct" to return the underlying struct when the argument is an object.

I tested this on some fairly complex objects and it seems to work for -text, -hdf5 and -v6 files. I don't like the way it fails when there are errors (e.g. a constructor with arguments) and will make it nicer, but this works quite nicely.

Bob
--
Robert T. Short
PhaseLocked Systems
# HG changeset patch
# User Robert T. Short <address@hidden>
# Date 1241801217 25200
# Node ID fd05ccbe6aba015cae8b3e0411f619817f1cde41
# Parent  6fed559d0ab2f8e8a4e8a517a8631d1f03a3da9f
        * ov-class.h, ov-class.cc (octave_class::reconstruct_exemplar):
        New function.
        * ov-class.cc (octave_class::load_binary, octave_class::load_hdf5,
                       octave_class::load_ascii):
        Construct exemplar table and ensure inheritance is correct.
        * ov-struct.cc (struct): Return struct from object

diff -r 6fed559d0ab2 -r fd05ccbe6aba src/ChangeLog
--- a/src/ChangeLog     Thu May 07 10:42:32 2009 -0700
+++ b/src/ChangeLog     Fri May 08 09:46:57 2009 -0700
@@ -1,3 +1,13 @@
+2009-05-05  Robert T. Short  <address@hidden>
+
+       * ov-class.h, ov-class.cc (octave_class::reconstruct_exemplar):
+       New function.
+       * ov-class.cc (octave_class::load_binary, octave_class::load_hdf5,
+                       octave_class::load_ascii):
+       Construct exemplar table and ensure inheritance is correct.
+        * ov-struct.cc (struct): Return struct from object
+
+
 2009-05-06  Jaroslav Hajek  <address@hidden>
 
        * ov-re-mat.cc (Fdouble): Fix order of branches.
diff -r 6fed559d0ab2 -r fd05ccbe6aba src/ov-class.cc
--- a/src/ov-class.cc   Thu May 07 10:42:32 2009 -0700
+++ b/src/ov-class.cc   Fri May 08 09:46:57 2009 -0700
@@ -813,6 +813,39 @@
     }
 }
 
+// Loading a class properly requires an exemplar map entry for success.
+// If we don't have one, we attempt to create one by calling the constructor 
+// with no arguments.
+bool
+octave_class::reconstruct_exemplar (void)
+{
+  bool retval = false;
+
+  octave_class::exemplar_const_iterator it
+    = octave_class::exemplar_map.find (c_name);
+
+  if (it != octave_class::exemplar_map.end ())
+    retval = true;
+  else
+    {
+      octave_value ctor = symbol_table::find_method (c_name, c_name);
+
+      if (ctor.is_defined ())
+       {
+         octave_value_list result = feval (ctor, 1);
+
+         if (result.length () == 1)
+           retval = true;
+         else
+           warning ("call to constructor for class %s failed", c_name.c_str 
());
+       }
+      else
+       warning ("no constructor for class %s", c_name.c_str ());
+    }
+
+  return retval;
+}
+
 //  Load/save does not provide enough information to reconstruct the
 //  class inheritance structure.  reconstruct_parents () attempts to
 //  do so.  If successful, a "true" value is returned.
@@ -947,19 +980,26 @@
 
              if (is) 
                {
+                 c_name = classname;
+                 reconstruct_exemplar ();
+
                  map = m;
-                 c_name = classname;
+                 
+                 if (! reconstruct_parents ())
+                   warning ("load: unable to reconstruct object inheritance");
+                 else
+                   {
+                     if (load_path::find_method (classname, "loadobj")
+                         != std::string ())
+                       {
+                         octave_value in = new octave_class (*this);
+                         octave_value_list tmp = feval ("loadobj", in, 1);
 
-                 if (load_path::find_method (classname, "loadobj")
-                     != std::string ())
-                   {
-                     octave_value in = new octave_class (*this);
-                     octave_value_list tmp = feval ("loadobj", in, 1);
-
-                     if (! error_state)
-                       map = tmp(0).map_value ();
-                     else
-                       success = false;
+                         if (! error_state)
+                           map = tmp(0).map_value ();
+                         else
+                           success = false;
+                       }
                    }
                }
              else
@@ -1052,6 +1092,7 @@
       return false;
     c_name = classname;
   }
+  reconstruct_exemplar ();
 
   int32_t len;
   if (! is.read (reinterpret_cast<char *> (&len), 4))
@@ -1092,7 +1133,7 @@
          map = m;
 
          if (! reconstruct_parents ())
-           error ("load: unable to reconstruct object inheritance");
+           warning ("load: unable to reconstruct object inheritance");
          else
            {
              if (load_path::find_method (c_name, "loadobj") != std::string ())
@@ -1109,7 +1150,7 @@
        }
       else
        {
-         error ("load: failed to load class");
+         warning ("load: failed to load class");
          success = false;
        }
     }
@@ -1281,6 +1322,7 @@
       c_name = classname;
     }
   while (0);
+  reconstruct_exemplar ();
 
 
 #ifdef HAVE_H5GGET_NUM_OBJS
@@ -1317,7 +1359,7 @@
       map = m;
 
       if (!reconstruct_parents ())
-       error ("load: unable to reconstruct object inheritance");
+       warning ("load: unable to reconstruct object inheritance");
       else
        {
          if (load_path::find_method (c_name, "loadobj") != std::string ())
diff -r 6fed559d0ab2 -r fd05ccbe6aba src/ov-class.h
--- a/src/ov-class.h    Thu May 07 10:42:32 2009 -0700
+++ b/src/ov-class.h    Fri May 08 09:46:57 2009 -0700
@@ -144,6 +144,8 @@
   void print_with_name (std::ostream& os, const std::string& name, 
                        bool print_padding = true) const;
 
+  bool reconstruct_exemplar (void);
+
   bool reconstruct_parents (void);
 
   bool save_ascii (std::ostream& os);
diff -r 6fed559d0ab2 -r fd05ccbe6aba src/ov-struct.cc
--- a/src/ov-struct.cc  Thu May 07 10:42:32 2009 -0700
+++ b/src/ov-struct.cc  Fri May 08 09:46:57 2009 -0700
@@ -701,6 +701,8 @@
 Singleton cells and non-cell values are repeated so that they fill\n\
 the entire array.  If the cells are empty, create an empty structure\n\
 array with the specified field names.\n\
+\n\
+If the argument is an object, return the underlying struct.\n\
 @end deftypefn")
 {
   octave_value retval;
@@ -714,6 +716,15 @@
 
   // Note that struct () creates a 1x1 struct with no fields for
   // compatibility with Matlab.
+
+  if (nargin == 1)
+    if (args(0).is_object ())
+      {
+       Octave_map m = args(0).map_value ();
+       retval = octave_value (new octave_struct (m));
+       
+       return retval;
+      }
 
   if ((nargin == 1 || nargin == 2)
       && args(0).is_empty () && args(0).is_real_matrix ())

reply via email to

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