Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactorBranch/HeuristicLab.Core/StorableBase.cs @ 840

Last change on this file since 840 was 836, checked in by gkronber, 16 years ago

worked on #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 size: 4.4 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Xml;
26
27namespace HeuristicLab.Core {
28  /// <summary>
29  /// The base class for all storable objects.
30  /// </summary>
31  public abstract class StorableBase : IStorable {
32    private Guid myGuid;
33    /// <summary>
34    /// Gets the Guid of the item.
35    /// </summary>
36    public Guid Guid {
37      get { return myGuid; }
38    }
39
40    /// <summary>
41    /// Initializes a new instance of the class <see cref="StorableBase"/> with a new <see cref="Guid"/>.
42    /// </summary>
43    protected StorableBase() {
44      myGuid = Guid.NewGuid();
45    }
46
47    /// <summary>
48    ///  Copy constructor to create a deep clone of the original object
49    /// </summary>
50    /// <param name="original">The object to be cloned</param>
51    protected StorableBase(StorableBase original, IDictionary<Guid, object> clonedObjects) : this() {
52      clonedObjects.Add(myGuid, this);
53    }
54
55    /// <summary>
56    /// Clones the current instance (deep clone).
57    /// </summary>
58    /// <remarks>Uses the <see cref="Auxiliary.Clone"/> method of the class <see cref="Auxiliary"/>.</remarks>
59    /// <returns>The clone.</returns>
60    public object Clone() {
61      return Auxiliary.Clone(this, new Dictionary<Guid, object>());
62    }
63    /// <summary>
64    /// Default implementation of clone throws NotSupportedException.
65    /// All actually cloneable objects must override this method.
66    /// </summary>
67    /// <param name="clonedObjects">All already cloned objects.</param>
68    /// <returns>Nothing</returns>
69    public virtual object Clone(IDictionary<Guid, object> clonedObjects) {
70      throw new NotSupportedException("Can't clone abstract type StorableBase.");
71    }
72
73    /// <summary>
74    /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
75    /// </summary>
76    /// <remarks>The type of the current instance is saved as <see cref="XmlAttribute"/> with tag name
77    /// <c>Type</c>, the guid is also saved as an attribute with the tag name <c>GUID</c>.</remarks>
78    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
79    /// <param name="document">The <see cref="XmlDocument"/> where to save the data.</param>
80    /// <param name="persistedObjects">The dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
81    /// <returns>The saved <see cref="XmlNode"/>.</returns>
82    public virtual XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
83      XmlNode node = document.CreateNode(XmlNodeType.Element, name, null);
84      XmlAttribute typeAttribute = document.CreateAttribute("Type");
85      typeAttribute.Value = PersistenceManager.BuildTypeString(this.GetType());
86      node.Attributes.Append(typeAttribute);
87      XmlAttribute guidAttribute = document.CreateAttribute("GUID");
88      guidAttribute.Value = Guid.ToString();
89      node.Attributes.Append(guidAttribute);
90      return node;
91    }
92    /// <summary>
93    /// Loads the persisted object from the specified <paramref name="node"/>.
94    /// </summary>
95    /// <remarks>Loads only guid; type,... already loaded by the <see cref="PersistenceManager"/>.</remarks>
96    /// <param name="node">The <see cref="XmlNode"/> where the object is saved.</param>
97    /// <param name="restoredObjects">The dictionary of all already restored objects.
98    /// (Needed to avoid cycles.)</param>
99    public virtual void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
100      myGuid = new Guid(node.Attributes["GUID"].Value);
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.