1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Parameters {
|
---|
28 | /// <summary>
|
---|
29 | /// A parameter whose value is defined it the parameter itself.
|
---|
30 | /// </summary>
|
---|
31 | [Item("ValueParameter<T>", "A parameter whose value is defined it the parameter itself.")]
|
---|
32 | public sealed class ValueParameter<T> : Parameter, IValueParameter<T> where T : class, IItem {
|
---|
33 | private T value;
|
---|
34 | [Storable]
|
---|
35 | public T Value {
|
---|
36 | get { return this.value; }
|
---|
37 | set {
|
---|
38 | if (value == null) throw new ArgumentNullException();
|
---|
39 | if (value != this.value) {
|
---|
40 | if (this.value != null) this.value.Changed -= new ChangedEventHandler(Value_Changed);
|
---|
41 | this.value = value;
|
---|
42 | this.value.Changed += new ChangedEventHandler(Value_Changed);
|
---|
43 | OnValueChanged();
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }
|
---|
47 | IItem IValueParameter.Value {
|
---|
48 | get { return Value; }
|
---|
49 | set {
|
---|
50 | if (value == null) throw new ArgumentNullException();
|
---|
51 | T val = value as T;
|
---|
52 | if (val == null)
|
---|
53 | throw new InvalidOperationException(
|
---|
54 | string.Format("Type mismatch. Value is not a \"{0}\".",
|
---|
55 | typeof(T).GetPrettyName())
|
---|
56 | );
|
---|
57 | Value = val;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | private ValueParameter()
|
---|
62 | : base("Anonymous", typeof(T)) {
|
---|
63 | }
|
---|
64 | public ValueParameter(string name, T value)
|
---|
65 | : base(name, typeof(T)) {
|
---|
66 | Value = value;
|
---|
67 | }
|
---|
68 | public ValueParameter(string name, string description, T value)
|
---|
69 | : base(name, description, typeof(T)) {
|
---|
70 | Value = value;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
74 | ValueParameter<T> clone = new ValueParameter<T>(name, description, value);
|
---|
75 | cloner.RegisterClonedObject(this, clone);
|
---|
76 | clone.Value = (T)cloner.Clone(value);
|
---|
77 | return clone;
|
---|
78 | }
|
---|
79 |
|
---|
80 | public override string ToString() {
|
---|
81 | return string.Format("{0}: {1} ({2})", Name, Value.ToString(), DataType.GetPrettyName());
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected override IItem GetActualValue() {
|
---|
85 | return Value;
|
---|
86 | }
|
---|
87 | protected override void SetActualValue(IItem value) {
|
---|
88 | ((IValueParameter)this).Value = value;
|
---|
89 | }
|
---|
90 |
|
---|
91 | public event EventHandler ValueChanged;
|
---|
92 | private void OnValueChanged() {
|
---|
93 | if (ValueChanged != null)
|
---|
94 | ValueChanged(this, EventArgs.Empty);
|
---|
95 | OnChanged();
|
---|
96 | }
|
---|
97 | private void Value_Changed(object sender, ChangedEventArgs e) {
|
---|
98 | OnChanged(e);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|