Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5593 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 5.0 KB
RevLine 
[2653]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 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]
[2664]35  public abstract class Parameter : NamedItem, IParameter {
[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
57    private Lazy<ThreadLocal<IItem>> cachedActualValues;
[2757]58    public IItem ActualValue {
[2987]59      get {
[5193]60        if (cachedActualValues.Value.Value == null) cachedActualValues.Value.Value = GetActualValue();
61        return cachedActualValues.Value.Value;
[2987]62      }
63      set {
[5193]64        cachedActualValues.Value.Value = value;
[2987]65        SetActualValue(value);
66      }
[2757]67    }
[5193]68    private Lazy<ThreadLocal<IExecutionContext>> executionContexts;
[2834]69    public IExecutionContext ExecutionContext {
[5193]70      get { return executionContexts.Value.Value; }
[2757]71      set {
[5193]72        if (value != executionContexts.Value.Value) {
73          executionContexts.Value.Value = value;
74          cachedActualValues.Value.Value = null;
[2757]75        }
76      }
[2740]77    }
[2653]78
[4722]79    [StorableConstructor]
[5193]80    protected Parameter(bool deserializing)
81      : base(deserializing) {
82      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
83      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
84    }
[4722]85    protected Parameter(Parameter original, Cloner cloner)
86      : base(original, cloner) {
87      dataType = original.dataType;
[5193]88      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
89      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
[4722]90    }
[2664]91    protected Parameter()
[2653]92      : base("Anonymous") {
93      dataType = typeof(IItem);
[5193]94      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
95      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
[2653]96    }
[2756]97    protected Parameter(string name, Type dataType)
98      : base(name) {
99      if (dataType == null) throw new ArgumentNullException();
100      this.dataType = dataType;
[5193]101      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
102      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
[2756]103    }
[2664]104    protected Parameter(string name, string description, Type dataType)
[2653]105      : base(name, description) {
106      if (dataType == null) throw new ArgumentNullException();
107      this.dataType = dataType;
[5193]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    }
111
[2669]112    public override string ToString() {
[3688]113      return Name;
[2669]114    }
[2757]115
116    protected abstract IItem GetActualValue();
117    protected abstract void SetActualValue(IItem value);
[2653]118  }
119}
Note: See TracBrowser for help on using the repository browser.