Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/ConstrainedTypeValue.cs @ 12779

Last change on this file since 12779 was 8091, checked in by ascheibe, 12 years ago

#1877 fixed NullReferenceExceptions in MetaOpt detected by TestCloningAllDeepCloneables

File size: 2.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7using HeuristicLab.PluginInfrastructure;
8
9namespace HeuristicLab.Problems.MetaOptimization {
10  [Item("ConstrainedTypeValue", "Represents a type with constraints.")]
11  [StorableClass]
12  public class ConstrainedTypeValue : TypeValue {
13
14    private IEnumerable<Type> validTypes;
15    public IEnumerable<Type> ValidTypes {
16      get { return validTypes; }
17      set { validTypes = value; }
18    }
19
20    public override Type Value {
21      set {
22        if (!ValidTypes.Contains(value)) throw new NotSupportedException("Value cannot be set. Type is not enlisted in ValidTypes");
23        base.Value = value;
24      }
25    }
26
27    public ConstrainedTypeValue()
28      : base() {
29      this.readOnly = false;
30    }
31    public ConstrainedTypeValue(Type value)
32      : this() {
33      this.Value = value;
34    }
35
36    [StorableConstructor]
37    protected ConstrainedTypeValue(bool deserializing) : base(deserializing) { }
38    protected ConstrainedTypeValue(ConstrainedTypeValue original, Cloner cloner)
39      : base(original, cloner) {
40      if (original.validTypes != null)
41        this.validTypes = new List<Type>(original.validTypes);
42    }
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new ConstrainedTypeValue(this, cloner);
45    }
46  }
47
48  [Item("ConstrainedTypeValue<>", "Represents a type with constraints.")]
49  [StorableClass]
50  public class ConstrainedTypeValue<T> : ConstrainedTypeValue where T : class, IItem {
51
52    public ConstrainedTypeValue()
53      : base() {
54      this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
55      this.Value = ValidTypes.First();
56    }
57    public ConstrainedTypeValue(Type value)
58      : base() {
59      this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
60      this.Value = value;
61    }
62
63    [StorableConstructor]
64    protected ConstrainedTypeValue(bool deserializing)
65      : base(deserializing) {
66      if (deserializing) {
67        this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
68      }
69    }
70
71    protected ConstrainedTypeValue(ConstrainedTypeValue original, Cloner cloner) : base(original, cloner) { }
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new ConstrainedTypeValue<T>(this, cloner);
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.