1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
25 | using System;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Optimization.Networks {
|
---|
28 | [Item("ValuePort", "Abstract base class for ports of an optimization network node which contain a single value.")]
|
---|
29 | [StorableClass]
|
---|
30 | public abstract class ValuePort<T> : Port, IValuePort<T> where T : class, IItem {
|
---|
31 | public Type DataType {
|
---|
32 | get { return typeof(T); }
|
---|
33 | }
|
---|
34 |
|
---|
35 | [Storable]
|
---|
36 | protected T value;
|
---|
37 | public virtual T Value {
|
---|
38 | get { return value; }
|
---|
39 | protected set {
|
---|
40 | DeregisterValueEvents();
|
---|
41 | this.value = value;
|
---|
42 | RegisterValueEvents();
|
---|
43 | OnValueChanged();
|
---|
44 | }
|
---|
45 | }
|
---|
46 | IItem IValuePort.Value {
|
---|
47 | get { return Value; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | [StorableConstructor]
|
---|
51 | protected ValuePort(bool deserializing) : base(deserializing) { }
|
---|
52 | protected ValuePort(ValuePort<T> original, Cloner cloner)
|
---|
53 | : base(original, cloner) {
|
---|
54 | value = cloner.Clone(original.value);
|
---|
55 | RegisterValueEvents();
|
---|
56 | }
|
---|
57 | protected ValuePort() : base("ValuePort") { }
|
---|
58 | protected ValuePort(string name) : base(name) { }
|
---|
59 | protected ValuePort(string name, string description) : base(name, description) { }
|
---|
60 |
|
---|
61 | [StorableHook(HookType.AfterDeserialization)]
|
---|
62 | private void AfterDeserialization() {
|
---|
63 | RegisterValueEvents();
|
---|
64 | }
|
---|
65 |
|
---|
66 | public override string ToString() {
|
---|
67 | return base.ToString() + ": " + (Value != null ? Value.ToString() : "null");
|
---|
68 | }
|
---|
69 |
|
---|
70 | public event EventHandler ValueChanged;
|
---|
71 | protected virtual void OnValueChanged() {
|
---|
72 | var handler = ValueChanged;
|
---|
73 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
74 | OnToStringChanged();
|
---|
75 | }
|
---|
76 |
|
---|
77 | protected virtual void RegisterValueEvents() {
|
---|
78 | if (value != null) {
|
---|
79 | value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | protected virtual void DeregisterValueEvents() {
|
---|
83 | if (value != null) {
|
---|
84 | value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
|
---|
85 | }
|
---|
86 | }
|
---|
87 | private void Value_ToStringChanged(object sender, EventArgs e) {
|
---|
88 | OnToStringChanged();
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|