1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 | using HeuristicLab.PluginInfrastructure;
|
---|
8 |
|
---|
9 | namespace 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 | this.validTypes = new List<Type>(original.validTypes);
|
---|
41 | }
|
---|
42 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
43 | return new ConstrainedTypeValue(this, cloner);
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | [Item("ConstrainedTypeValue<>", "Represents a type with constraints.")]
|
---|
48 | [StorableClass]
|
---|
49 | public class ConstrainedTypeValue<T> : ConstrainedTypeValue where T : class, IItem {
|
---|
50 |
|
---|
51 | public ConstrainedTypeValue()
|
---|
52 | : base() {
|
---|
53 | this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
|
---|
54 | this.Value = ValidTypes.First();
|
---|
55 | }
|
---|
56 | public ConstrainedTypeValue(Type value)
|
---|
57 | : base() {
|
---|
58 | this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
|
---|
59 | this.Value = value;
|
---|
60 | }
|
---|
61 |
|
---|
62 | [StorableConstructor]
|
---|
63 | protected ConstrainedTypeValue(bool deserializing)
|
---|
64 | : base(deserializing) {
|
---|
65 | if (deserializing) {
|
---|
66 | this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | protected ConstrainedTypeValue(ConstrainedTypeValue original, Cloner cloner) : base(original, cloner) { }
|
---|
71 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
72 | return new ConstrainedTypeValue<T>(this, cloner);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | } |
---|