[2] | 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 |
|
---|
| 27 | namespace HeuristicLab.Core {
|
---|
[776] | 28 | /// <summary>
|
---|
| 29 | /// The base class for all storable objects.
|
---|
| 30 | /// </summary>
|
---|
[2] | 31 | public abstract class StorableBase : IStorable {
|
---|
| 32 | private Guid myGuid;
|
---|
[776] | 33 | /// <summary>
|
---|
| 34 | /// Gets the Guid of the item.
|
---|
| 35 | /// </summary>
|
---|
[2] | 36 | public Guid Guid {
|
---|
| 37 | get { return myGuid; }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[776] | 40 | /// <summary>
|
---|
| 41 | /// Initializes a new instance of the class <see cref="StorableBase"/> with a new <see cref="Guid"/>.
|
---|
| 42 | /// </summary>
|
---|
[2] | 43 | protected StorableBase() {
|
---|
| 44 | myGuid = Guid.NewGuid();
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[776] | 47 | /// <summary>
|
---|
| 48 | /// Clones the current instance (deep clone).
|
---|
| 49 | /// </summary>
|
---|
| 50 | /// <remarks>Uses the <see cref="Auxiliary.Clone"/> method of the class <see cref="Auxiliary"/>.</remarks>
|
---|
| 51 | /// <returns>The clone.</returns>
|
---|
[2] | 52 | public object Clone() {
|
---|
| 53 | return Auxiliary.Clone(this, new Dictionary<Guid, object>());
|
---|
| 54 | }
|
---|
[776] | 55 | /// <summary>
|
---|
| 56 | /// Clones the current instance with the <see cref="M:Activator.CreateInstance"/>
|
---|
| 57 | /// method of <see cref="Activator"/>.
|
---|
| 58 | /// </summary>
|
---|
| 59 | /// <param name="clonedObjects">All already cloned objects.</param>
|
---|
| 60 | /// <returns>The clone.</returns>
|
---|
[2] | 61 | public virtual object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
| 62 | object clone = Activator.CreateInstance(this.GetType());
|
---|
| 63 | clonedObjects.Add(Guid, clone);
|
---|
| 64 | return clone;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[776] | 67 | /// <summary>
|
---|
| 68 | /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
|
---|
| 69 | /// </summary>
|
---|
| 70 | /// <remarks>The type of the current instance is saved as <see cref="XmlAttribute"/> with tag name
|
---|
| 71 | /// <c>Type</c>, the guid is also saved as an attribute with the tag name <c>GUID</c>.</remarks>
|
---|
| 72 | /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
|
---|
| 73 | /// <param name="document">The <see cref="XmlDocument"/> where to save the data.</param>
|
---|
| 74 | /// <param name="persistedObjects">The dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
|
---|
| 75 | /// <returns>The saved <see cref="XmlNode"/>.</returns>
|
---|
[2] | 76 | public virtual XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
|
---|
| 77 | XmlNode node = document.CreateNode(XmlNodeType.Element, name, null);
|
---|
| 78 | XmlAttribute typeAttribute = document.CreateAttribute("Type");
|
---|
[40] | 79 | typeAttribute.Value = PersistenceManager.BuildTypeString(this.GetType());
|
---|
[2] | 80 | node.Attributes.Append(typeAttribute);
|
---|
| 81 | XmlAttribute guidAttribute = document.CreateAttribute("GUID");
|
---|
| 82 | guidAttribute.Value = Guid.ToString();
|
---|
| 83 | node.Attributes.Append(guidAttribute);
|
---|
| 84 | return node;
|
---|
| 85 | }
|
---|
[776] | 86 | /// <summary>
|
---|
| 87 | /// Loads the persisted object from the specified <paramref name="node"/>.
|
---|
| 88 | /// </summary>
|
---|
| 89 | /// <remarks>Loads only guid; type,... already loaded by the <see cref="PersistenceManager"/>.</remarks>
|
---|
| 90 | /// <param name="node">The <see cref="XmlNode"/> where the object is saved.</param>
|
---|
| 91 | /// <param name="restoredObjects">The dictionary of all already restored objects.
|
---|
| 92 | /// (Needed to avoid cycles.)</param>
|
---|
[2] | 93 | public virtual void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
|
---|
| 94 | myGuid = new Guid(node.Attributes["GUID"].Value);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|