changeset 9521:e08d72bb988e

simplify cloning
author Jaroslav Hajek <highegg@gmail.com>
date Thu, 13 Aug 2009 11:52:07 +0200
parents bd5909b89457
children e79470be3ecb
files src/ChangeLog src/ov-base.cc src/ov-base.h src/ov-class.cc src/ov-class.h src/ov.h
diffstat 6 files changed, 42 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Thu Aug 13 08:25:02 2009 +0200
+++ b/src/ChangeLog	Thu Aug 13 11:52:07 2009 +0200
@@ -1,3 +1,20 @@
+2009-08-12  Jaroslav Hajek  <highegg@gmail.com>
+
+	* ov-base.h (octave_base_value::count): Declare as octave_idx_type.
+	(octave_base_value::octave_base_value (const octave_base_value&)):
+	Initialize count to 1.
+	(octave_base_value::unique_clone ()): New method.
+	(octave_base_value::print_with_name): Declare as non-const.
+	* ov-base.cc (octave_base_value::print_with_name): Update.
+	* ov.h (octave_value::make_unique (void)): Don't set rep->count.
+	Call unique_clone.
+	(octave_value::make_unique (int)): Don't set rep->count.
+	Call unique_clone.
+	(octave_value::octave_value (const octave_base_value *)): Declare
+	as private.
+	* ov-class.cc (octave_class::print_with_name): Avoid using clone ().
+	* ov-class.h (octave_class::print_with_name): Declare as non-const.
+
 2009-08-13  John W. Eaton  <jwe@octave.org>
 
 	* Makefile.in: Consistently add library-specific CPPFLAGS and
--- a/src/ov-base.cc	Thu Aug 13 08:25:02 2009 +0200
+++ b/src/ov-base.cc	Thu Aug 13 11:52:07 2009 +0200
@@ -386,7 +386,7 @@
 void
 octave_base_value::print_with_name (std::ostream& output_buf,
 				    const std::string& name, 
-				    bool print_padding) const
+				    bool print_padding)
 {
   bool pad_after = print_name_tag (output_buf, name);
 
--- a/src/ov-base.h	Thu Aug 13 08:25:02 2009 +0200
+++ b/src/ov-base.h	Thu Aug 13 11:52:07 2009 +0200
@@ -125,16 +125,24 @@
 
   octave_base_value (void) : count (1) { }
 
-  octave_base_value (const octave_base_value&) { }
+  octave_base_value (const octave_base_value&) : count (1) { }
 
   virtual ~octave_base_value (void) { }
 
+  // Unconditional clone. Always clones.
   virtual octave_base_value *
   clone (void) const { return new octave_base_value (*this); }
 
+  // Empty clone.
   virtual octave_base_value *
   empty_clone (void) const { return new octave_base_value (); }
 
+  // Unique clone. Usually clones, but may be overriden to fake the
+  // cloning when sharing copies is to be controlled from within an
+  // instance (see octave_class).
+  virtual octave_base_value *
+  unique_clone (void) { return clone (); }
+
   virtual type_conv_info
   numeric_conversion_function (void) const
     { return type_conv_info (); }
@@ -500,7 +508,7 @@
 
   virtual void
   print_with_name (std::ostream& output_buf, const std::string& name, 
-		   bool print_padding = true) const;
+		   bool print_padding = true);
 
   virtual void print_info (std::ostream& os, const std::string& prefix) const;
 
@@ -643,7 +651,10 @@
   void reset (void) const;
 
   // A reference count.
-  int count;
+  // NOTE: the declaration is octave_idx_type because with 64-bit indexing,
+  // it is well possible to have more than MAX_INT copies of a single value
+  // (think of an empty cell array with >2G elements).
+  octave_idx_type count;
 
 private:
 
--- a/src/ov-class.cc	Thu Aug 13 08:25:02 2009 +0200
+++ b/src/ov-class.cc	Thu Aug 13 11:52:07 2009 +0200
@@ -906,7 +906,7 @@
 
 void
 octave_class::print_with_name (std::ostream&, const std::string& name, 
-			       bool) const
+			       bool)
 {
   octave_value fcn = symbol_table::find_method ("display", class_name ());
 
@@ -914,7 +914,8 @@
     {
       octave_value_list args;
 
-      args(0) = octave_value (clone (), 1);
+      count++;
+      args(0) = octave_value (this);
       
       string_vector arg_names (1);
 
--- a/src/ov-class.h	Thu Aug 13 08:25:02 2009 +0200
+++ b/src/ov-class.h	Thu Aug 13 11:52:07 2009 +0200
@@ -146,7 +146,7 @@
   bool print_name_tag (std::ostream& os, const std::string& name) const;
 
   void print_with_name (std::ostream& os, const std::string& name, 
-			bool print_padding = true) const;
+			bool print_padding = true);
 
   bool reconstruct_exemplar (void);
 
--- a/src/ov.h	Thu Aug 13 08:25:02 2009 +0200
+++ b/src/ov.h	Thu Aug 13 11:52:07 2009 +0200
@@ -303,8 +303,7 @@
       if (rep->count > 1)
 	{
 	  --rep->count;
-	  rep = rep->clone ();
-	  rep->count = 1;
+	  rep = rep->unique_clone ();
 	}
     }
 
@@ -316,8 +315,7 @@
       if (rep->count > obsolete_copies + 1)
 	{
 	  --rep->count;
-	  rep = rep->clone ();
-	  rep->count = 1;
+	  rep = rep->unique_clone ();
 	}
     }
 
@@ -1142,6 +1140,10 @@
 
   binary_op op_eq_to_binary_op (assign_op op);
 
+  // This declaration protects against constructing octave_value from
+  // const octave_base_value* which actually silently calls octave_value (bool).
+  octave_value (const octave_base_value *);
+
   DECLARE_OCTAVE_ALLOCATOR
 };