Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Data/3.3/StringConvertibleValueTuple.cs @ 17456

Last change on this file since 17456 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 3.4 KB
RevLine 
[5757]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5757]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
22using System;
[5774]23using System.Drawing;
[5757]24using HeuristicLab.Common;
25using HeuristicLab.Core;
[17097]26using HEAL.Attic;
[5757]27
28namespace HeuristicLab.Data {
[17097]29  [StorableType("06A7697C-2C25-49AF-95A2-DDB14D2F7B81")]
[5757]30  [Item("StringConvertibleValueTuple<,>", "A generic abstract base class for representing multiple values of a string convertible value.")]
31  public abstract class StringConvertibleValueTuple<T, U> : Item, IStringConvertibleValueTuple
32    where T : class, IDeepCloneable, IStringConvertibleValue
33    where U : class, IDeepCloneable, IStringConvertibleValue {
[7201]34    public static new Image StaticItemImage {
[5774]35      get { return HeuristicLab.Common.Resources.VSImageLibrary.ValueType; }
36    }
[5757]37
38    [Storable]
39    protected bool readOnly;
40    public virtual bool ReadOnly {
41      get { return readOnly; }
42    }
43
44    [Storable(Name = "Values")]
45    protected Tuple<T, U> values;
46
47    protected T Item1 { get { return values.Item1; } }
48    IStringConvertibleValue IStringConvertibleValueTuple.Item1 { get { return Item1; } }
49    protected U Item2 { get { return values.Item2; } }
50    IStringConvertibleValue IStringConvertibleValueTuple.Item2 { get { return Item2; } }
51
52    [StorableConstructor]
[17097]53    protected StringConvertibleValueTuple(StorableConstructorFlag _) : base(_) {
[5770]54    }
[5757]55    protected StringConvertibleValueTuple(StringConvertibleValueTuple<T, U> original, Cloner cloner)
56      : base(original, cloner) {
57      T item1 = (T)cloner.Clone(original.values.Item1);
58      U item2 = (U)cloner.Clone(original.values.Item2);
59      values = Tuple.Create<T, U>(item1, item2);
[5770]60      RegisterEventHandler();
[5757]61    }
62
63    public StringConvertibleValueTuple(T item1, U item2)
64      : base() {
65      values = Tuple.Create<T, U>(item1, item2);
66      readOnly = false;
67      RegisterEventHandler();
68    }
69
[5791]70    [StorableHook(HookType.AfterDeserialization)]
71    private void AfterDeserialization() {
72      RegisterEventHandler();
73    }
74
75
[5757]76    public abstract StringConvertibleValueTuple<T, U> AsReadOnly();
77
78    public override string ToString() {
79      return string.Format("Item1: {0}, Item2: {1}", Item1, Item2);
80    }
81
82    private void RegisterEventHandler() {
83      Item1.ValueChanged += Item_ValueChanged;
84      Item2.ValueChanged += Item_ValueChanged;
85    }
86
87    private void Item_ValueChanged(object sender, EventArgs e) {
88      OnValueChanged();
89    }
90
91    public event EventHandler ValueChanged;
92    protected virtual void OnValueChanged() {
93      if (ValueChanged != null)
94        ValueChanged(this, EventArgs.Empty);
95      OnToStringChanged();
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.