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