changeset 19218:e0a7718ac085

Implement calling "struct" on classdef objects. * ov-classdef.h (cdef_object::map_value, octave_classdef::map_value): New methods. (cdef_class::get_properties, cdef_class::cdef_class_rep::get_properties, cdef_class::cdef_class_rep::find_properties): Change signature to include an int argument "mode". (cdef_class::get_property_map, cdef_class::cdef_class_rep::get_property_map): New methods. (class cdef_class): New enum to hold possible values of mode argument for method get_properties/get_property_map. (cdef_object_rep::map_keys): Code style. * ov-classdef.cc (cdef_class::cdef_class_rep::find_properties): Change signature to include an int argument "mode". (cdef_class::cdef_class_rep::get_properties): Likewise. Use get_property_map instead of find_properties. (cdef_class::cdef_class_rep::get_property_map): New method. (cdef_object::map_value): Likewise.
author Michael Goffioul <michael.goffioul@gmail.com>
date Sun, 28 Sep 2014 21:43:51 -0400
parents 666c4e0aca1e
children 8ca827e18349
files libinterp/octave-value/ov-classdef.cc libinterp/octave-value/ov-classdef.h
diffstat 2 files changed, 98 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/octave-value/ov-classdef.cc	Sun Sep 28 12:55:03 2014 -0700
+++ b/libinterp/octave-value/ov-classdef.cc	Sun Sep 28 21:43:51 2014 -0400
@@ -1330,6 +1330,63 @@
 
 //----------------------------------------------------------------------------
 
+octave_map
+cdef_object::map_value (void) const
+{
+  octave_map retval;
+
+  warning_with_id ("Octave:classdef-to-struct",
+                   "struct: converting a classdef object into a struct "
+                   "overrides the access restrictions defined for properties. "
+                   "All properties are returned, including private and "
+                   "protected ones.");
+
+  cdef_class cls = get_class ();
+
+  if (cls.ok ())
+    {
+      std::map<std::string, cdef_property> props;
+
+      props = cls.get_property_map (cdef_class::property_all);
+
+      for (std::map<std::string, cdef_property>::iterator it = props.begin ();
+           it != props.end (); ++it)
+        {
+          octave_value pvalue;
+
+          if (is_array ())
+            {
+              Array<cdef_object> a_obj = array_value ();
+
+              Cell cvalue (a_obj.dims ());
+
+              for (octave_idx_type i = 0; i < a_obj.numel (); i++)
+                {
+                  cvalue (i) = it->second.get_value (a_obj(i), false);
+
+                  if (error_state)
+                    break;
+                }
+
+              if (! error_state)
+                retval.setfield (it->first, cvalue);
+            }
+          else
+            {
+              Cell cvalue (dim_vector (1, 1), it->second.get_value (*this, false));
+
+              if (! error_state)
+                retval.setfield (it->first, cvalue);
+            }
+
+          if (error_state)
+            break;
+        }
+    }
+
+  return retval;
+}
+
 string_vector
 cdef_object_rep::map_keys (void) const
 {
@@ -2218,11 +2275,11 @@
 }
 
 Cell
-cdef_class::cdef_class_rep::get_properties (void)
+cdef_class::cdef_class_rep::get_properties (int mode)
 {
   std::map<std::string,cdef_property> props;
 
-  find_properties (props, false);
+  props = get_property_map (mode);
 
   if (! error_state)
     {
@@ -2240,9 +2297,19 @@
   return Cell ();
 }
 
+std::map<std::string, cdef_property>
+cdef_class::cdef_class_rep::get_property_map (int mode)
+{
+  std::map<std::string,cdef_property> props;
+
+  find_properties (props, mode);
+
+  return props;
+}
+
 void
 cdef_class::cdef_class_rep::find_properties (std::map<std::string,cdef_property>& props,
-                                             bool only_inherited)
+                                             int mode)
 {
   property_const_iterator it;
 
@@ -2253,7 +2320,7 @@
 
       if (props.find (nm) == props.end ())
         {
-          if (only_inherited)
+          if (mode == property_inherited)
             {
               octave_value acc = it->second.get ("GetAccess");
 
@@ -2275,7 +2342,10 @@
       cdef_class cls = lookup_class (super_classes(i));
 
       if (! error_state)
-        cls.get_rep ()->find_properties (props, true);
+        cls.get_rep ()->find_properties (props,
+                                         (mode == property_all ?
+                                          property_all :
+                                          property_inherited));
       else
         break;
     }
--- a/libinterp/octave-value/ov-classdef.h	Sun Sep 28 12:55:03 2014 -0700
+++ b/libinterp/octave-value/ov-classdef.h	Sun Sep 28 21:43:51 2014 -0400
@@ -122,7 +122,7 @@
       return octave_value ();
     }
 
-  virtual string_vector map_keys(void) const;
+  virtual string_vector map_keys (void) const;
 
   virtual bool is_valid (void) const { return false; }
 
@@ -270,6 +270,8 @@
 
   string_vector map_keys (void) const { return rep->map_keys (); }
 
+  octave_map map_value (void) const;
+
   const cdef_object_rep* get_rep (void) const { return rep; }
 
   bool ok (void) const { return rep->is_valid (); }
@@ -646,7 +648,9 @@
 
     void install_property (const cdef_property& prop);
 
-    Cell get_properties (void);
+    Cell get_properties (int mode);
+
+    std::map<std::string, cdef_property> get_property_map (int mode);
 
     string_vector get_names (void);
 
@@ -708,7 +712,7 @@
     void find_names (std::set<std::string>& names, bool all);
     
     void find_properties (std::map<std::string,cdef_property>& props,
-                          bool only_inherited);
+                          int mode = 0);
 
     void find_methods (std::map<std::string, cdef_method>& meths,
                        bool only_inherited);
@@ -804,7 +808,12 @@
   void install_property (const cdef_property& prop)
     { get_rep ()->install_property (prop); }
 
-  Cell get_properties (void) { return get_rep ()->get_properties (); }
+  Cell get_properties (int mode = property_normal)
+    { return get_rep ()->get_properties (mode); }
+
+  std::map<std::string, cdef_property>
+  get_property_map (int mode = property_normal)
+    { return get_rep ()->get_property_map (mode); }
 
   string_vector get_names (void) { return get_rep ()->get_names (); }
 
@@ -866,6 +875,14 @@
 
   void unregister_object (void) { get_rep ()->unregister_object (); }
 
+public:
+  enum
+    {
+      property_normal,
+      property_inherited,
+      property_all
+    };
+
 private:
   cdef_class_rep* get_rep (void)
     { return dynamic_cast<cdef_class_rep *> (cdef_object::get_rep ()); }
@@ -1452,6 +1469,8 @@
 
   string_vector map_keys (void) const { return object.map_keys (); }
 
+  octave_map map_value (void) const { return object.map_value (); }
+
   dim_vector dims (void) const { return object.dims (); }
 
 private: