Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/02/08 14:13:06 (15 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())

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/CloningRefactorBranch/HeuristicLab.Data/ConstrainedItemList.cs

    r737 r885  
    5353
    5454    /// <summary>
     55    /// Copy constructor to create clones (deep).
     56    /// </summary>
     57    /// <param name="original">The original instance to be cloned.</param>
     58    public ConstrainedItemList(ConstrainedItemList original) : this(original, new Dictionary<Guid, object>()) { }
     59
     60    /// <summary>
     61    /// Copy constructor to create clones (deep) reusing already cloned object references.
     62    /// </summary>
     63    /// <param name="original">The instance to be cloned.</param>
     64    /// <param name="clonedObjects">Already cloned objects (for referential integrity).</param>
     65    protected ConstrainedItemList(ConstrainedItemList original, IDictionary<Guid, object> clonedObjects)
     66      : base(original, clonedObjects) {
     67      foreach (IConstraint constraint in original.Constraints)
     68        this.AddConstraint((IConstraint)Auxiliary.Clone(constraint, clonedObjects));
     69      this.suspendConstraintCheck = original.suspendConstraintCheck;
     70      foreach (IItem item in original.list) {
     71        this.list.Add((IItem)Auxiliary.Clone(item, clonedObjects));
     72      }
     73    }
     74
     75    /// <summary>
    5576    /// Creates a new instance of <see cref="ConstrainedItemListView"/>.
    5677    /// </summary>
     
    6485    /// Clones the current instance.
    6586    /// </summary>
    66     /// <remarks>The elements of the current instance are cloned with the
    67     /// <see cref="HeuristicLab.Core.Auxiliary.Clone"/> method of the class <see cref="Auxiliary"/>.</remarks>
     87    /// <remarks>The elements of the current instance are cloned with the copy constructor.</remarks>
    6888    /// <param name="clonedObjects">A dictionary of all already cloned objects.</param>
    6989    /// <returns>The cloned instance as <see cref="ConstrainedItemList"/>.</returns>
    7090    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    71       ConstrainedItemList clone = new ConstrainedItemList();
    72       clonedObjects.Add(Guid, clone);
    73       foreach (IConstraint constraint in Constraints)
    74         clone.AddConstraint((IConstraint)Auxiliary.Clone(constraint, clonedObjects));
    75       clone.suspendConstraintCheck = suspendConstraintCheck;
    76       foreach (IItem item in list) {
    77         clone.list.Add((IItem)Auxiliary.Clone(item, clonedObjects));
    78       }
    79       return clone;
     91      return new ConstrainedItemList(this, clonedObjects);
    8092    }
    8193
     
    94106    /// <param name="persistedObjects">A dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
    95107    /// <returns>The saved <see cref="XmlNode"/>.</returns>
    96     public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
     108    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    97109      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    98110      XmlNode listNode = document.CreateNode(XmlNodeType.Element, "ListItems", null);
     
    115127    /// <param name="node">The <see cref="XmlNode"/> where the int is saved.</param>
    116128    /// <param name="restoredObjects">A dictionary of all already restored objects. (Needed to avoid cycles.)</param>
    117     public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
     129    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    118130      base.Populate(node, restoredObjects);
    119131      XmlNode listNode = node.SelectSingleNode("ListItems");
Note: See TracChangeset for help on using the changeset viewer.