Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2664 was 2664, checked in by swagner, 14 years ago

Abandoned policy that the names of all abstract base classes have to end in "Base" (#95)

File size: 4.0 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Xml;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Common;
28
29namespace HeuristicLab.Core {
30  /// <summary>
31  /// Represents a variable which has a name and holds an IItem.
32  /// </summary>
33  [Item("Variable", "A variable which has a name and holds an IItem.")]
34  [Creatable("Test")]
35  public sealed class Variable : NamedItem {
36    private IItem value;
37    /// <inheritdoc/>
38    /// <remarks>Calls <see cref="OnValueChanged"/> in the setter.</remarks>
39    [Storable]
40    public IItem Value {
41      get { return value; }
42      set {
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);
47          OnValueChanged();
48        }
49      }
50    }
51
52    /// <summary>
53    /// Initializes a new instance of <see cref="Variable"/> with name <c>Anonymous</c>
54    /// and value <c>null</c>.
55    /// </summary>
56    public Variable()
57      : base("Anonymous") {
58      Value = null;
59    }
60    /// <summary>
61    /// Initializes a new instance of <see cref="Variable"/> with the specified <paramref name="name"/>
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>
66    public Variable(string name, IItem value)
67      : base(name) {
68      Value = value;
69    }
70
71    /// <inheritdoc cref="IVariable.GetValue&lt;T&gt;"/>
72    public T GetValue<T>() where T : class, IItem {
73      return (T)Value;
74    }
75
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>
81    public override IDeepCloneable Clone(Cloner cloner) {
82      Variable clone = new Variable();
83      cloner.RegisterClonedObject(this, clone);
84      clone.name = name;
85      clone.description = description;
86      clone.Value = (IItem)cloner.Clone(value);
87      return clone;
88    }
89
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>
94    public override string ToString() {
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);
99    }
100
101    /// <inheritdoc/>
102    public event EventHandler ValueChanged;
103    /// <summary>
104    /// Fires a new <c>ValueChanged</c> even.
105    /// </summary>
106    private void OnValueChanged() {
107      if (ValueChanged != null)
108        ValueChanged(this, new EventArgs());
109      OnChanged();
110    }
111
112    private void Value_Changed(object sender, ChangedEventArgs e) {
113      OnChanged(e);
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.