[5313] | 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]
|
---|
[5337] | 15 | public class ConstrainedTypeValue : TypeValue {
|
---|
[5313] | 16 |
|
---|
| 17 | [Storable]
|
---|
[5328] | 18 | private IEnumerable<Type> validTypes;
|
---|
[5313] | 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 | }
|
---|
[5337] | 44 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 45 | return new ConstrainedTypeValue(this, cloner);
|
---|
| 46 | }
|
---|
[5313] | 47 | }
|
---|
| 48 |
|
---|
| 49 | [Item("ConstrainedTypeValue<>", "Represents a type with constraints.")]
|
---|
| 50 | [StorableClass]
|
---|
| 51 | public class ConstrainedTypeValue<T> : ConstrainedTypeValue where T : class, IItem {
|
---|
| 52 |
|
---|
| 53 | public ConstrainedTypeValue() : base() {
|
---|
[5328] | 54 | this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
|
---|
[5313] | 55 | this.Value = ValidTypes.First();
|
---|
| 56 | }
|
---|
| 57 | public ConstrainedTypeValue(Type value) : base() {
|
---|
[5328] | 58 | this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
|
---|
[5313] | 59 | this.Value = value;
|
---|
| 60 | }
|
---|
[5337] | 61 | [StorableConstructor]
|
---|
| 62 | protected ConstrainedTypeValue(bool deserializing) : base(deserializing) { }
|
---|
[5313] | 63 | protected ConstrainedTypeValue(ConstrainedTypeValue original, Cloner cloner) : base(original, cloner) { }
|
---|
| 64 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 65 | return new ConstrainedTypeValue<T>(this, cloner);
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | } |
---|