using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Core; using HeuristicLab.Common; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Data; namespace HeuristicLab.Problems.MetaOptimization { [StorableClass] public class ConstrainedValue : Item { [Storable] protected Type valueDataType; public Type ValueDataType { get { return this.valueDataType; } protected set { this.valueDataType = value; } } [Storable] protected IItem value; public IItem Value { get { return value; } set { if (this.value != value) { if(this.value != null) DeregisterEvents(); this.value = value; if(this.value != null) RegisterEvents(); OnToStringChanged(); } } } [Storable] protected Type[] validTypes; public Type[] ValidTypes { get { return validTypes; } protected set { if (this.validTypes != value) { this.validTypes = value; } } } [Storable] protected bool isNullable; public bool IsNullable { get { return isNullable; } protected set { if (this.isNullable != value) { this.isNullable = value; } } } private void RegisterEvents() { this.value.ToStringChanged += new EventHandler(value_ToStringChanged); } private void DeregisterEvents() { this.value.ToStringChanged -= new EventHandler(value_ToStringChanged); } #region constructors and cloning public ConstrainedValue() { } [StorableConstructor] protected ConstrainedValue(bool deserializing) : base(deserializing) { } public ConstrainedValue(IItem value, Type valueDataType, Type[] validTypes, bool isNullable) { this.Value = value; this.ValueDataType = valueDataType; this.ValidTypes = validTypes; this.isNullable = isNullable; } protected ConstrainedValue(ConstrainedValue original, Cloner cloner) : base(original, cloner) { this.valueDataType = original.valueDataType; this.Value = cloner.Clone(original.value); if(original.ValidTypes != null) this.ValidTypes = original.ValidTypes.ToArray(); this.isNullable = original.isNullable; } public override IDeepCloneable Clone(Cloner cloner) { return new ConstrainedValue(this, cloner); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { if (this.value != null) RegisterEvents(); } #endregion void value_ToStringChanged(object sender, EventArgs e) { OnToStringChanged(); } public override string ToString() { return value != null ? value.ToString() : base.ToString(); } } }