Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2636 was 2526, checked in by swagner, 15 years ago

Refactored cloning (#806)

File size: 4.8 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    /// Clones the current instance (deep clone).
94    /// </summary>
95    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
96    /// <returns>The cloned object as <see cref="Variable"/>.</returns>
97    public override IItem Clone(ICloner cloner) {
98      Variable clone = new Variable();
99      cloner.RegisterClonedObject(this, clone);
100      clone.myName = Name;
101      if (Value != null)
102        clone.myValue = (IItem)cloner.Clone(Value);
103      return clone;
104    }
105
106    /// <summary>
107    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
108    /// </summary>
109    /// <returns>The current instance as a string.</returns>
110    public override string ToString() {
111      return Name + ": " + ((Value == null) ? ("null") : (Value.ToString()));
112    }
113
114    /// <inheritdoc/>
115    public event EventHandler<CancelEventArgs<string>> NameChanging;
116    /// <summary>
117    /// Fires a new <c>NameChanging</c> event.
118    /// </summary>
119    /// <param name="e">The event arguments of the changing.</param>
120    protected virtual void OnNameChanging(CancelEventArgs<string> e) {
121      if (NameChanging != null)
122        NameChanging(this, e);
123    }
124    /// <inheritdoc/>
125    public event EventHandler NameChanged;
126    /// <summary>
127    /// Fires a new <c>NameChanged</c> event.
128    /// </summary>
129    /// <remarks>Calls <see cref="ItemBase.OnChanged"/>.</remarks>
130    protected virtual void OnNameChanged() {
131      if (NameChanged != null)
132        NameChanged(this, new EventArgs());
133      OnChanged();
134    }
135    /// <inheritdoc/>
136    public event EventHandler ValueChanged;
137    /// <summary>
138    /// Fires a new <c>ValueChanged</c> even.
139    /// </summary>
140    protected virtual void OnValueChanged() {
141      if (ValueChanged != null)
142        ValueChanged(this, new EventArgs());
143      OnChanged();
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.