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