WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
ExtList.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 
5 namespace DirectOutput.General.Generic
6 {
11  public class ExtList<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, ICollection, IList
12  {
13  private List<T> _InternalList = new List<T>();
14 
15 
21  public int IndexOf(T Item)
22  {
23 
24  return _InternalList.IndexOf(Item);
25  }
26 
32  public void CopyTo(T[] Array, int ArrayIndex)
33  {
34  _InternalList.CopyTo(Array, ArrayIndex);
35  }
36 
37 
43  public bool IsReadOnly
44  {
45  get { return false; }
46  }
47 
48 
52  public IEnumerator<T> GetEnumerator()
53  {
54  return _InternalList.GetEnumerator();
55  }
56 
57 
61  IEnumerator IEnumerable.GetEnumerator()
62  {
63  return GetEnumerator();
64  }
65 
70  public void Add(T NewListItem)
71  {
72  if (BeforeInsert != null)
73  {
74  BeforeInsert(this, new InsertEventArgs<T>(-1, NewListItem));
75  }
76  _InternalList.Add(NewListItem);
77  if (AfterInsert != null)
78  {
79  AfterInsert(this, new InsertEventArgs<T>(_InternalList.IndexOf(NewListItem), NewListItem));
80  }
81  }
82 
83 
88  public void AddRange(IEnumerable<T> Collection)
89  {
90  foreach (T Item in Collection)
91  {
92  Add(Item);
93  }
94  }
95 
96 
101  public ExtList<T> Clone()
102  {
103  ExtList<T> L = new ExtList<T>();
104  foreach (T Item in this)
105  {
106  L.Add(Item);
107  }
108  return L;
109  }
110 
111 
112 
118  public void Insert(int Index, T Item)
119  {
120  if (BeforeInsert != null)
121  {
122  BeforeInsert(this, new InsertEventArgs<T>(Index, Item));
123  }
124 
125  _InternalList.Insert(Index, Item);
126 
127  if (AfterInsert != null)
128  {
129  AfterInsert(this, new InsertEventArgs<T>(Index, Item));
130  }
131 
132 
133  }
134 
135 
136 
140  public void Clear()
141  {
142  if (BeforeClear != null)
143  {
144  BeforeClear(this, new EventArgs());
145  }
146  _InternalList.Clear();
147  if (AfterClear != null)
148  {
149  AfterClear(this, new EventArgs());
150  }
151  }
152 
153 
154 
158  public int Count
159  {
160  get { return _InternalList.Count; }
161  }
162 
163 
164 
169  public bool Remove(T ItemToRemove)
170  {
171  bool ItemRemoved = false;
172  if (_InternalList.Contains(ItemToRemove))
173  {
174  int Index = _InternalList.IndexOf(ItemToRemove);
175  if (BeforeRemove != null)
176  {
177  BeforeRemove(this, new RemoveEventArgs<T>(Index, ItemToRemove));
178  }
179 
180  _InternalList.Remove(ItemToRemove);
181  ItemRemoved = true;
182  if (AfterRemove != null)
183  {
184  AfterRemove(this, new RemoveEventArgs<T>(Index, ItemToRemove));
185  }
186  }
187  return ItemRemoved;
188  }
189 
190 
195  public void RemoveAt(int Index)
196  {
197  T ItemToRemove = this[Index];
198  if (BeforeRemove != null)
199  {
200  BeforeRemove(this, new RemoveEventArgs<T>(Index, ItemToRemove));
201  }
202 
203  _InternalList.RemoveAt(Index);
204 
205  if (AfterRemove != null)
206  {
207  AfterRemove(this, new RemoveEventArgs<T>(Index, ItemToRemove));
208  }
209 
210  }
211 
212 
213 
219  public bool Contains(T ItemToCheck)
220  {
221  return _InternalList.Contains(ItemToCheck);
222  }
223 
224 
225 
232  public T this[int Index]
233  {
234 
235  get { return _InternalList[Index]; }
236  set
237  {
238  T OldItem = _InternalList[Index];
239  if (BeforeSet != null)
240  {
241  BeforeSet(this, new SetEventArgs<T>(Index, OldItem, value));
242  }
243 
244  _InternalList[Index] = value;
245  if (AfterSet != null)
246  {
247  AfterSet(this, new SetEventArgs<T>(Index, OldItem, value));
248  }
249  }
250  }
251 
252 
256  public void Sort()
257  {
258  _InternalList.Sort();
259  }
260 
261 
266  public void Sort(IComparer<T> Comparer)
267  {
268  _InternalList.Sort(Comparer);
269  }
274  public void Sort(Comparison<T> Comparison)
275  {
276  _InternalList.Sort(Comparison);
277  }
278 
279 
280 
285  public T[] ToArray()
286  {
287  return _InternalList.ToArray();
288  }
289 
290 
291  #region Events
292  #region Clear Events
293 
294 
299  public event EventHandler<EventArgs> BeforeClear;
300 
301 
302 
306  public event EventHandler<EventArgs> AfterClear;
307 
308 
309  #endregion
310  #region Insert Events
311 
317  public event EventHandler<InsertEventArgs<T>> BeforeInsert;
318 
323  public event EventHandler<InsertEventArgs<T>> AfterInsert;
324 
325 
326  #endregion
327  #region Remove Events
328 
332  public event EventHandler<RemoveEventArgs<T>> BeforeRemove;
333 
334 
338  public event EventHandler<RemoveEventArgs<T>> AfterRemove;
339 
340  #endregion
341  #region Set Events
342 
343 
348  public event EventHandler<SetEventArgs<T>> BeforeSet;
349 
350 
354  public event EventHandler<SetEventArgs<T>> AfterSet;
355 
356 
357 
358  #endregion
359 
360  #endregion
361 
362 
363  public ExtList()
364  {
365 
366  }
367 
368  public ExtList(IEnumerable<T> EnumerableList)
369  {
370  AddRange(EnumerableList);
371  }
372 
373 
374 
375  #region ICollection Member
376 
377  public void CopyTo(Array array, int index)
378  {
379  ((ICollection)_InternalList).CopyTo(array, index);
380  }
381 
382  public bool IsSynchronized
383  {
384  get { return ((ICollection)_InternalList).IsSynchronized; }
385  }
386 
387  public object SyncRoot
388  {
389  get { return ((ICollection)_InternalList).SyncRoot; }
390  }
391 
392  #endregion
393 
394  #region IList Member
395 
396 
397  public int Add(object value)
398  {
399  if(value.GetType() != typeof(T)) {
400  throw new ArgumentException(" Value is of a wrong type.");
401  }
402  Add((T)value);
403  return _InternalList.Count - 1;
404  }
405 
406 
407  public bool Contains(object value)
408  {
409  if (value.GetType() != typeof(T))
410  {
411  throw new ArgumentException(" Value is of a wrong type.");
412  }
413  return _InternalList.Contains((T)value);
414  }
415 
416  public int IndexOf(object value)
417  {
418  if (value.GetType() != typeof(T))
419  {
420  throw new ArgumentException(" Value is of a wrong type.");
421  }
422  return _InternalList.IndexOf((T)value);
423  }
424 
425  public void Insert(int index, object value)
426  {
427  if (value.GetType() != typeof(T))
428  {
429  throw new ArgumentException(" Value is of a wrong type.");
430  }
431  Insert(index, (T)value);
432  }
433 
434  public bool IsFixedSize
435  {
436  get { return ((IList)_InternalList).IsFixedSize; }
437  }
438 
439  public void Remove(object value)
440  {
441  if (value.GetType() != typeof(T))
442  {
443  throw new ArgumentException(" Value is of a wrong type.");
444  }
445  Remove((T)value);
446  }
447 
448  object IList.this[int index]
449  {
450  get
451  {
452  return _InternalList[index];
453  }
454  set
455  {
456  this[index] = (T)value;
457  }
458  }
459 
460  #endregion
461  }
462 
463 }
T[] ToArray()
Returns a array containg the ExtList items.
Definition: ExtList.cs:285
Event args for Set events.
Definition: SetEventArgs.cs:9
int IndexOf(T Item)
Determines the index of a specific item.
Definition: ExtList.cs:21
bool Contains(T ItemToCheck)
Checks wether the specified item is contained in the ExtList.
Definition: ExtList.cs:219
void Insert(int index, object value)
Definition: ExtList.cs:425
void Sort()
Sorts the ExtList.
Definition: ExtList.cs:256
ExtList(IEnumerable< T > EnumerableList)
Definition: ExtList.cs:368
void CopyTo(Array array, int index)
Definition: ExtList.cs:377
bool Remove(T ItemToRemove)
Romves a item from the ExtList.
Definition: ExtList.cs:169
EventHandler< SetEventArgs< T > > AfterSet
Fires after a item has been set in the ExtList.
Definition: ExtList.cs:354
void CopyTo(T[] Array, int ArrayIndex)
Copies the elements of the ExtList to an Array, starting at a particular Array index.
Definition: ExtList.cs:32
EventHandler< EventArgs > BeforeClear
Fires before the ExtList is cleared. If a exception is trown within the events, the list is not clea...
Definition: ExtList.cs:299
void AddRange(IEnumerable< T > Collection)
Adds a list of items to the ExtList.
Definition: ExtList.cs:88
void Clear()
Clears the ExtList.
Definition: ExtList.cs:140
void Sort(IComparer< T > Comparer)
Sorts the ExtList.
Definition: ExtList.cs:266
void Sort(Comparison< T > Comparison)
Sorts the elements in the entire ExtList using the specified System.Comparison.
Definition: ExtList.cs:274
EventHandler< InsertEventArgs< T > > BeforeInsert
Fires before a new item is inserted into the ExtList. If a exception is occurs in the event...
Definition: ExtList.cs:317
IEnumerator< T > GetEnumerator()
Returns an enumerator that iterates through a ExtList.
Definition: ExtList.cs:52
ExtList< T > Clone()
Creates a clone of the ExtList
Definition: ExtList.cs:101
void Insert(int Index, T Item)
Inserts an item to the ExtList at the specified Index.
Definition: ExtList.cs:118
EventHandler< RemoveEventArgs< T > > AfterRemove
Fires after a item is removed from the ExtList.
Definition: ExtList.cs:338
Extended version of the generic List class supporting events for various actions on the list...
Definition: ExtList.cs:11
EventHandler< RemoveEventArgs< T > > BeforeRemove
Fires before a item is removed from the ExtList.
Definition: ExtList.cs:332
Eventargs of BeforeInsert and AfterInsert events.
EventHandler< SetEventArgs< T > > BeforeSet
Fires before a item is set in the ExtList. OnValidate is called prior to this method.
Definition: ExtList.cs:348
void Add(T NewListItem)
Adds a new item to the ExtList.
Definition: ExtList.cs:70
EventHandler< EventArgs > AfterClear
Fires after the ExtList is cleared.
Definition: ExtList.cs:306
EventHandler< InsertEventArgs< T > > AfterInsert
Fires after a new item is inserted into the ExtList. OnValidate is called prior to this method...
Definition: ExtList.cs:323
void RemoveAt(int Index)
Removes a item at a specified index.
Definition: ExtList.cs:195