[5313] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
[7153] | 4 | using HeuristicLab.Common;
|
---|
[5313] | 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]
|
---|
[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 | } |
---|