Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/ValueTypeValue.cs @ 4203

Last change on this file since 4203 was 3822, checked in by swagner, 14 years ago

Implemented reviewers' comments (#863)

  • adapted item names of generic items to reflect the type of their generic parameters
File size: 2.7 KB
RevLine 
[2663]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2663]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;
[3306]23using System.Drawing;
[3376]24using HeuristicLab.Common;
[2663]25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Data {
[3822]29  [Item("ValueTypeValue", "An abstract base class for representing values of value types.")]
[3017]30  [StorableClass]
[3054]31  public abstract class ValueTypeValue<T> : Item where T : struct {
[3306]32    public override Image ItemImage {
33      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.ValueType; }
34    }
35
[2663]36    [Storable]
[3054]37    protected T value;
38    public virtual T Value {
[2663]39      get { return value; }
40      set {
[3430]41        if (ReadOnly) throw new NotSupportedException("Value cannot be set. ValueTypeValue is read-only.");
[2663]42        if (!value.Equals(this.value)) {
43          this.value = value;
[2932]44          OnValueChanged();
[2663]45        }
46      }
47    }
48
[3430]49    [Storable]
50    protected bool readOnly;
51    public virtual bool ReadOnly {
52      get { return readOnly; }
53    }
54
[3054]55    protected ValueTypeValue() {
[2669]56      this.value = default(T);
[3430]57      this.readOnly = false;
[2663]58    }
[3054]59    protected ValueTypeValue(T value) {
[2669]60      this.value = value;
[3430]61      this.readOnly = false;
[2669]62    }
[2663]63
64    public override IDeepCloneable Clone(Cloner cloner) {
[3048]65      ValueTypeValue<T> clone = (ValueTypeValue<T>)base.Clone(cloner);
[2663]66      clone.value = value;
[3430]67      clone.readOnly = readOnly;
[2663]68      return clone;
69    }
70
[3430]71    public virtual ValueTypeValue<T> AsReadOnly() {
72      ValueTypeValue<T> readOnlyValueTypeValue = (ValueTypeValue<T>)this.Clone();
73      readOnlyValueTypeValue.readOnly = true;
74      return readOnlyValueTypeValue;
75    }
76
[2663]77    public override string ToString() {
78      return value.ToString();
79    }
[2932]80
81    public event EventHandler ValueChanged;
82    protected virtual void OnValueChanged() {
83      if (ValueChanged != null)
84        ValueChanged(this, EventArgs.Empty);
85      OnToStringChanged();
86    }
[2663]87  }
88}
Note: See TracBrowser for help on using the repository browser.