[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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;
|
---|
[2818] | 23 | using HeuristicLab.Common;
|
---|
[1823] | 24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 25 |
|
---|
| 26 | namespace HeuristicLab.Core {
|
---|
[776] | 27 | /// <summary>
|
---|
[2653] | 28 | /// Represents a variable which has a name and holds an IItem.
|
---|
[776] | 29 | /// </summary>
|
---|
[2653] | 30 | [Item("Variable", "A variable which has a name and holds an IItem.")]
|
---|
| 31 | [Creatable("Test")]
|
---|
[3017] | 32 | [StorableClass]
|
---|
[2687] | 33 | public sealed class Variable : NamedItem, IVariable {
|
---|
[2653] | 34 | private IItem value;
|
---|
[776] | 35 | /// <inheritdoc/>
|
---|
[2653] | 36 | /// <remarks>Calls <see cref="OnValueChanged"/> in the setter.</remarks>
|
---|
[1667] | 37 | [Storable]
|
---|
[2] | 38 | public IItem Value {
|
---|
[2653] | 39 | get { return value; }
|
---|
[2] | 40 | set {
|
---|
[2653] | 41 | if (this.value != value) {
|
---|
[2932] | 42 | if (this.value != null) this.value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
|
---|
[2653] | 43 | this.value = value;
|
---|
[2932] | 44 | if (this.value != null) this.value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
[2] | 45 | OnValueChanged();
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
[1667] | 49 |
|
---|
[776] | 50 | /// <summary>
|
---|
| 51 | /// Initializes a new instance of <see cref="Variable"/> with name <c>Anonymous</c>
|
---|
| 52 | /// and value <c>null</c>.
|
---|
| 53 | /// </summary>
|
---|
[2653] | 54 | public Variable()
|
---|
| 55 | : base("Anonymous") {
|
---|
[2830] | 56 | this.value = null;
|
---|
[2] | 57 | }
|
---|
[776] | 58 | /// <summary>
|
---|
[801] | 59 | /// Initializes a new instance of <see cref="Variable"/> with the specified <paramref name="name"/>
|
---|
[776] | 60 | /// and the specified <paramref name="value"/>.
|
---|
| 61 | /// </summary>
|
---|
| 62 | /// <param name="name">The name of the current instance.</param>
|
---|
| 63 | /// <param name="value">The value of the current instance.</param>
|
---|
[2830] | 64 | public Variable(string name)
|
---|
| 65 | : base(name) {
|
---|
| 66 | this.value = null;
|
---|
| 67 | }
|
---|
| 68 | public Variable(string name, string description)
|
---|
| 69 | : base(name, description) {
|
---|
| 70 | this.value = null;
|
---|
| 71 | }
|
---|
[2653] | 72 | public Variable(string name, IItem value)
|
---|
| 73 | : base(name) {
|
---|
[2830] | 74 | this.value = value;
|
---|
[2932] | 75 | if (this.value != null) this.value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
[2] | 76 | }
|
---|
[2830] | 77 | public Variable(string name, string description, IItem value)
|
---|
| 78 | : base(name, description) {
|
---|
| 79 | this.value = value;
|
---|
[2932] | 80 | if (this.value != null) this.value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
[2830] | 81 | }
|
---|
[2] | 82 |
|
---|
[776] | 83 | /// <summary>
|
---|
| 84 | /// Clones the current instance (deep clone).
|
---|
| 85 | /// </summary>
|
---|
| 86 | /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
|
---|
| 87 | /// <returns>The cloned object as <see cref="Variable"/>.</returns>
|
---|
[2653] | 88 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[2830] | 89 | Variable clone = new Variable(Name, Description, (IItem)cloner.Clone(value));
|
---|
[2526] | 90 | cloner.RegisterClonedObject(this, clone);
|
---|
[2] | 91 | return clone;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[776] | 94 | /// <summary>
|
---|
| 95 | /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
|
---|
| 96 | /// </summary>
|
---|
| 97 | /// <returns>The current instance as a string.</returns>
|
---|
[2] | 98 | public override string ToString() {
|
---|
[2653] | 99 | if (Value == null)
|
---|
| 100 | return string.Format("{0}: null", Name);
|
---|
| 101 | else
|
---|
[2818] | 102 | return string.Format("{0}: {1} ({2})", Name, Value.ToString(), Value.GetType().GetPrettyName());
|
---|
[2] | 103 | }
|
---|
| 104 |
|
---|
[776] | 105 | /// <inheritdoc/>
|
---|
[2] | 106 | public event EventHandler ValueChanged;
|
---|
[776] | 107 | /// <summary>
|
---|
| 108 | /// Fires a new <c>ValueChanged</c> even.
|
---|
| 109 | /// </summary>
|
---|
[2653] | 110 | private void OnValueChanged() {
|
---|
[2] | 111 | if (ValueChanged != null)
|
---|
[2793] | 112 | ValueChanged(this, EventArgs.Empty);
|
---|
[2989] | 113 | OnToStringChanged();
|
---|
[2] | 114 | }
|
---|
[2653] | 115 |
|
---|
[2932] | 116 | private void Value_ToStringChanged(object sender, EventArgs e) {
|
---|
| 117 | OnToStringChanged();
|
---|
[2653] | 118 | }
|
---|
[2] | 119 | }
|
---|
| 120 | }
|
---|