using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Core; using HeuristicLab.Data; using System.Drawing; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Common; using HeuristicLab.PluginInfrastructure; namespace HeuristicLab.Problems.MetaOptimization { [Item("ConstrainedTypeValue", "Represents a type with constraints.")] [StorableClass] public class ConstrainedTypeValue : TypeValue { [Storable] private IEnumerable validTypes; public IEnumerable ValidTypes { get { return validTypes; } set { validTypes = value; } } public override Type Value { set { if (!ValidTypes.Contains(value)) throw new NotSupportedException("Value cannot be set. Type is not enlisted in ValidTypes"); base.Value = value; } } public ConstrainedTypeValue() : base() { this.readOnly = false; } public ConstrainedTypeValue(Type value) : this() { this.Value = value; } [StorableConstructor] protected ConstrainedTypeValue(bool deserializing) : base(deserializing) { } protected ConstrainedTypeValue(ConstrainedTypeValue original, Cloner cloner) : base(original, cloner) { this.validTypes = new List(original.validTypes); } public override IDeepCloneable Clone(Cloner cloner) { return new ConstrainedTypeValue(this, cloner); } } [Item("ConstrainedTypeValue<>", "Represents a type with constraints.")] [StorableClass] public class ConstrainedTypeValue : ConstrainedTypeValue where T : class, IItem { public ConstrainedTypeValue() : base() { this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList(); this.Value = ValidTypes.First(); } public ConstrainedTypeValue(Type value) : base() { this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList(); this.Value = value; } [StorableConstructor] protected ConstrainedTypeValue(bool deserializing) : base(deserializing) { } protected ConstrainedTypeValue(ConstrainedTypeValue original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new ConstrainedTypeValue(this, cloner); } } }