#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace HeuristicLab.Core {
///
/// The base class for all storable objects.
///
public abstract class StorableBase : IStorable {
private Guid myGuid;
///
/// Gets the Guid of the item.
///
public Guid Guid {
get { return myGuid; }
}
///
/// Initializes a new instance of the class with a new .
///
protected StorableBase() {
myGuid = Guid.NewGuid();
}
///
/// Clones the current instance (deep clone).
///
/// Uses the method of the class .
/// The clone.
public object Clone() {
return Auxiliary.Clone(this, new Dictionary());
}
///
/// Clones the current instance with the
/// method of .
///
/// All already cloned objects.
/// The clone.
public virtual object Clone(IDictionary clonedObjects) {
object clone = Activator.CreateInstance(this.GetType());
clonedObjects.Add(Guid, clone);
return clone;
}
///
/// Saves the current instance as in the specified .
///
/// The type of the current instance is saved as with tag name
/// Type, the guid is also saved as an attribute with the tag name GUID.
/// The (tag)name of the .
/// The where to save the data.
/// The dictionary of all already persisted objects. (Needed to avoid cycles.)
/// The saved .
public virtual XmlNode GetXmlNode(string name, XmlDocument document, IDictionary persistedObjects) {
XmlNode node = document.CreateNode(XmlNodeType.Element, name, null);
XmlAttribute typeAttribute = document.CreateAttribute("Type");
typeAttribute.Value = PersistenceManager.BuildTypeString(this.GetType());
node.Attributes.Append(typeAttribute);
XmlAttribute guidAttribute = document.CreateAttribute("GUID");
guidAttribute.Value = Guid.ToString();
node.Attributes.Append(guidAttribute);
return node;
}
///
/// Loads the persisted object from the specified .
///
/// Loads only guid; type,... already loaded by the .
/// The where the object is saved.
/// The dictionary of all already restored objects.
/// (Needed to avoid cycles.)
public virtual void Populate(XmlNode node, IDictionary restoredObjects) {
myGuid = new Guid(node.Attributes["GUID"].Value);
}
}
}