1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using System.Drawing;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 | using HeuristicLab.Common;
|
---|
10 | using HeuristicLab.PluginInfrastructure;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
13 | [Item("ConstrainedTypeValue", "Represents a type with constraints.")]
|
---|
14 | [StorableClass]
|
---|
15 | public abstract class ConstrainedTypeValue : TypeValue {
|
---|
16 |
|
---|
17 | [Storable]
|
---|
18 | 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);
|
---|
52 | this.Value = ValidTypes.First();
|
---|
53 | }
|
---|
54 | public ConstrainedTypeValue(Type value) : base() {
|
---|
55 | this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true);
|
---|
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 | } |
---|