Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated year of copyrights to 2012 (#1716)

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