Changeset 17257 for branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/ContextLookupParameter.cs
- Timestamp:
- 09/17/19 14:05:47 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/ContextLookupParameter.cs
r17254 r17257 1 1 using System; 2 using System.Threading;3 2 using HEAL.Attic; 4 3 using HeuristicLab.Common; … … 8 7 [Item("ContextLookupParameter", "A parameter that looks up contexts by type.")] 9 8 [StorableType("4ac189c8-6cf3-48fd-bf79-392d35a872db")] 10 public class ContextLookupParameter<T> : Parameter, IContextLookupParameter<T>, IStatefulItem9 public class ContextLookupParameter<T> : ContextParameter, IContextLookupParameter<T> 11 10 where T : class, IParameterizedItem { 12 11 … … 15 14 } 16 15 17 private Lazy<ThreadLocal<IItem>> cachedActualValues;18 protected IItem CachedActualValue {19 get { return cachedActualValues.Value.Value; }20 set { cachedActualValues.Value.Value = value; }21 }22 23 private Lazy<ThreadLocal<IExecutionContext>> executionContexts;24 public IExecutionContext ExecutionContext {25 get { return executionContexts.Value.Value; }26 set {27 if (value != executionContexts.Value.Value) {28 executionContexts.Value.Value = value;29 cachedActualValues.Value.Value = null;30 }31 }32 }33 34 16 [StorableConstructor] 35 protected ContextLookupParameter(StorableConstructorFlag _) : base(_) { 36 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 37 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 38 } 39 protected ContextLookupParameter(ContextLookupParameter<T> original, Cloner cloner) 40 : base(original, cloner) { 41 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 42 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 43 } 44 public ContextLookupParameter() : this("Anonymous") { } 45 public ContextLookupParameter(string name, string description = null) 46 : base(name, description, typeof(T)) { 47 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 48 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 17 protected ContextLookupParameter(StorableConstructorFlag _) : base(_) { } 18 protected ContextLookupParameter(ContextLookupParameter<T> original, Cloner cloner) : base(original, cloner) { } 19 public ContextLookupParameter() : base("ContextLookup." + typeof(T).Name, string.Empty, typeof(T)) { 20 Hidden = true; 49 21 } 50 22 … … 53 25 } 54 26 55 protected override IItem GetActualValue() { 56 if (CachedActualValue != null) return CachedActualValue; 57 27 protected override IItem GetActualValueFromContext() { 58 28 IItem item = null; 59 29 var context = ExecutionContext; … … 65 35 context = context.Parent; 66 36 } 67 CachedActualValue = item;68 37 return item; 69 38 } … … 72 41 throw new NotSupportedException("The context lookup parameter may not be used to set an item."); 73 42 } 74 75 public virtual void InitializeState() {76 }77 public virtual void ClearState() {78 if (cachedActualValues.IsValueCreated) {79 cachedActualValues.Value.Dispose();80 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);81 }82 if (executionContexts.IsValueCreated) {83 executionContexts.Value.Dispose();84 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);85 }86 }87 43 } 88 44 }
Note: See TracChangeset
for help on using the changeset viewer.