[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")]
|
---|
[2664] | 35 | public sealed class Variable : NamedItem {
|
---|
[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 | /// <inheritdoc cref="IVariable.GetValue<T>"/>
|
---|
[2] | 72 | public T GetValue<T>() where T : class, IItem {
|
---|
| 73 | return (T)Value;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[776] | 76 | /// <summary>
|
---|
| 77 | /// Clones the current instance (deep clone).
|
---|
| 78 | /// </summary>
|
---|
| 79 | /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
|
---|
| 80 | /// <returns>The cloned object as <see cref="Variable"/>.</returns>
|
---|
[2653] | 81 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[2] | 82 | Variable clone = new Variable();
|
---|
[2526] | 83 | cloner.RegisterClonedObject(this, clone);
|
---|
[2653] | 84 | clone.name = name;
|
---|
| 85 | clone.description = description;
|
---|
| 86 | clone.Value = (IItem)cloner.Clone(value);
|
---|
[2] | 87 | return clone;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[776] | 90 | /// <summary>
|
---|
| 91 | /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
|
---|
| 92 | /// </summary>
|
---|
| 93 | /// <returns>The current instance as a string.</returns>
|
---|
[2] | 94 | public override string ToString() {
|
---|
[2653] | 95 | if (Value == null)
|
---|
| 96 | return string.Format("{0}: null", Name);
|
---|
| 97 | else
|
---|
| 98 | return string.Format("{0}: {1} ({2})", Name, Value.ToString(), Value.GetType().Name);
|
---|
[2] | 99 | }
|
---|
| 100 |
|
---|
[776] | 101 | /// <inheritdoc/>
|
---|
[2] | 102 | public event EventHandler ValueChanged;
|
---|
[776] | 103 | /// <summary>
|
---|
| 104 | /// Fires a new <c>ValueChanged</c> even.
|
---|
| 105 | /// </summary>
|
---|
[2653] | 106 | private void OnValueChanged() {
|
---|
[2] | 107 | if (ValueChanged != null)
|
---|
| 108 | ValueChanged(this, new EventArgs());
|
---|
| 109 | OnChanged();
|
---|
| 110 | }
|
---|
[2653] | 111 |
|
---|
| 112 | private void Value_Changed(object sender, ChangedEventArgs e) {
|
---|
| 113 | OnChanged(e);
|
---|
| 114 | }
|
---|
[2] | 115 | }
|
---|
| 116 | }
|
---|