Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1877 fixed NullReferenceExceptions in MetaOpt detected by TestCloningAllDeepCloneables

File size: 2.5 KB
RevLine 
[5313]1using System;
2using System.Collections.Generic;
3using System.Linq;
[7153]4using HeuristicLab.Common;
[5313]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]
[5337]12  public class ConstrainedTypeValue : TypeValue {
[5313]13
[5328]14    private IEnumerable<Type> validTypes;
[5313]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
[7153]27    public ConstrainedTypeValue()
28      : base() {
[5313]29      this.readOnly = false;
30    }
[7153]31    public ConstrainedTypeValue(Type value)
32      : this() {
[5313]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) {
[8091]40      if (original.validTypes != null)
41        this.validTypes = new List<Type>(original.validTypes);
[5313]42    }
[5337]43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new ConstrainedTypeValue(this, cloner);
45    }
[5313]46  }
47
48  [Item("ConstrainedTypeValue<>", "Represents a type with constraints.")]
49  [StorableClass]
50  public class ConstrainedTypeValue<T> : ConstrainedTypeValue where T : class, IItem {
51
[7153]52    public ConstrainedTypeValue()
53      : base() {
[5328]54      this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
[5313]55      this.Value = ValidTypes.First();
56    }
[7153]57    public ConstrainedTypeValue(Type value)
58      : base() {
[5328]59      this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
[5313]60      this.Value = value;
61    }
[7153]62
[5337]63    [StorableConstructor]
[7153]64    protected ConstrainedTypeValue(bool deserializing)
65      : base(deserializing) {
66      if (deserializing) {
67        this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
68      }
69    }
70
[5313]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.