source: trunk/sources/HeuristicLab.Parameters/3.3/Parameter.cs @ 7259

Last change on this file since 7259 was 7259, checked in by swagner, 11 years ago

Updated year of copyrights to 2012 (#1716)

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Drawing;
24using System.Threading;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Parameters {
30  /// <summary>
31  /// A base class for parameters.
32  /// </summary>
33  [Item("Parameter", "A base class for parameters.")]
34  [StorableClass]
35  public abstract class Parameter : NamedItem, IParameter, IStatefulItem {
36    public override Image ItemImage {
37      get {
38        if ((dataType != null) && (typeof(IOperator).IsAssignableFrom(dataType)))
39          return HeuristicLab.Common.Resources.VSImageLibrary.Method;
40        else
41          return base.ItemImage;
42      }
43    }
44    public override bool CanChangeName {
45      get { return false; }
46    }
47    public override bool CanChangeDescription {
48      get { return false; }
49    }
50
51    [Storable]
52    private Type dataType;
53    public Type DataType {
54      get { return dataType; }
55    }
56
57    [Storable(DefaultValue = false)]
58    private bool hidden;
59    public bool Hidden {
60      get { return hidden; }
61      set {
62        if (value != hidden) {
63          hidden = value;
64          OnHiddenChanged();
65        }
66      }
67    }
68
69    private Lazy<ThreadLocal<IItem>> cachedActualValues;
70    public IItem ActualValue {
71      get {
72        if (cachedActualValues.Value.Value == null) cachedActualValues.Value.Value = GetActualValue();
73        return cachedActualValues.Value.Value;
74      }
75      set {
76        cachedActualValues.Value.Value = value;
77        SetActualValue(value);
78      }
79    }
80    private Lazy<ThreadLocal<IExecutionContext>> executionContexts;
81    public IExecutionContext ExecutionContext {
82      get { return executionContexts.Value.Value; }
83      set {
84        if (value != executionContexts.Value.Value) {
85          executionContexts.Value.Value = value;
86          cachedActualValues.Value.Value = null;
87        }
88      }
89    }
90
91    [StorableConstructor]
92    protected Parameter(bool deserializing)
93      : base(deserializing) {
94      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
95      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
96    }
97    protected Parameter(Parameter original, Cloner cloner)
98      : base(original, cloner) {
99      dataType = original.dataType;
100      hidden = original.hidden;
101      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
102      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
103    }
104    protected Parameter()
105      : base("Anonymous") {
106      dataType = typeof(IItem);
107      hidden = false;
108      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
109      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
110    }
111    protected Parameter(string name, Type dataType)
112      : base(name) {
113      if (dataType == null) throw new ArgumentNullException();
114      this.dataType = dataType;
115      hidden = false;
116      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
117      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
118    }
119    protected Parameter(string name, string description, Type dataType)
120      : base(name, description) {
121      if (dataType == null) throw new ArgumentNullException();
122      this.dataType = dataType;
123      hidden = false;
124      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
125      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
126    }
127
128    public virtual void InitializeState() { }
129    public virtual void ClearState() {
130      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
131      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
132    }
133
134    public override string ToString() {
135      return Name;
136    }
137
138    protected abstract IItem GetActualValue();
139    protected abstract void SetActualValue(IItem value);
140
141    public event EventHandler HiddenChanged;
142    protected virtual void OnHiddenChanged() {
143      EventHandler handler = HiddenChanged;
144      if (handler != null) handler(this, EventArgs.Empty);
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.