Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
TrackingCollection.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Collections.ObjectModel;
6 using System.Collections.Specialized;
7 using SiliconStudio.Core.Serialization.Serializers;
8 
9 namespace SiliconStudio.Core.Collections
10 {
11  public interface ITrackingCollectionChanged
12  {
13  event EventHandler<TrackingCollectionChangedEventArgs> CollectionChanged;
14  }
15 
16  /// <summary>
17  /// Overrides <see cref="Collection{T}"/> with value types enumerators to avoid allocation in foreach loops, and various helper functions.
18  /// </summary>
19  /// <typeparam name="T"></typeparam>
20  [DataSerializer(typeof(ListAllSerializer<,>), Mode = DataSerializerGenericMode.TypeAndGenericArguments)]
21  public class FastCollection<T> : Collection<T>
22  {
23  /// <summary>
24  /// Adds the elements of the specified source to the end of <see cref="FastCollection{T}"/>.
25  /// </summary>
26  /// <param name="items">The items.</param>
27  public void AddRange(IEnumerable<T> items)
28  {
29  foreach (var item in items)
30  {
31  Add(item);
32  }
33  }
34 
35  /// <summary>
36  /// Gets the enumerator.
37  /// </summary>
38  /// <returns>The enumerator</returns>
39  public new List<T>.Enumerator GetEnumerator()
40  {
41  // Assume the underlying collection is a list. (true when created with null constructor -> TODO improve this code in future)
42  return ((List<T>)Items).GetEnumerator();
43  }
44 
45  /// <summary>
46  /// Sorts the element in this <see cref="FastCollection{T}"/> using the default comparer.
47  /// </summary>
48  public void Sort()
49  {
50  //Assume the underlying collection is a list. (true when created with null constructor -> TODO improve this code in future)
51  ((List<T>)Items).Sort();
52  }
53 
54  /// <summary>
55  /// Sorts the element in this <see cref="FastCollection{T}"/> using the specified comparer.
56  /// </summary>
57  /// <param name="comparison">The comparison to use.</param>
58  public void Sort(Comparison<T> comparison)
59  {
60  //Assume the underlying collection is a list. (true when created with null constructor -> TODO improve this code in future)
61  ((List<T>)Items).Sort(comparison);
62  }
63 
64  /// <summary>
65  /// Sorts the element in this <see cref="FastCollection{T}"/> using the specified comparer.
66  /// </summary>
67  /// <param name="comparer">The comparer to use.</param>
68  public void Sort(Comparer<T> comparer)
69  {
70  //Assume the underlying collection is a list. (true when created with null constructor -> TODO improve this code in future)
71  ((List<T>)Items).Sort(comparer);
72  }
73  }
74 
75  /// <summary>
76  /// Represents a collection that generates events when items get added or removed.
77  /// </summary>
78  /// <typeparam name="T">The type of elements in the collection.</typeparam>
79  [DataSerializer(typeof(ListAllSerializer<,>), Mode = DataSerializerGenericMode.TypeAndGenericArguments)]
80  public class TrackingCollection<T> : FastCollection<T>, ITrackingCollectionChanged
81  {
82  /// <inheritdoc/>
83  public event EventHandler<TrackingCollectionChangedEventArgs> CollectionChanged;
84 
85  /// <inheritdoc/>
86  protected override void InsertItem(int index, T item)
87  {
88  base.InsertItem(index, item);
89  var collectionChanged = CollectionChanged;
90  if (collectionChanged != null)
91  collectionChanged(this, new TrackingCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, null, index, true));
92  }
93 
94  /// <inheritdoc/>
95  protected override void RemoveItem(int index)
96  {
97  var collectionChanged = CollectionChanged;
98  if (collectionChanged != null)
99  collectionChanged(this, new TrackingCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, Items[index], null, index, true));
100  base.RemoveItem(index);
101  }
102 
103  /// <inheritdoc/>
104  protected override void ClearItems()
105  {
106  var collectionChanged = CollectionChanged;
107  if (collectionChanged != null)
108  {
109  for (int i = Items.Count - 1; i >= 0; --i)
110  collectionChanged(this, new TrackingCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, Items[i], null, i, true));
111  }
112  base.ClearItems();
113  }
114 
115  /// <inheritdoc/>
116  protected override void SetItem(int index, T item)
117  {
118  var collectionChanged = CollectionChanged;
119  object oldItem = (collectionChanged != null) ? (object)Items[index] : null;
120  if (collectionChanged != null)
121  collectionChanged(this, new TrackingCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldItem, null, index, false));
122  base.SetItem(index, item);
123  if (collectionChanged != null)
124  collectionChanged(this, new TrackingCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, oldItem, index, false));
125  }
126  }
127 }
DataSerializerGenericMode
Defines what generic parameters to pass to the serializer.
void AddRange(IEnumerable< T > items)
Adds the elements of the specified source to the end of FastCollection{T}.
void Sort()
Sorts the element in this FastCollection{T} using the default comparer.
EventHandler< TrackingCollectionChangedEventArgs > CollectionChanged
EventHandler< TrackingCollectionChangedEventArgs > CollectionChanged
void Sort(Comparer< T > comparer)
Sorts the element in this FastCollection{T} using the specified comparer.
void Sort(Comparison< T > comparison)
Sorts the element in this FastCollection{T} using the specified comparer.
new List< T >.Enumerator GetEnumerator()
Gets the enumerator.