Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Core/3.3/Variable.cs @ 4412

Last change on this file since 4412 was 2474, checked in by swagner, 15 years ago

Implemented generic EventArgs (#796)

File size: 5.1 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 of an operator having a name and a value.
32  /// </summary>
33  public class Variable : ItemBase, IVariable {
34
35    [Storable]
36    private string myName;
37    /// <inheritdoc/>
38    /// <remarks>Calls <see cref="OnNameChanging"/> and also <see cref="OnNameChanged"/>
39    /// eventually in the setter.</remarks>
40    public string Name {
41      get { return myName; }
42      set {
43        if (!myName.Equals(value)) {
44          CancelEventArgs<string> e = new CancelEventArgs<string>(value);
45          OnNameChanging(e);
46          if (!e.Cancel) {
47            myName = value;
48            OnNameChanged();
49          }
50        }
51      }
52    }
53
54    [Storable]
55    private IItem myValue;
56    /// <inheritdoc/>
57    /// <remarks>Calls <see cref="OnValueChanged"/> in the setter.</remarks>
58    public IItem Value {
59      get { return myValue; }
60      set {
61        if (myValue != value) {
62          myValue = value;
63          OnValueChanged();
64        }
65      }
66    }
67
68    /// <summary>
69    /// Initializes a new instance of <see cref="Variable"/> with name <c>Anonymous</c>
70    /// and value <c>null</c>.
71    /// </summary>
72    public Variable() {
73      myName = "Anonymous";
74      myValue = null;
75    }
76    /// <summary>
77    /// Initializes a new instance of <see cref="Variable"/> with the specified <paramref name="name"/>
78    /// and the specified <paramref name="value"/>.
79    /// </summary>
80    /// <param name="name">The name of the current instance.</param>
81    /// <param name="value">The value of the current instance.</param>
82    public Variable(string name, IItem value) {
83      myName = name;
84      myValue = value;
85    }
86
87    /// <inheritdoc cref="IVariable.GetValue&lt;T&gt;"/>
88    public T GetValue<T>() where T : class, IItem {
89      return (T)Value;
90    }
91
92    /// <summary>
93    /// Creates a new instance of <see cref="VariableView"/> to represent the current instance visually.
94    /// </summary>
95    /// <returns>The created view as <see cref="VariableView"/>.</returns>
96    public override IView CreateView() {
97      return new VariableView(this);
98    }
99
100    /// <summary>
101    /// Clones the current instance (deep clone).
102    /// </summary>
103    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
104    /// <returns>The cloned object as <see cref="Variable"/>.</returns>
105    public override object Clone(IDictionary<Guid, object> clonedObjects) {
106      Variable clone = new Variable();
107      clonedObjects.Add(Guid, clone);
108      clone.myName = Name;
109      if (Value != null)
110        clone.myValue = (IItem)Auxiliary.Clone(Value, clonedObjects);
111      return clone;
112    }
113
114    /// <summary>
115    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
116    /// </summary>
117    /// <returns>The current instance as a string.</returns>
118    public override string ToString() {
119      return Name + ": " + ((Value == null) ? ("null") : (Value.ToString()));
120    }
121
122    /// <inheritdoc/>
123    public event EventHandler<CancelEventArgs<string>> NameChanging;
124    /// <summary>
125    /// Fires a new <c>NameChanging</c> event.
126    /// </summary>
127    /// <param name="e">The event arguments of the changing.</param>
128    protected virtual void OnNameChanging(CancelEventArgs<string> e) {
129      if (NameChanging != null)
130        NameChanging(this, e);
131    }
132    /// <inheritdoc/>
133    public event EventHandler NameChanged;
134    /// <summary>
135    /// Fires a new <c>NameChanged</c> event.
136    /// </summary>
137    /// <remarks>Calls <see cref="ItemBase.OnChanged"/>.</remarks>
138    protected virtual void OnNameChanged() {
139      if (NameChanged != null)
140        NameChanged(this, new EventArgs());
141      OnChanged();
142    }
143    /// <inheritdoc/>
144    public event EventHandler ValueChanged;
145    /// <summary>
146    /// Fires a new <c>ValueChanged</c> even.
147    /// </summary>
148    protected virtual void OnValueChanged() {
149      if (ValueChanged != null)
150        ValueChanged(this, new EventArgs());
151      OnChanged();
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.