Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/02/08 21:10:17 (16 years ago)
Author:
gkronber
Message:

Refactored cloning in all plugins except: HL.Communication, HL.Hive, HL.GP, HL.Routing, HL.Scheduling, HL.SimOpt, HL.Visualization

#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.Operators.Stopwatch/Stopwatch.cs

    r693 r887  
    4040    }
    4141
     42    /// <summary>
     43    /// Copy constructor to create deep clones.
     44    /// </summary>
     45    /// <param name="original">The instance to be cloned.</param>
     46    public Stopwatch(Stopwatch original) : this(original, new Dictionary<Guid, object>()) { }
     47    /// <summary>
     48    /// Copy constructor to create deep clones reusing already cloned object references.
     49    /// </summary>
     50    /// <param name="original">The instance to be cloned.</param>
     51    /// <param name="clonedObjects">Already cloned objects (for referential integrity).</param>
     52    protected Stopwatch(Stopwatch original, IDictionary<Guid, object> clonedObjects)
     53      : base(original, clonedObjects) {
     54      elapsedTime = original.elapsedTime;
     55      if (original.running) this.Start();
     56    }
     57
    4258    public void Start() {
    4359      stopwatch.Start();
     
    5874    }
    5975
     76    /// <summary>
     77    /// Creates a deep clone with the copy constructor reusing already cloned
     78    /// object references.
     79    /// </summary>
     80    /// <param name="clonedObjects">Already cloned objects (for referential integrity).</param>
     81    /// <returns>The cloned instance.</returns>
    6082    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    61       Stopwatch clone = (Stopwatch)base.Clone(clonedObjects);
    62       if(running) clone.Start();
    63       clone.elapsedTime = elapsedTime;
    64       return clone;
     83      return new Stopwatch(this, clonedObjects);
    6584    }
    6685
    67     public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
     86    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    6887      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    6988      XmlAttribute runningAttr = document.CreateAttribute("Running");
     
    7594      return node;
    7695    }
    77     public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
     96    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    7897      long elapsedTicks = long.Parse(node.Attributes["ElapsedTicks"].Value);
    7998      elapsedTime = TimeSpan.FromTicks(elapsedTicks);
    8099      running = bool.Parse(node.Attributes["Running"].Value);
    81       if(running) stopwatch.Start();
     100      if (running) stopwatch.Start();
    82101      base.Populate(node, restoredObjects);
    83102    }
Note: See TracChangeset for help on using the changeset viewer.