#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; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Common; 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 { 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") { 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, IItem value) : base(name) { Value = value; } /// public T GetValue() where T : class, IItem { return (T)Value; } /// /// 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(); cloner.RegisterClonedObject(this, clone); clone.name = name; clone.description = description; clone.Value = (IItem)cloner.Clone(value); 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().Name); } /// public event EventHandler ValueChanged; /// /// Fires a new ValueChanged even. /// private void OnValueChanged() { if (ValueChanged != null) ValueChanged(this, new EventArgs()); OnChanged(); } private void Value_Changed(object sender, ChangedEventArgs e) { OnChanged(e); } } }