This repository has been archived by the owner on Aug 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 343
/
TrackingCollection.cs
82 lines (72 loc) · 3.48 KB
/
TrackingCollection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) 2014-2017 Silicon Studio Corp. All rights reserved. (https://www.siliconstudio.co.jp)
// See LICENSE.md for full license information.
using System;
using System.Collections.Specialized;
using SiliconStudio.Core.Serialization;
using SiliconStudio.Core.Serialization.Serializers;
namespace SiliconStudio.Core.Collections
{
/// <summary>
/// Represents a collection that generates events when items get added or removed.
/// </summary>
/// <typeparam name="T">The type of elements in the collection.</typeparam>
[DataSerializer(typeof(ListAllSerializer<,>), Mode = DataSerializerGenericMode.TypeAndGenericArguments)]
public class TrackingCollection<T> : FastCollection<T>, ITrackingCollectionChanged
{
private EventHandler<TrackingCollectionChangedEventArgs> itemAdded;
private EventHandler<TrackingCollectionChangedEventArgs> itemRemoved;
/// <inheritdoc/>
public event EventHandler<TrackingCollectionChangedEventArgs> CollectionChanged
{
add
{
// We keep a list in reverse order for removal, so that we can easily have multiple handlers depending on each others
itemAdded = (EventHandler<TrackingCollectionChangedEventArgs>)Delegate.Combine(itemAdded, value);
itemRemoved = (EventHandler<TrackingCollectionChangedEventArgs>)Delegate.Combine(value, itemRemoved);
}
remove
{
itemAdded = (EventHandler<TrackingCollectionChangedEventArgs>)Delegate.Remove(itemAdded, value);
itemRemoved = (EventHandler<TrackingCollectionChangedEventArgs>)Delegate.Remove(itemRemoved, value);
}
}
/// <inheritdoc/>
protected override void InsertItem(int index, T item)
{
base.InsertItem(index, item);
itemAdded?.Invoke(this, new TrackingCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, null, index, true));
}
/// <inheritdoc/>
protected override void RemoveItem(int index)
{
itemRemoved?.Invoke(this, new TrackingCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, this[index], null, index, true));
base.RemoveItem(index);
}
/// <inheritdoc/>
protected override void ClearItems()
{
ClearItemsEvents();
base.ClearItems();
}
protected void ClearItemsEvents()
{
// Note: Changing CollectionChanged is not thread-safe
var collectionChanged = itemRemoved;
if (collectionChanged != null)
{
for (var i = Count - 1; i >= 0; --i)
collectionChanged(this, new TrackingCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, this[i], null, i, true));
}
}
/// <inheritdoc/>
protected override void SetItem(int index, T item)
{
// Note: Changing CollectionChanged is not thread-safe
var collectionChangedRemoved = itemRemoved;
var oldItem = collectionChangedRemoved != null ? (object)this[index] : null;
collectionChangedRemoved?.Invoke(this, new TrackingCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldItem, null, index, false));
base.SetItem(index, item);
itemAdded?.Invoke(this, new TrackingCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, oldItem, index, false));
}
}
}