Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Data/3.3/StringConvertibleValueTuple.cs @ 5757

Last change on this file since 5757 was 5757, checked in by mkommend, 14 years ago

#1418: Implemented StringConvertibleValueTuple, IntRange, DoubleRange and the according views.

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Data {
28  [StorableClass]
29  [Item("StringConvertibleValueTuple<,>", "A generic abstract base class for representing multiple values of a string convertible value.")]
30  public abstract class StringConvertibleValueTuple<T, U> : Item, IStringConvertibleValueTuple
31    where T : class, IDeepCloneable, IStringConvertibleValue
32    where U : class, IDeepCloneable, IStringConvertibleValue {
33
34    [Storable]
35    protected bool readOnly;
36    public virtual bool ReadOnly {
37      get { return readOnly; }
38    }
39
40    [Storable(Name = "Values")]
41    protected Tuple<T, U> values;
42
43    protected T Item1 { get { return values.Item1; } }
44    IStringConvertibleValue IStringConvertibleValueTuple.Item1 { get { return Item1; } }
45    protected U Item2 { get { return values.Item2; } }
46    IStringConvertibleValue IStringConvertibleValueTuple.Item2 { get { return Item2; } }
47
48    [StorableConstructor]
49    protected StringConvertibleValueTuple(bool deserializing) : base(deserializing) { }
50    protected StringConvertibleValueTuple(StringConvertibleValueTuple<T, U> original, Cloner cloner)
51      : base(original, cloner) {
52      T item1 = (T)cloner.Clone(original.values.Item1);
53      U item2 = (U)cloner.Clone(original.values.Item2);
54      values = Tuple.Create<T, U>(item1, item2);
55    }
56
57    public StringConvertibleValueTuple()
58      : base() {
59      values = Tuple.Create<T, U>(default(T), default(U));
60      readOnly = false;
61      RegisterEventHandler();
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
70    public abstract StringConvertibleValueTuple<T, U> AsReadOnly();
71
72    public override string ToString() {
73      return string.Format("Item1: {0}, Item2: {1}", Item1, Item2);
74    }
75
76    private void RegisterEventHandler() {
77      Item1.ValueChanged += Item_ValueChanged;
78      Item2.ValueChanged += Item_ValueChanged;
79    }
80
81    private void Item_ValueChanged(object sender, EventArgs e) {
82      OnValueChanged();
83    }
84
85    public event EventHandler ValueChanged;
86    protected virtual void OnValueChanged() {
87      if (ValueChanged != null)
88        ValueChanged(this, EventArgs.Empty);
89      OnToStringChanged();
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.