# HG changeset patch # User Jaroslav Hajek # Date 1250157127 -7200 # Node ID e08d72bb988e0d288580b11df3803795b69edf8e # Parent bd5909b89457a240c61a29c5d9008f4ec613b682 simplify cloning diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,20 @@ +2009-08-12 Jaroslav Hajek + + * 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 * Makefile.in: Consistently add library-specific CPPFLAGS and diff --git a/src/ov-base.cc b/src/ov-base.cc --- a/src/ov-base.cc +++ b/src/ov-base.cc @@ -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); diff --git a/src/ov-base.h b/src/ov-base.h --- a/src/ov-base.h +++ b/src/ov-base.h @@ -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: diff --git a/src/ov-class.cc b/src/ov-class.cc --- a/src/ov-class.cc +++ b/src/ov-class.cc @@ -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); diff --git a/src/ov-class.h b/src/ov-class.h --- a/src/ov-class.h +++ b/src/ov-class.h @@ -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); diff --git a/src/ov.h b/src/ov.h --- a/src/ov.h +++ b/src/ov.h @@ -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 };