Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Parameters/3.3/ParameterValueBinding.cs @ 4757

Last change on this file since 4757 was 4757, checked in by abeham, 13 years ago

#1258

  • worked on parameter binding (wip)
File size: 2.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6using HeuristicLab.Core;
7using HeuristicLab.Common;
8
9namespace HeuristicLab.Parameters {
10  [StorableClass]
11  public class ParameterValueBinding : IParameterBinding {
12    [Storable]
13    private IValueParameter recipient;
14    public IValueParameter Recipient {
15      get { return recipient; }
16    }
17    [Storable]
18    private IValueParameter donor;
19    public IValueParameter Donor {
20      get { return donor; }
21    }
22
23    [StorableConstructor]
24    protected ParameterValueBinding(bool deserializing) { }
25    protected ParameterValueBinding(ParameterValueBinding original, Cloner cloner) {
26      this.donor = cloner.Clone(donor);
27      this.recipient = cloner.Clone(recipient);
28      RegisterEventHandlers();
29    }
30    public ParameterValueBinding(IValueParameter recipient, IValueParameter donor) {
31      if (recipient == null) throw new ArgumentNullException("destination");
32      if (donor == null) throw new ArgumentNullException("source");
33      this.recipient = recipient;
34      this.donor = donor;
35      RegisterEventHandlers();
36    }
37
38    public override IDeepCloneable Clone(Cloner cloner) {
39      return new ParameterValueBinding(this, cloner);
40    }
41
42    public void Bind() {
43      RegisterEventHandlers();
44    }
45
46    public void Unbind() {
47      DeregisterEventHandlers();
48    }
49
50    [StorableHook(HookType.AfterDeserialization)]
51    private void AfterDeserialization() {
52      RegisterEventHandlers();
53    }
54
55    private void RegisterEventHandlers() {
56      donor.ValueChanged += new EventHandler(source_ValueChanged);
57    }
58
59    private void DeregisterEventHandlers() {
60      donor.ValueChanged -= new EventHandler(source_ValueChanged);
61    }
62   
63
64    private void source_ValueChanged(object sender, EventArgs e) {
65      recipient.Value = (IItem)donor.Value.Clone();
66    }
67  }
68}
Note: See TracBrowser for help on using the repository browser.