- Timestamp:
- 01/29/13 14:39:02 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Parameters/3.3/LookupParameter.cs
r7259 r9195 21 21 22 22 using System; 23 using System.Threading; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 31 32 [Item("LookupParameter", "A parameter whose value is retrieved from or written to a scope.")] 32 33 [StorableClass] 33 public class LookupParameter<T> : Parameter, I LookupParameter<T> where T : class, IItem {34 public class LookupParameter<T> : Parameter, IStatefulItem, ILookupParameter<T> where T : class, IItem { 34 35 [Storable] 35 36 private string actualName; … … 59 60 } 60 61 62 private Lazy<ThreadLocal<IItem>> cachedActualValues; 63 private IItem CachedActualValue { 64 get { return cachedActualValues.Value.Value; } 65 } 66 67 private Lazy<ThreadLocal<IExecutionContext>> executionContexts; 68 public IExecutionContext ExecutionContext { 69 get { return executionContexts.Value.Value; } 70 set { 71 if (value != executionContexts.Value.Value) { 72 executionContexts.Value.Value = value; 73 cachedActualValues.Value.Value = null; 74 } 75 } 76 } 77 61 78 [StorableConstructor] 62 protected LookupParameter(bool deserializing) : base(deserializing) { } 79 protected LookupParameter(bool deserializing) 80 : base(deserializing) { 81 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 82 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 83 } 63 84 protected LookupParameter(LookupParameter<T> original, Cloner cloner) 64 85 : base(original, cloner) { 65 86 actualName = original.actualName; 87 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 88 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 66 89 } 67 90 public LookupParameter() … … 69 92 this.actualName = Name; 70 93 this.Hidden = true; 94 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 95 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 71 96 } 72 97 public LookupParameter(string name) … … 74 99 this.actualName = Name; 75 100 this.Hidden = true; 101 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 102 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 76 103 } 77 104 public LookupParameter(string name, string description) … … 79 106 this.actualName = Name; 80 107 this.Hidden = true; 108 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 109 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 81 110 } 82 111 public LookupParameter(string name, string description, string actualName) … … 84 113 this.actualName = string.IsNullOrWhiteSpace(actualName) ? Name : actualName; 85 114 this.Hidden = true; 115 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 116 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 86 117 } 87 118 … … 132 163 } 133 164 protected override IItem GetActualValue() { 165 if (CachedActualValue != null) return CachedActualValue; 134 166 string name; 135 167 // try to get value from context stack … … 146 178 typeof(T).GetPrettyName()) 147 179 ); 180 cachedActualValues.Value.Value = var.Value; 148 181 return var.Value; 149 182 } … … 156 189 typeof(T).GetPrettyName()) 157 190 ); 191 cachedActualValues.Value.Value = value; 192 158 193 // try to set value in context stack 159 194 string name; … … 175 210 } 176 211 212 public virtual void InitializeState() { 213 } 214 public virtual void ClearState() { 215 if (cachedActualValues.IsValueCreated) { 216 cachedActualValues.Value.Dispose(); 217 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 218 } 219 if (executionContexts.IsValueCreated) { 220 executionContexts.Value.Dispose(); 221 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 222 } 223 } 224 177 225 public event EventHandler ActualNameChanged; 178 226 protected virtual void OnActualNameChanged() {
Note: See TracChangeset
for help on using the changeset viewer.