Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/02/08 14:13:06 (16 years ago)
Author:
gkronber
Message:

Refactored cloning in HL.Core, HL.Data and HL.Constraints

#285 (Cloning could be improved by creating objects at the bottom of the cloning chain with 'new' instead of the top with Activator.CreateInstance())

Location:
branches/CloningRefactorBranch/HeuristicLab.Core
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • branches/CloningRefactorBranch/HeuristicLab.Core/AtomicOperation.cs

    r836 r885  
    6262
    6363    /// <summary>
     64    /// Copy constructor to create a deep clone of an AtomicOperation instance.
     65    /// <remarks>Calls <see cref="AtomicOperation(AtomicOperation original,
     66    /// IDictionary<Guid, object> clonedObjects)"/></remarks> with an initially
     67    /// empty dictionary of cloned objects to create the clone.
     68    /// </summary>
     69    /// <param name="original">The original instance to be cloned.</param>
     70    public AtomicOperation(AtomicOperation original) : this(original, new Dictionary<Guid, object>()) { }
     71
     72    /// <summary>
     73    /// Copy constructor to create a deep clone of an AtomicOperation instance
     74    /// automatically reusing references of already cloned objects.
     75    /// </summary>
     76    /// <param name="original">The original instance to be cloned</param>
     77    /// <param name="clonedObjects">Already cloned object references</param>
     78    protected AtomicOperation(AtomicOperation original, IDictionary<Guid, object> clonedObjects)
     79      : base(original, clonedObjects) {
     80      this.myOperator = (IOperator)Auxiliary.Clone(original.Operator, clonedObjects);
     81      this.myScope = (IScope)Auxiliary.Clone(original.Scope, clonedObjects);
     82    }
     83
     84    /// <summary>
    6485    /// Clones the current instance.
    6586    /// </summary>
    66     /// <remarks>The operator and the scope objects are cloned with the
    67     /// <see cref="HeuristicLab.Core.Auxiliary.Clone"/> method of the <see cref="Auxiliary"/> class.</remarks>
    68     /// <param name="clonedObjects">All already cloned objects. (Needed to avoid cycles.)</param>
     87    /// <remarks>The operator and the scope objects are cloned with the copy constructor
     88    /// <see cref="HeuristicLab.Core.AtomicOperation(AtomicOperation original, IDictionary<Guid, object> clonedObjects"/>
     89    /// copy constructor.</remarks>
     90    /// <param name="clonedObjects">All already cloned objects. (Needed for referential integrity)</param>
    6991    /// <returns>The cloned object as <see cref="AtomicOperation"/>.</returns>
    7092    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    71       AtomicOperation clone = new AtomicOperation();
    72       clonedObjects.Add(Guid, clone);
    73       clone.myOperator = (IOperator)Auxiliary.Clone(Operator, clonedObjects);
    74       clone.myScope = (IScope)Auxiliary.Clone(Scope, clonedObjects);
    75       return clone;
     93      return new AtomicOperation(this, clonedObjects);
    7694    }
    7795
  • branches/CloningRefactorBranch/HeuristicLab.Core/CompositeOperation.cs

    r776 r885  
    5959
    6060    /// <summary>
     61    /// Copy constructor to create a deep clone of a CompositeOperation instance.
     62    /// <remarks>Calls <see cref="CompositeOperation(CompositeOperation original,
     63    /// IDictionary<Guid, object> clonedObjects)"/></remarks> with an initially
     64    /// empty dictionary of cloned objects to create the clone.
     65    /// </summary>
     66    /// <param name="original">The original instance to be cloned.</param>
     67    public CompositeOperation(CompositeOperation original) : this(original, new Dictionary<Guid, object>()) { }
     68
     69    /// <summary>
     70    /// Copy constructor to create a deep clone of a CompositeOperation instance
     71    /// automatically reusing references of already cloned objects.
     72    /// </summary>
     73    /// <param name="original">The original instance to be cloned</param>
     74    /// <param name="clonedObjects">Already cloned object references</param>
     75    protected CompositeOperation(CompositeOperation original, IDictionary<Guid, object> clonedObjects)
     76      : base(original, clonedObjects) {
     77      this.myExecuteInParallel = original.ExecuteInParallel;
     78      for (int i = 0; i < original.Operations.Count; i++)
     79        this.AddOperation((IOperation)Auxiliary.Clone(original.Operations[i], clonedObjects));
     80    }
     81
     82    /// <summary>
    6183    /// Adds an operation to the current list of operations.
    6284    /// </summary>
     
    7799    /// </summary>
    78100    /// <remarks>All operations of the current instance are cloned, too (deep clone), with the
    79     /// <see cref="HeuristicLab.Core.Auxiliary.Clone"/> method of the class <see cref="Auxiliary"/>.</remarks>
    80     /// <param name="clonedObjects">A dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
     101    /// copy consturctor <see cref="HeuristicLab.Core.CompositeOperation(
     102    /// CompositeOperation original, IDictionary<Guid, object> clonedObjects)"/>.</remarks>
     103    /// <param name="clonedObjects">A dictionary of all already cloned objects. (Needed for referential integrity.)</param>
    81104    /// <returns>The cloned operation as <see cref="CompositeOperation"/>.</returns>
    82105    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    83       CompositeOperation clone = new CompositeOperation();
    84       clonedObjects.Add(Guid, clone);
    85       clone.myExecuteInParallel = ExecuteInParallel;
    86       for (int i = 0; i < Operations.Count; i++)
    87         clone.AddOperation((IOperation)Auxiliary.Clone(Operations[i], clonedObjects));
    88       return clone;
     106      return new CompositeOperation(this, clonedObjects);
    89107    }
    90108
  • branches/CloningRefactorBranch/HeuristicLab.Core/EngineBase.cs

    r836 r885  
    118118
    119119    /// <summary>
    120     /// Copy constructor.
    121     /// </summary>
    122     /// <param name="original"></param>
    123     /// <param name="clonedObjects"></param>
     120    /// Copy constructor to create a deep clone of an EngineBase instance.
     121    /// </summary>
     122    /// <remarks>Calls the copy constructor of the base class <see cref="ItemBase"/>
     123    /// and <see cref="Auxiliary.Clone"/> to clone contained objects</remarks>
     124    /// <param name="original">The instance to be cloned.</param>
     125    /// <param name="clonedObjects">Already cloned object references</param>
    124126    protected EngineBase(EngineBase original, IDictionary<Guid, object> clonedObjects)
    125127      : base(original, clonedObjects) {
     
    133135      this.myRunning = original.Running;
    134136      this.myCanceled = original.Canceled;
    135     }
    136 
    137     /// <summary>
    138     /// Clones the current instance (deep clone).
    139     /// </summary>
    140     /// <remarks>Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
    141     /// <see cref="Auxiliary"/>.</remarks>
    142     /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
    143     /// <returns>The cloned object as <see cref="EngineBase"/>.</returns>
    144     public override object Clone(IDictionary<Guid, object> clonedObjects) {
    145       return new EngineBase(this, clonedObjects);
    146137    }
    147138
  • branches/CloningRefactorBranch/HeuristicLab.Core/OperatorBase.cs

    r836 r885  
    105105
    106106    /// <summary>
    107     /// Copy constructor
    108     /// </summary>
    109     /// <param name="original"></param>
    110     /// <param name="clonedObjects"></param>
     107    /// Copy constructor.
     108    /// </summary>
     109    /// <param name="original">The original instance to be cloned</param>
     110    /// <param name="clonedObjects">Already cloned objects (for referential integrity).</param>
    111111    protected OperatorBase(OperatorBase original, IDictionary<Guid, object> clonedObjects)
    112112      : base(original, clonedObjects) {
  • branches/CloningRefactorBranch/HeuristicLab.Core/OperatorGraph.cs

    r776 r885  
    6060
    6161    /// <summary>
     62    /// Copy constructor to create a deep clone of an OperatorGraph instance.
     63    /// </summary>
     64    /// <param name="original">The original instance to be cloned.</param>
     65    public OperatorGraph(OperatorGraph original) : this(original, new Dictionary<Guid, object>()) { }
     66
     67    /// <summary>
     68    /// Copy constructor to create a deep clone of an OperatorGraph instance
     69    /// reusing already cloned objects.
     70    /// </summary>
     71    /// <param name="original">The original instance to be cloned</param>
     72    /// <param name="clonedObjects">Already cloned objects (for referential integrity).</param>
     73    protected OperatorGraph(OperatorGraph original, IDictionary<Guid, object> clonedObjects)
     74      : base(original, clonedObjects) {
     75      foreach (IOperator op in original.Operators)
     76        this.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
     77      if (original.InitialOperator != null)
     78        this.myInitialOperator = (IOperator)Auxiliary.Clone(original.InitialOperator, clonedObjects);
     79    }
     80
     81    /// <summary>
    6282    /// Creates a new instance of <see cref="OperatorGraphView"/> to represent the current instance
    6383    /// visually.
     
    7191    /// Clones the current instance (deep clone).
    7292    /// </summary>
    73     /// <remarks>Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
    74     /// <see cref="Auxiliary"/>.</remarks>
     93    /// <remarks>Deep clone using the copy constructor.</remarks>
    7594    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
    7695    /// <returns>The cloned object as <see cref="OperatorGraph"/>.</returns>
    7796    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    78       OperatorGraph clone = new OperatorGraph();
    79       clonedObjects.Add(Guid, clone);
    80       foreach (IOperator op in Operators)
    81         clone.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
    82       if (InitialOperator != null)
    83         clone.myInitialOperator = (IOperator)Auxiliary.Clone(InitialOperator, clonedObjects);
    84       return clone;
     97      return new OperatorGraph(this, clonedObjects);
    8598    }
    8699
     
    179192    /// (Needed to avoid cycles.)</param>
    180193    /// <returns>The saved <see cref="XmlNode"/>.</returns>
    181     public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
     194    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    182195      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    183196      XmlNode ops = document.CreateNode(XmlNodeType.Element, "Operators", null);
     
    197210    /// <param name="restoredObjects">The dictionary of all already restored objects.
    198211    /// (Needed to avoid cycles.)</param>
    199     public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
     212    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    200213      base.Populate(node, restoredObjects);
    201214
  • branches/CloningRefactorBranch/HeuristicLab.Core/OperatorGroup.cs

    r836 r885  
    7171
    7272    /// <summary>
    73     /// Copy constructor for deep cloning
    74     /// </summary>
    75     /// <param name="original"></param>
    76     /// <param name="clonedObjects"></param>
     73    /// Copy constructor to create a deep clone of an OperatorGroup instance.
     74    /// </summary>
     75    /// <param name="original">The instance to be cloned.</param>
     76    public OperatorGroup(OperatorGroup original) : this(original, new Dictionary<Guid, object>()) {}
     77
     78    /// <summary>
     79    /// Copy constructor to create a deep clone of an OperatorGroup instance reusing
     80    /// already cloned object references.
     81    /// </summary>
     82    /// <param name="original">The original instance to be cloned</param>
     83    /// <param name="clonedObjects">Already cloned objects (for referential integrity).</param>
    7784    protected OperatorGroup(OperatorGroup original, IDictionary<Guid, object> clonedObjects) : base(original, clonedObjects) {
    7885      this.myName = original.myName;
     
    8895    /// Clones the current instance (deep clone).
    8996    /// </summary>
    90     /// <remarks>Deep clone with <see cref="Auxiliary.Clone"/> method of helper class
    91     /// <see cref="Auxiliary"/>.</remarks>
    92     /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
     97    /// <remarks>Uses copy constructor to create the deep clone.</remarks>
     98    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed for referential integrity.)</param>
    9399    /// <returns>The cloned object as <see cref="OperatorGroup"/>.</returns>
    94100    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    95       OperatorGroup clone = new OperatorGroup(this, clonedObjects);
    96       return clone;
     101      return new OperatorGroup(this, clonedObjects);
    97102    }
    98103
  • branches/CloningRefactorBranch/HeuristicLab.Core/OperatorLibrary.cs

    r776 r885  
    4646
    4747    /// <summary>
     48    /// Copy constructor to create a deep clone of an OperatorLibrary instance.
     49    /// </summary>
     50    /// <param name="original">The instance to be cloned.</param>
     51    public OperatorLibrary(OperatorLibrary original) : this(original, new Dictionary<Guid, object>()) { }
     52
     53    /// <summary>
     54    /// Copy constructor to create a deep clone of an OperatorLibrary instance reusing
     55    /// already cloned object references.
     56    /// </summary>
     57    /// <param name="original">The original instance to be cloned</param>
     58    /// <param name="clonedObjects">Already cloned objects (for referential integrity).</param>
     59    protected OperatorLibrary(OperatorLibrary original, IDictionary<Guid, object> clonedObjects)
     60      : base(original, clonedObjects) {
     61      this.myGroup = (IOperatorGroup)Auxiliary.Clone(original.Group, clonedObjects);
     62    }
     63
     64    /// <summary>
    4865    /// Creates a new instance of <see cref="OperatorLibraryEditor"/> to display the current instance.
    4966    /// </summary>
     
    6380    /// Clones the current instance (deep clone).
    6481    /// </summary>
    65     /// <remarks>Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
    66     /// <see cref="Auxiliary"/>.</remarks>
    67     /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
     82    /// <remarks>Deep clone through the copy constructor.</remarks>
     83    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed for referential integrity.)</param>
    6884    /// <returns>The cloned object as <see cref="OperatorLibrary"/>.</returns>
    6985    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    70       OperatorLibrary clone = new OperatorLibrary();
    71       clonedObjects.Add(Guid, clone);
    72       clone.myGroup = (IOperatorGroup)Auxiliary.Clone(Group, clonedObjects);
    73       return clone;
     86      return new OperatorLibrary(this, clonedObjects);
    7487    }
    7588
  • branches/CloningRefactorBranch/HeuristicLab.Core/Scope.cs

    r836 r885  
    7878
    7979    /// <summary>
    80     /// Copy constructor to create deep clones.
     80    /// Copy constructor to clone (deep) a Scope instance.
    8181    /// </summary>
    8282    /// <param name="original"></param>
    83     /// <param name="clonedObjects"></param>
     83    public Scope(Scope original) : this(original, new Dictionary<Guid, object>()) { }
     84
     85    /// <summary>
     86    /// Copy constructor to clone (deep) a Scope instance reusing
     87    /// already cloned object references.
     88    /// </summary>
     89    /// <param name="original">The original instance to be cloned.</param>
     90    /// <param name="clonedObjects">Already cloned object references (for referential integrity).</param>
    8491    protected Scope(Scope original, IDictionary<Guid, object> clonedObjects)
    8592      : base(original, clonedObjects) {
  • branches/CloningRefactorBranch/HeuristicLab.Core/Variable.cs

    r801 r885  
    7979    }
    8080
     81    /// <summary>
     82    /// Copy constructor to deep clone Variable instances.
     83    /// </summary>
     84    /// <param name="original">The instance to be cloned.</param>
     85    public Variable(Variable original) : this(original, new Dictionary<Guid, object>()) { }
     86
     87    /// <summary>
     88    /// Copy constructor to deep clone Variable instances reusing
     89    /// already cloned object references.
     90    /// </summary>
     91    /// <param name="original">The instance to be cloned.</param>
     92    /// <param name="clonedObjects">Already cloned objects (for referential integrity).</param>
     93    protected Variable(Variable original, IDictionary<Guid, object> clonedObjects)
     94      : base(original, clonedObjects) {
     95      this.myName = original.Name;
     96      if (original.Value != null)
     97        this.myValue = (IItem)Auxiliary.Clone(original.Value, clonedObjects);
     98    }
     99
    81100    /// <inheritdoc cref="IVariable.GetValue&lt;T&gt;"/>
    82101    public T GetValue<T>() where T : class, IItem {
     
    95114    /// Clones the current instance (deep clone).
    96115    /// </summary>
    97     /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
     116    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed for referential integrity.)</param>
    98117    /// <returns>The cloned object as <see cref="Variable"/>.</returns>
    99118    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    100       Variable clone = new Variable();
    101       clonedObjects.Add(Guid, clone);
    102       clone.myName = Name;
    103       if (Value != null)
    104         clone.myValue = (IItem)Auxiliary.Clone(Value, clonedObjects);
    105       return clone;
     119      return new Variable(this, clonedObjects);
    106120    }
    107121
  • branches/CloningRefactorBranch/HeuristicLab.Core/VariableInfo.cs

    r776 r885  
    119119
    120120    /// <summary>
     121    /// Copy constructor to deep clone VariableInfo instances.
     122    /// </summary>
     123    /// <param name="original">The instance to be cloned.</param>
     124    public VariableInfo(VariableInfo original) : this(original, new Dictionary<Guid, object>()) { }
     125
     126    /// <summary>
     127    /// Copy constructor to deep clone VariableInfo instances
     128    /// reusing already cloned object references.
     129    /// </summary>
     130    /// <param name="original">The instance to be cloned.</param>
     131    /// <param name="clonedObjects">Already cloned instances (for referential integrity).</param>
     132    protected VariableInfo(VariableInfo original, IDictionary<Guid, object> clonedObjects)
     133      : base(original, clonedObjects) {
     134      this.myActualName = original.ActualName;
     135      this.myFormalName = original.FormalName;
     136      this.myDescription = original.Description;
     137      this.myDataType = original.DataType;
     138      this.myKind = original.Kind;
     139      this.myLocal = original.Local;
     140    }
     141
     142    /// <summary>
    121143    /// Creates a new instance of <see cref="VariableInfoView"/> to represent the current instance
    122144    /// visually.
     
    130152    /// Clones the current instance (deep clone).
    131153    /// </summary>
    132     /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
     154    /// <remarks>Uses the copy constructor to create the deep clone.</remarks>
     155    /// <param name="clonedObjects">Dictionary of all already cloned objects (for referential integrity.)</param>
    133156    /// <returns>The cloned object as <see cref="VariableInfo"/>.</returns>
    134157    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    135       VariableInfo clone = new VariableInfo();
    136       clonedObjects.Add(Guid, clone);
    137       clone.myActualName = ActualName;
    138       clone.myFormalName = FormalName;
    139       clone.myDescription = Description;
    140       clone.myDataType = DataType;
    141       clone.myKind = Kind;
    142       clone.myLocal = Local;
    143       return clone;
     158      return new VariableInfo(this, clonedObjects);
    144159    }
    145160
Note: See TracChangeset for help on using the changeset viewer.