Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2770 was 2687, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • implemented ideas which came up during today's presentation of HeuristicLab.Core and related plugins
File size: 3.9 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, IVariable {
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    /// <summary>
72    /// Clones the current instance (deep clone).
73    /// </summary>
74    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
75    /// <returns>The cloned object as <see cref="Variable"/>.</returns>
76    public override IDeepCloneable Clone(Cloner cloner) {
77      Variable clone = new Variable();
78      cloner.RegisterClonedObject(this, clone);
79      clone.name = name;
80      clone.description = description;
81      clone.Value = (IItem)cloner.Clone(value);
82      return clone;
83    }
84
85    /// <summary>
86    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
87    /// </summary>
88    /// <returns>The current instance as a string.</returns>
89    public override string ToString() {
90      if (Value == null)
91        return string.Format("{0}: null", Name);
92      else
93        return string.Format("{0}: {1} ({2})", Name, Value.ToString(), Value.GetType().Name);
94    }
95
96    /// <inheritdoc/>
97    public event EventHandler ValueChanged;
98    /// <summary>
99    /// Fires a new <c>ValueChanged</c> even.
100    /// </summary>
101    private void OnValueChanged() {
102      if (ValueChanged != null)
103        ValueChanged(this, new EventArgs());
104      OnChanged();
105    }
106
107    private void Value_Changed(object sender, ChangedEventArgs e) {
108      OnChanged(e);
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.