Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Data/3.3/ValueTypeArray.cs @ 4662

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

#922

  • Refactored HeuristicLab.Data
  • Refactored HeuristicLab.Operators
File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 System.Collections;
24using System.Collections.Generic;
25using System.Drawing;
26using System.Linq;
27using System.Text;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Data {
33  [Item("ValueTypeArray", "An abstract base class for representing arrays of value types.")]
34  [StorableClass]
35  public abstract class ValueTypeArray<T> : Item, IEnumerable<T> where T : struct {
36    public override Image ItemImage {
37      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Class; }
38    }
39
40    [Storable]
41    protected T[] array;
42
43    public virtual int Length {
44      get { return array.Length; }
45      protected set {
46        if (ReadOnly) throw new NotSupportedException("Length cannot be set. ValueTypeArray is read-only.");
47        if (value != Length) {
48          Array.Resize<T>(ref array, value);
49          OnReset();
50        }
51      }
52    }
53    public virtual T this[int index] {
54      get { return array[index]; }
55      set {
56        if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeArray is read-only.");
57        if (!value.Equals(array[index])) {
58          array[index] = value;
59          OnItemChanged(index);
60        }
61      }
62    }
63
64    [Storable]
65    protected bool readOnly;
66    public virtual bool ReadOnly {
67      get { return readOnly; }
68    }
69
70    [StorableConstructor]
71    protected ValueTypeArray(bool deserializing) : base(deserializing) { }
72    protected ValueTypeArray(ValueTypeArray<T> original, Cloner cloner)
73      : base(original, cloner) {
74      this.array = (T[])original.array.Clone();
75      this.readOnly = original.readOnly;
76    }
77    protected ValueTypeArray() {
78      array = new T[0];
79      readOnly = false;
80    }
81    protected ValueTypeArray(int length) {
82      array = new T[length];
83      readOnly = false;
84    }
85    protected ValueTypeArray(T[] elements) {
86      if (elements == null) throw new ArgumentNullException();
87      array = (T[])elements.Clone();
88      readOnly = false;
89    }
90
91    public virtual ValueTypeArray<T> AsReadOnly() {
92      ValueTypeArray<T> readOnlyValueTypeArray = (ValueTypeArray<T>)this.Clone();
93      readOnlyValueTypeArray.readOnly = true;
94      return readOnlyValueTypeArray;
95    }
96
97    public override string ToString() {
98      StringBuilder sb = new StringBuilder();
99      sb.Append("[");
100      if (array.Length > 0) {
101        sb.Append(array[0].ToString());
102        for (int i = 1; i < array.Length; i++)
103          sb.Append(";").Append(array[i].ToString());
104      }
105      sb.Append("]");
106      return sb.ToString();
107    }
108
109    public virtual IEnumerator<T> GetEnumerator() {
110      return array.Cast<T>().GetEnumerator();
111    }
112
113    IEnumerator IEnumerable.GetEnumerator() {
114      return GetEnumerator();
115    }
116
117    public event EventHandler<EventArgs<int>> ItemChanged;
118    protected virtual void OnItemChanged(int index) {
119      if (ItemChanged != null)
120        ItemChanged(this, new EventArgs<int>(index));
121      OnToStringChanged();
122    }
123    public event EventHandler Reset;
124    protected virtual void OnReset() {
125      if (Reset != null)
126        Reset(this, EventArgs.Empty);
127      OnToStringChanged();
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.