Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5328 was 5328, checked in by cneumuel, 13 years ago

#1215

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