Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3107 was 3017, checked in by epitzer, 15 years ago

Merge StorableClassType.Empty into StorableClassType.MarkedOnly and make it the default if not specified (#548)

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  [Creatable("Test")]
32  [StorableClass]
33  public sealed class Variable : NamedItem, IVariable {
34    private IItem value;
35    /// <inheritdoc/>
36    /// <remarks>Calls <see cref="OnValueChanged"/> in the setter.</remarks>
37    [Storable]
38    public IItem Value {
39      get { return value; }
40      set {
41        if (this.value != value) {
42          if (this.value != null) this.value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
43          this.value = value;
44          if (this.value != null) this.value.ToStringChanged += new EventHandler(Value_ToStringChanged);
45          OnValueChanged();
46        }
47      }
48    }
49
50    /// <summary>
51    /// Initializes a new instance of <see cref="Variable"/> with name <c>Anonymous</c>
52    /// and value <c>null</c>.
53    /// </summary>
54    public Variable()
55      : base("Anonymous") {
56      this.value = null;
57    }
58    /// <summary>
59    /// Initializes a new instance of <see cref="Variable"/> with the specified <paramref name="name"/>
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>
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    }
72    public Variable(string name, IItem value)
73      : base(name) {
74      this.value = value;
75      if (this.value != null) this.value.ToStringChanged += new EventHandler(Value_ToStringChanged);
76    }
77    public Variable(string name, string description, IItem value)
78      : base(name, description) {
79      this.value = value;
80      if (this.value != null) this.value.ToStringChanged += new EventHandler(Value_ToStringChanged);
81    }
82
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>
88    public override IDeepCloneable Clone(Cloner cloner) {
89      Variable clone = new Variable(Name, Description, (IItem)cloner.Clone(value));
90      cloner.RegisterClonedObject(this, clone);
91      return clone;
92    }
93
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>
98    public override string ToString() {
99      if (Value == null)
100        return string.Format("{0}: null", Name);
101      else
102        return string.Format("{0}: {1} ({2})", Name, Value.ToString(), Value.GetType().GetPrettyName());
103    }
104
105    /// <inheritdoc/>
106    public event EventHandler ValueChanged;
107    /// <summary>
108    /// Fires a new <c>ValueChanged</c> even.
109    /// </summary>
110    private void OnValueChanged() {
111      if (ValueChanged != null)
112        ValueChanged(this, EventArgs.Empty);
113      OnToStringChanged();
114    }
115
116    private void Value_ToStringChanged(object sender, EventArgs e) {
117      OnToStringChanged();
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.