Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3337 was 3317, checked in by swagner, 15 years ago

Implemented ReadOnlyView property for items (#969).

File size: 4.5 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    [Storable]
34    private IItem value;
35    /// <inheritdoc/>
36    /// <remarks>Calls <see cref="OnValueChanged"/> in the setter.</remarks>
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      Initialize();
75    }
76    public Variable(string name, string description, IItem value)
77      : base(name, description) {
78      this.value = value;
79      Initialize();
80    }
81    [StorableConstructor]
82    private Variable(bool deserializing) : base(deserializing) { }
83
84    [StorableHook(HookType.AfterDeserialization)]
85    private void Initialize() {
86      if (value != null) value.ToStringChanged += new EventHandler(Value_ToStringChanged);
87    }
88
89    /// <summary>
90    /// Clones the current instance (deep clone).
91    /// </summary>
92    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
93    /// <returns>The cloned object as <see cref="Variable"/>.</returns>
94    public override IDeepCloneable Clone(Cloner cloner) {
95      Variable clone = new Variable(Name, Description);
96      cloner.RegisterClonedObject(this, clone);
97      clone.ReadOnlyView = ReadOnlyView;
98      clone.value = (IItem)cloner.Clone(value);
99      clone.Initialize();
100      return clone;
101    }
102
103    /// <summary>
104    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
105    /// </summary>
106    /// <returns>The current instance as a string.</returns>
107    public override string ToString() {
108      if (Value == null)
109        return string.Format("{0}: null", Name);
110      else
111        return string.Format("{0}: {1} ({2})", Name, Value.ToString(), Value.GetType().GetPrettyName());
112    }
113
114    /// <inheritdoc/>
115    public event EventHandler ValueChanged;
116    /// <summary>
117    /// Fires a new <c>ValueChanged</c> even.
118    /// </summary>
119    private void OnValueChanged() {
120      if (ValueChanged != null)
121        ValueChanged(this, EventArgs.Empty);
122      OnToStringChanged();
123    }
124
125    private void Value_ToStringChanged(object sender, EventArgs e) {
126      OnToStringChanged();
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.