Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/07/14 08:32:58 (10 years ago)
Author:
swagner
Message:

#2205: Worked on optimization networks

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Port.cs

    r11406 r11421  
    2929  [Item("Port", "Abstract base class for ports of an optimization network node.")]
    3030  [StorableClass]
    31   public abstract class Port<T> : NamedItem, IPort<T> where T : class, IItem {
     31  public abstract class Port : NamedItem, IPort {
    3232    public static new Image StaticItemImage {
    3333      get { return HeuristicLab.Common.Resources.VSImageLibrary.Field; }
     
    3535
    3636    [Storable]
    37     protected T value;
    38     public virtual T Value {
    39       get { return value; }
    40       protected set {
    41         DeregisterValueEvents();
    42         this.value = value;
    43         RegisterValueEvents();
    44         OnValueChanged();
     37    private INode node;
     38    public INode Node {
     39      get { return node; }
     40      set {
     41        if ((value != null) && !value.Ports.Contains(this))
     42          throw new InvalidOperationException("Port is not contained in port collection of node.");
     43        if ((value == null) && node.Ports.Contains(this))
     44          throw new InvalidOperationException("Port is still contained in port collection of node.");
     45        node = value;
    4546      }
    46     }
    47     object IPort.Value {
    48       get { return Value; }
    4947    }
    5048
    5149    [StorableConstructor]
    5250    protected Port(bool deserializing) : base(deserializing) { }
    53     protected Port(Port<T> original, Cloner cloner)
     51    protected Port(Port original, Cloner cloner)
    5452      : base(original, cloner) {
    55       value = cloner.Clone(original.value);
    56       RegisterValueEvents();
     53      node = (INode)cloner.GetClone(original.node);  // NOTE: original node is NOT automatically cloned
    5754    }
    5855    protected Port() : base("Port") { }
    5956    protected Port(string name) : base(name) { }
    6057    protected Port(string name, string description) : base(name, description) { }
    61 
    62     [StorableHook(HookType.AfterDeserialization)]
    63     private void AfterDeserialization() {
    64       RegisterValueEvents();
    65     }
    66 
    67     public override string ToString() {
    68       return Name + ": " + (Value != null ? Value.ToString() : "null");
    69     }
    70 
    71     public event EventHandler ValueChanged;
    72     protected virtual void OnValueChanged() {
    73       var handler = ValueChanged;
    74       if (handler != null) handler(this, EventArgs.Empty);
    75       OnToStringChanged();
    76     }
    77 
    78     protected virtual void RegisterValueEvents() {
    79       if (value != null) {
    80         value.ToStringChanged += new EventHandler(Value_ToStringChanged);
    81       }
    82     }
    83     protected virtual void DeregisterValueEvents() {
    84       if (value != null) {
    85         value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
    86       }
    87     }
    88     private void Value_ToStringChanged(object sender, EventArgs e) {
    89       OnToStringChanged();
    90     }
    9158  }
    9259}
Note: See TracChangeset for help on using the changeset viewer.