Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/Variable.cs @ 3280

Last change on this file since 3280 was 3160, checked in by swagner, 15 years ago

Removed Creatable test attribute (#935).

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