changeset 15910:dfd0dc594c4f classdef

Add object counting to cdef_class. * libinterp/octave-value/ov-classdef.h (cdef_class::cdef_class_rep::object_count): New member. (cdef_class::cdef_class_rep::cdef_class_rep): Initialize it. (cdef_class::cdef_class_rep::register_object, cdef_class::cdef_class_rep::unregister_object): New methods. (cdef_class::register_object, cdef_class::unregister_object): Likewise. (cdef_object_base::set_class, cdef_object_base::register_object, cdef_object_base::unregister_object): Use them. * libinterp/octave_value/ov-classdef.cc (cdef_class::cdef_class_rep::cdef_class_rep (std::list<cdef_class>)): Initialize object_count.
author Michael Goffioul <michael.goffioul@gmail.com>
date Sun, 06 Jan 2013 15:45:28 -0500
parents b8bff84022d6
children b18b7e560236
files libinterp/octave-value/ov-classdef.cc libinterp/octave-value/ov-classdef.h
diffstat 2 files changed, 35 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/octave-value/ov-classdef.cc	Sun Jan 06 12:19:16 2013 -0500
+++ b/libinterp/octave-value/ov-classdef.cc	Sun Jan 06 15:45:28 2013 -0500
@@ -1214,7 +1214,7 @@
 }
 
 cdef_class::cdef_class_rep::cdef_class_rep (const std::list<cdef_class>& superclasses)
-     : handle_cdef_object (), handle_class (false)
+     : handle_cdef_object (), handle_class (false), object_count (0)
 {
   put ("SuperClasses", to_ov (superclasses));
   implicit_ctor_list = superclasses;
--- a/libinterp/octave-value/ov-classdef.h	Sun Jan 06 12:19:16 2013 -0500
+++ b/libinterp/octave-value/ov-classdef.h	Sun Jan 06 15:45:28 2013 -0500
@@ -405,7 +405,7 @@
   {
   public:
     cdef_class_rep (void)
-	: handle_cdef_object (), handle_class (false) { }
+	: handle_cdef_object (), handle_class (false), object_count (0) { }
 
     cdef_class_rep (const std::list<cdef_class>& superclasses);
 
@@ -450,6 +450,10 @@
 
     bool is_handle_class (void) const { return handle_class; }
 
+    void register_object (void) { object_count++; }
+
+    void unregister_object (void) { object_count--; }
+
   private:
     void load_all_methods (void);
 
@@ -487,6 +491,9 @@
     // called explicitly by the class constructor.
     std::list<cdef_class> implicit_ctor_list;
 
+    // The number of objects of this class.
+    octave_refcount<octave_idx_type> object_count;
+
     // 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;
@@ -597,6 +604,10 @@
   static const cdef_class& meta_method (void) { return _meta_method; }
   static const cdef_class& meta_package (void) { return _meta_package; }
 
+  void register_object (void) { get_rep ()->register_object (); }
+
+  void unregister_object (void) { get_rep ()->unregister_object (); }
+
 private:
   cdef_class_rep* get_rep (void)
     { return dynamic_cast<cdef_class_rep *> (cdef_object::get_rep ()); }
@@ -835,19 +846,38 @@
 inline void
 cdef_object_base::set_class (const cdef_class& cls)
 {
-  klass = cls;
+  if ((klass.ok () && cls.ok () && cls != get_class ())
+      || (klass.ok () && ! cls.ok ())
+      || (! klass.ok () && cls.ok ()))
+    {
+      unregister_object ();
+      klass = cls;
+      register_object ();
+    }
 }
 
 inline void
 cdef_object_base::register_object (void)
 {
-  // FIXME: implement this
+  if (klass.ok ())
+    {
+      cdef_class cls (get_class ());
+
+      if (! error_state && cls.ok ())
+        cls.register_object ();
+    }
 }
 
 inline void
 cdef_object_base::unregister_object (void)
 {
-  // FIXME: implement this
+  if (klass.ok ())
+    {
+      cdef_class cls (get_class ());
+
+      if (! error_state && cls.ok ())
+        cls.unregister_object ();
+    }
 }
 
 inline cdef_method