#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 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 HeuristicLab.Common;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Core {
///
/// Represents a variable which has a name and holds an IItem.
///
[Item("Variable", "A variable which has a name and holds an IItem.")]
[Creatable("Test")]
public sealed class Variable : NamedItem, IVariable {
private IItem value;
///
/// Calls in the setter.
[Storable]
public IItem Value {
get { return value; }
set {
if (this.value != value) {
if (this.value != null) this.value.Changed -= new ChangedEventHandler(Value_Changed);
this.value = value;
if (this.value != null) this.value.Changed += new ChangedEventHandler(Value_Changed);
OnValueChanged();
}
}
}
///
/// Initializes a new instance of with name Anonymous
/// and value null.
///
public Variable()
: base("Anonymous") {
this.value = null;
}
///
/// Initializes a new instance of with the specified
/// and the specified .
///
/// The name of the current instance.
/// The value of the current instance.
public Variable(string name)
: base(name) {
this.value = null;
}
public Variable(string name, string description)
: base(name, description) {
this.value = null;
}
public Variable(string name, IItem value)
: base(name) {
this.value = value;
if (this.value != null) this.value.Changed += new ChangedEventHandler(Value_Changed);
}
public Variable(string name, string description, IItem value)
: base(name, description) {
this.value = value;
if (this.value != null) this.value.Changed += new ChangedEventHandler(Value_Changed);
}
///
/// Clones the current instance (deep clone).
///
/// Dictionary of all already cloned objects. (Needed to avoid cycles.)
/// The cloned object as .
public override IDeepCloneable Clone(Cloner cloner) {
Variable clone = new Variable(Name, Description, (IItem)cloner.Clone(value));
cloner.RegisterClonedObject(this, clone);
return clone;
}
///
/// Gets the string representation of the current instance in the format: Name: [null|Value].
///
/// The current instance as a string.
public override string ToString() {
if (Value == null)
return string.Format("{0}: null", Name);
else
return string.Format("{0}: {1} ({2})", Name, Value.ToString(), Value.GetType().GetPrettyName());
}
///
public event EventHandler ValueChanged;
///
/// Fires a new ValueChanged even.
///
private void OnValueChanged() {
if (ValueChanged != null)
ValueChanged(this, EventArgs.Empty);
OnChanged();
}
private void Value_Changed(object sender, ChangedEventArgs e) {
OnChanged(e);
}
}
}