diff libinterp/octave-value/ov-classdef.h @ 15869:5e5705b3e505 classdef

Implement some embryonic handle-classdef semantic. * libinterp/octave-value/ov-classdef.h (cdef_object_rep::subsasgn, handle_cdef_object::subsasgn, octave_classdef::subsasgn): New method declarations. (handle_cdef_object::~handle_cdef_object): New destructor declaration. (cdef_class::cdef_class_rep::handle_class): New boolean field. (cdef_class::cdef_class_rep::cdef_class_rep): Initialize it. (cdef_class::cdef_class_rep::mark_as_handle_class, cdef_class::cdef_class_rep::is_handle_class, cdef_class::mark_as_handle_class, cdef_class::is_handle_class): Manipulate it. (cdef_class::cdef_class_rep::get_name): New method. (cdef_class::get_name): Use it. (cdef_class::cdef_class_rep::initialize_object, cdef_class::cdef_class_rep::subsref_meta, cdef_class::cdef_class_rep::run_constructor, cdef_class::cdef_class_rep::construct, cdef_class:initialize_object, cdef_class::subsref_meta, cdef_class::run_constructor, cdef_class::construct): New methods. (cdef_class::cdef_class_rep::find_method, cdef_class::find_method): Add "local" argument. (cdef_class::cdef_class_rep::find_names, cdef_class::find_names): Change signature to use std::set and a boolean flag. (cdef_class::cdef_class_rep::find_methods, cdef_class::find_methods): Change signature to use a boolean flag as second argument. (cdef_class::make_meta_class): New static method. (cdef_class::get_method_function, cdef_class::get_constructor_function): New methods. (cdef_property::cdef_property_rep::set_value, cdef_property::set_value): Make cdef_object argument non const. (cdef_property::cder_property_rep::is_relative_set): New method. (cdef_property::get_get_access, cdef_property::get_set_access): Delete methods. (cdef_property::check_get_access, cdef_property::check_set_access): Remove string argument. (cdef_method::get_access): Delete method. (cdef_method::check_access): Remove string argument. * libinterp/octave-value/ov-classdef.cc (gripe_method_access, gripe_property_access): Support access specified as cell array of classes. (make_function_of_class): New static function(s). (check_access (std::string, std::string), superclass_access): Remove static functions. (lookup_class): Use symbol_table when class hasn't been loaded yet. (lookup_classes): Returns std::list<cdef_class> instead of Cell. (class_get_superclasses, class_get_inferiorclasses): Use it. (to_ov (const std::list<cdef_class>&)): New static function. (get_class_context, check_access (const cdef_class&, const octave_value&)): Likewise. (handle_cdef_object::subsref): Use new signature of access check methods. (property_get_defaultvalue): New built-in property accessor. (make_class): Change signature to support multiple inheritance. Set "Sealed" to false by default. Determine value for HandleCompatible property and handle-class representation. (make_property): Take cdef_class as first argument. Add DefaultValue and HasDefault properties. Call make_function_of_class for property accessors. (make_attribute): Take cdef_class as first argument. (make_method): Likewise. Call make_function_of_class. (make_method (octave_builtin::fcn)): Do no construct a function handle object. (octave_classdef::subsasgn, handle_cdef_object::subsasgn): New method. (class octave_classdef_proxy): New class. (cdef_class::get_method_function): Use it, new method. (handle_cdef_object::~handle_cdef_object): New destructor. (cdef_class_rep::find_method): New boolean "local" argument. When true, only look into the current class, not in superclasses. (cdef_class_rep::find_methods): New signature. (cdef_class_rep::get_methods): Use it. (cdef_class_rep::find_properties): New signature. (cdef_class_rep::get_properties): Use it. (cdef_class_rep::find_names): New signature. (cdef_class_rep::get_names): Use it. (cdef_class_rep::subsref_meta, cdef_class_rep::initialize_object, cdef_class_rep::run_constructor, cdef_class_rep::construct): New methods. (compute_attribute_value, attribute_to_string): New static functions. (cdef_class::make_meta_class): Change signature, non const argument. Implement it. (cdef_property_rep::get_value): Do not check access here. (cdef_property_rep::set_value, cdef_property_rep::is_recursive_set): New method. (cdef_property_rep::check_get_access, cdef_property_rep::check_set_access, cdef_method_rep::check_access): Use static check_access utility function. (install_classdef): Adapt to change of signature of make_class. Mark meta classes as sealed. Add HandleCompatible property to meta.class. Add DefaultValue and HasDefault properties to meta.property. * libinterp/parse-tree/pt-classdef.h (tree_classdef::make_meta_class): Change return type to (octave_function *). * libinterp/parse-tree/pt-classdef.cc (tree_classdef::make_meta_class): Likewise. Call cdef_class::get_constructor_function. * libinterp/parse-tree/oct-parse.yy (parse_fcn_file): Adapt to new signature of tree_classdef::make_meta_class. * libinterp/octave-value/ov-fcn.h (octave_function::is_classdef_constructor): New virtual method. * libinterp/octave-value/ov-usr-fcn.h (octave_user_function::class_ctor_type): New private enum. (octave_user_function::mark_as_class_constructor, octave_user_function::is_class_constructor): Use it. (octave_user_function::mark_as_classdef_constructor, octave_user_function::is_classdef_constructor): New methods. (octave_user_function::class_constructor): Turn into class_ctor_type. * libinterp/octave-value/of-usr-fcn.cc (octave_user_function::octave_user_function): Initialize class_constructor. (octave_user_function::do_multi_index_op): When function is a classdef constructor, extract the first argument and use it to populate the first output argument.
author Michael Goffioul <michael.goffioul@gmail.com>
date Tue, 01 Jan 2013 19:42:17 -0500
parents 5861c4bde387
children 2b6fe094e615
line wrap: on
line diff
--- a/libinterp/octave-value/ov-classdef.h
+++ b/libinterp/octave-value/ov-classdef.h
@@ -23,6 +23,7 @@
 #if !defined (octave_classdef_h)
 #define octave_classdef_h 1
 
+#include <set>
 #include <string>
 
 #include "oct-map.h"
@@ -80,6 +81,14 @@
       return octave_value_list ();
     }
 
+  virtual octave_value subsasgn (const std::string&,
+                                 const std::list<octave_value_list>&,
+                                 const octave_value&)
+    {
+      error ("subsasgn: invalid object");
+      return octave_value ();
+    }
+
   virtual string_vector map_keys(void) const;
 
   virtual bool is_valid (void) const { return false; }
@@ -156,6 +165,11 @@
 			     int nargout, int& skip)
     { return rep->subsref (type, idx, nargout, skip); }
 
+  octave_value subsasgn (const std::string& type,
+                         const std::list<octave_value_list>& idx,
+                         const octave_value& rhs)
+    { return rep->subsasgn (type, idx, rhs); }
+
   string_vector map_keys (void) const { return rep->map_keys (); }
 
   const cdef_object_rep* get_rep (void) const { return rep; }
@@ -179,6 +193,8 @@
   handle_cdef_object (const std::string& nm)
       : cdef_object_rep (nm) { }
 
+  ~handle_cdef_object (void);
+
   cdef_object_rep* clone (void) const
     {
       handle_cdef_object *obj = const_cast<handle_cdef_object *> (this);
@@ -206,6 +222,10 @@
 			     const std::list<octave_value_list>& idx,
 			     int nargout, int& skip);
 
+  octave_value subsasgn (const std::string& type,
+                         const std::list<octave_value_list>& idx,
+                         const octave_value& rhs);
+
   bool is_valid (void) const { return true; }
 
 protected:
@@ -222,9 +242,12 @@
   {
   public:
     cdef_class_rep (const std::string& nm)
-	: handle_cdef_object (nm) { }
+	: handle_cdef_object (nm), handle_class (false) { }
 
-    cdef_method find_method (const std::string& nm);
+    std::string get_name (void) const
+      { return get ("Name").string_value (); }
+
+    cdef_method find_method (const std::string& nm, bool local = false);
 
     void install_method (const cdef_method& meth);
 
@@ -244,25 +267,47 @@
 
     void delete_object (cdef_object obj);
 
+    octave_value_list subsref_meta (const std::string& type,
+                                    const std::list<octave_value_list>& idx,
+                                    int nargout);
+
+    octave_value construct (const octave_value_list& args);
+
+    void initialize_object (cdef_object& obj);
+
+    void run_constructor (cdef_object& obj, const octave_value_list& args);
+
+    void mark_as_handle_class (void) { handle_class = true; }
+
+    bool is_handle_class (void) const { return handle_class; }
+
   private:
     void load_all_methods (void);
 
-    void find_names (std::map<std::string,std::string>& names,
-		     std::map<std::string,int>& count);
+    void find_names (std::set<std::string>& names, bool all);
     
     void find_properties (std::map<std::string,cdef_property>& props,
-			  std::map<std::string,int>& count);
-    
-    void find_methods (std::map<std::string,cdef_method>& meths,
-		       std::map<std::string,int>& count);
+                          bool only_inherited);
+
+    void find_methods (std::map<std::string, cdef_method>& meths,
+                       bool only_inherited);
 
   private:
+    // The @-directory were this class is loaded from.
+    // (not used yet)
     std::string directory;
 
+    // The methods defined by this class.
     std::map<std::string,cdef_method> method_map;
 
+    // The properties defined by this class.
     std::map<std::string,cdef_property> property_map;
 
+    // TRUE if this class is a handle class. A class is a handle
+    // class when the abstract "handle" class is one of its superclasses.
+    bool handle_class;
+
+    // Utility iterator typedef's.
     typedef std::map<std::string,cdef_method>::iterator method_iterator;
     typedef std::map<std::string,cdef_method>::const_iterator method_const_iterator;
     typedef std::map<std::string,cdef_property>::iterator property_iterator;
@@ -307,7 +352,7 @@
       return *this;
     }
 
-  cdef_method find_method (const std::string& nm);
+  cdef_method find_method (const std::string& nm, bool local = false);
 
   void install_method (const cdef_method& meth)
     { get_rep ()->install_method (meth); }
@@ -330,7 +375,7 @@
     { return get_rep ()->get_directory (); }
 
   std::string get_name (void) const
-    { return get ("Name").string_value (); }
+    { return get_rep ()->get_name (); }
 
   bool is_builtin (void) const
     { return get_directory ().empty (); }
@@ -338,7 +383,32 @@
   void delete_object (cdef_object obj)
     { get_rep ()->delete_object (obj); }
 
-  static cdef_class make_meta_class (const tree_classdef* t);
+  octave_value_list subsref_meta (const std::string& type,
+                                  const std::list<octave_value_list>& idx,
+                                  int nargout)
+    { return get_rep ()->subsref_meta (type, idx, nargout); }
+
+  static cdef_class make_meta_class (tree_classdef* t);
+
+  octave_function* get_method_function (const std::string& nm);
+
+  octave_function* get_constructor_function (void)
+    { return get_method_function (get_name ()); }
+
+  octave_value construct (const octave_value_list& args)
+    { return get_rep ()->construct (args); }
+
+  void initialize_object (cdef_object& obj)
+    { get_rep ()->initialize_object (obj); }
+
+  void run_constructor (cdef_object& obj, const octave_value_list& args)
+    { get_rep ()->run_constructor (obj, args); }
+
+  void mark_as_handle_class (void)
+    { get_rep ()->mark_as_handle_class (); }
+
+  bool is_handle_class (void) const
+    { return get_rep ()->is_handle_class (); }
 
 private:
   cdef_class_rep* get_rep (void)
@@ -378,7 +448,10 @@
 
     void set_value (const octave_value& val) { default_value = val; }
 
-    void set_value (const cdef_object& obj, const octave_value& val);
+    void set_value (cdef_object& obj, const octave_value& val);
+
+  private:
+    bool is_recursive_set (const cdef_object& obj) const;
 
   private:
     octave_value default_value;
@@ -414,20 +487,14 @@
 
   octave_value get_value (void) { return get_rep ()->get_value (); }
 
-  void set_value (const cdef_object& obj, const octave_value& val)
+  void set_value (cdef_object& obj, const octave_value& val)
     { get_rep ()->set_value (obj, val); }
 
   void set_value (const octave_value& val) { get_rep ()->set_value (val); }
  
-  std::string get_get_access (void) const
-    { return get ("GetAccess").string_value (); }
+  bool check_get_access (void) const;
   
-  std::string get_set_access (void) const
-    { return get ("SetAccess").string_value (); }
-
-  bool check_get_access (const std::string& acc) const;
-  
-  bool check_set_access (const std::string& acc) const;
+  bool check_set_access (void) const;
 
   std::string get_name (void) const
     { return get ("Name").string_value (); }
@@ -506,10 +573,7 @@
 			     const octave_value_list& args, int nargout)
     { return get_rep ()->execute (obj, args, nargout); }
 
-  std::string get_access (void) const
-    { return get ("Access").string_value (); }
-
-  bool check_access (const std::string& req) const;
+  bool check_access (void) const;
   
   std::string get_name (void) const
     { return get ("Name").string_value (); }
@@ -533,8 +597,8 @@
 { return rep->get_class (); }
 
 inline cdef_method
-cdef_class::find_method (const std::string& nm)
-{ return get_rep ()->find_method (nm); }
+cdef_class::find_method (const std::string& nm, bool local)
+{ return get_rep ()->find_method (nm, local); }
 
 inline cdef_property
 cdef_class::find_property (const std::string& nm)
@@ -679,6 +743,10 @@
       return (retval.length () > 0 ? retval(0) : octave_value ());
     }
 
+  octave_value subsasgn (const std::string& type,
+                         const std::list<octave_value_list>& idx,
+                         const octave_value& rhs);
+
   string_vector map_keys (void) const { return object.map_keys (); }
 
   dim_vector dims (void) const { return dim_vector (1, 1); }