[2663] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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 |
|
---|
| 22 | using System;
|
---|
[3306] | 23 | using System.Drawing;
|
---|
[3376] | 24 | using HeuristicLab.Common;
|
---|
[2663] | 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 |
|
---|
| 28 | namespace 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 {
|
---|
[13656] | 32 | public static new Image StaticItemImage
|
---|
| 33 | {
|
---|
| 34 | get { return new Bitmap(25, 25); }
|
---|
[3306] | 35 | }
|
---|
| 36 |
|
---|
[2663] | 37 | [Storable]
|
---|
[3054] | 38 | protected T value;
|
---|
[13656] | 39 | public virtual T Value
|
---|
| 40 | {
|
---|
[2663] | 41 | get { return value; }
|
---|
[13656] | 42 | set
|
---|
| 43 | {
|
---|
[3430] | 44 | if (ReadOnly) throw new NotSupportedException("Value cannot be set. ValueTypeValue is read-only.");
|
---|
[2663] | 45 | if (!value.Equals(this.value)) {
|
---|
| 46 | this.value = value;
|
---|
[2932] | 47 | OnValueChanged();
|
---|
[2663] | 48 | }
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[3430] | 52 | [Storable]
|
---|
| 53 | protected bool readOnly;
|
---|
[13656] | 54 | public virtual bool ReadOnly
|
---|
| 55 | {
|
---|
[3430] | 56 | get { return readOnly; }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[4722] | 59 | [StorableConstructor]
|
---|
| 60 | protected ValueTypeValue(bool deserializing) : base(deserializing) { }
|
---|
| 61 | protected ValueTypeValue(ValueTypeValue<T> original, Cloner cloner)
|
---|
| 62 | : base(original, cloner) {
|
---|
| 63 | this.value = original.value;
|
---|
| 64 | this.readOnly = original.readOnly;
|
---|
| 65 | }
|
---|
[3054] | 66 | protected ValueTypeValue() {
|
---|
[2669] | 67 | this.value = default(T);
|
---|
[3430] | 68 | this.readOnly = false;
|
---|
[2663] | 69 | }
|
---|
[3054] | 70 | protected ValueTypeValue(T value) {
|
---|
[2669] | 71 | this.value = value;
|
---|
[3430] | 72 | this.readOnly = false;
|
---|
[2669] | 73 | }
|
---|
[2663] | 74 |
|
---|
[3430] | 75 | public virtual ValueTypeValue<T> AsReadOnly() {
|
---|
| 76 | ValueTypeValue<T> readOnlyValueTypeValue = (ValueTypeValue<T>)this.Clone();
|
---|
| 77 | readOnlyValueTypeValue.readOnly = true;
|
---|
| 78 | return readOnlyValueTypeValue;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[2663] | 81 | public override string ToString() {
|
---|
| 82 | return value.ToString();
|
---|
| 83 | }
|
---|
[2932] | 84 |
|
---|
| 85 | public event EventHandler ValueChanged;
|
---|
| 86 | protected virtual void OnValueChanged() {
|
---|
| 87 | if (ValueChanged != null)
|
---|
| 88 | ValueChanged(this, EventArgs.Empty);
|
---|
| 89 | OnToStringChanged();
|
---|
| 90 | }
|
---|
[2663] | 91 | }
|
---|
| 92 | }
|
---|