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
RevLine 
[2]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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;
[2818]23using HeuristicLab.Common;
[1823]24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]25
26namespace HeuristicLab.Core {
[776]27  /// <summary>
[2653]28  /// Represents a variable which has a name and holds an IItem.
[776]29  /// </summary>
[2653]30  [Item("Variable", "A variable which has a name and holds an IItem.")]
[3017]31  [StorableClass]
[2687]32  public sealed class Variable : NamedItem, IVariable {
[3317]33    [Storable]
[2653]34    private IItem value;
[776]35    /// <inheritdoc/>
[2653]36    /// <remarks>Calls <see cref="OnValueChanged"/> in the setter.</remarks>
[2]37    public IItem Value {
[2653]38      get { return value; }
[2]39      set {
[2653]40        if (this.value != value) {
[2932]41          if (this.value != null) this.value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
[2653]42          this.value = value;
[2932]43          if (this.value != null) this.value.ToStringChanged += new EventHandler(Value_ToStringChanged);
[2]44          OnValueChanged();
45        }
46      }
47    }
[1667]48
[776]49    /// <summary>
50    /// Initializes a new instance of <see cref="Variable"/> with name <c>Anonymous</c>
51    /// and value <c>null</c>.
52    /// </summary>
[2653]53    public Variable()
54      : base("Anonymous") {
[2830]55      this.value = null;
[2]56    }
[776]57    /// <summary>
[801]58    /// Initializes a new instance of <see cref="Variable"/> with the specified <paramref name="name"/>
[776]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>
[2830]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    }
[2653]71    public Variable(string name, IItem value)
72      : base(name) {
[2830]73      this.value = value;
[3317]74      Initialize();
[2]75    }
[2830]76    public Variable(string name, string description, IItem value)
77      : base(name, description) {
78      this.value = value;
[3317]79      Initialize();
[2830]80    }
[3317]81    [StorableConstructor]
82    private Variable(bool deserializing) : base(deserializing) { }
[2]83
[3317]84    [StorableHook(HookType.AfterDeserialization)]
85    private void Initialize() {
86      if (value != null) value.ToStringChanged += new EventHandler(Value_ToStringChanged);
87    }
88
[776]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>
[2653]94    public override IDeepCloneable Clone(Cloner cloner) {
[3317]95      Variable clone = new Variable(Name, Description);
[2526]96      cloner.RegisterClonedObject(this, clone);
[3317]97      clone.ReadOnlyView = ReadOnlyView;
98      clone.value = (IItem)cloner.Clone(value);
99      clone.Initialize();
[2]100      return clone;
101    }
102
[776]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>
[2]107    public override string ToString() {
[2653]108      if (Value == null)
109        return string.Format("{0}: null", Name);
110      else
[2818]111        return string.Format("{0}: {1} ({2})", Name, Value.ToString(), Value.GetType().GetPrettyName());
[2]112    }
113
[776]114    /// <inheritdoc/>
[2]115    public event EventHandler ValueChanged;
[776]116    /// <summary>
117    /// Fires a new <c>ValueChanged</c> even.
118    /// </summary>
[2653]119    private void OnValueChanged() {
[2]120      if (ValueChanged != null)
[2793]121        ValueChanged(this, EventArgs.Empty);
[2989]122      OnToStringChanged();
[2]123    }
[2653]124
[2932]125    private void Value_ToStringChanged(object sender, EventArgs e) {
126      OnToStringChanged();
[2653]127    }
[2]128  }
129}
Note: See TracBrowser for help on using the repository browser.