1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Xml;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using System.Diagnostics;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Operators.Stopwatch {
|
---|
31 | public class Stopwatch : ItemBase {
|
---|
32 | private System.Diagnostics.Stopwatch stopwatch;
|
---|
33 | private TimeSpan elapsedTime;
|
---|
34 | private bool running;
|
---|
35 |
|
---|
36 | public Stopwatch() {
|
---|
37 | stopwatch = new System.Diagnostics.Stopwatch();
|
---|
38 | elapsedTime = new TimeSpan(0L);
|
---|
39 | running = false;
|
---|
40 | }
|
---|
41 |
|
---|
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 |
|
---|
58 | public void Start() {
|
---|
59 | stopwatch.Start();
|
---|
60 | running = true;
|
---|
61 | }
|
---|
62 |
|
---|
63 | public void Stop() {
|
---|
64 | stopwatch.Stop();
|
---|
65 | elapsedTime += stopwatch.Elapsed;
|
---|
66 | stopwatch.Reset();
|
---|
67 | running = false;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public TimeSpan Elapsed {
|
---|
71 | get {
|
---|
72 | return elapsedTime + stopwatch.Elapsed;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
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>
|
---|
82 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
83 | return new Stopwatch(this, clonedObjects);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
87 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
88 | XmlAttribute runningAttr = document.CreateAttribute("Running");
|
---|
89 | runningAttr.Value = running.ToString();
|
---|
90 | node.Attributes.Append(runningAttr);
|
---|
91 | XmlAttribute elapsedTicksAttr = document.CreateAttribute("ElapsedTicks");
|
---|
92 | elapsedTicksAttr.Value = elapsedTime.Ticks.ToString();
|
---|
93 | node.Attributes.Append(elapsedTicksAttr);
|
---|
94 | return node;
|
---|
95 | }
|
---|
96 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
97 | long elapsedTicks = long.Parse(node.Attributes["ElapsedTicks"].Value);
|
---|
98 | elapsedTime = TimeSpan.FromTicks(elapsedTicks);
|
---|
99 | running = bool.Parse(node.Attributes["Running"].Value);
|
---|
100 | if (running) stopwatch.Start();
|
---|
101 | base.Populate(node, restoredObjects);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|