2
0

BatchRemovalCollection.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //-----------------------------------------------------------------------------
  2. // BatchRemovalCollection.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. namespace NetRumble
  10. {
  11. /// <summary>
  12. /// A collection with an additional feature to isolate the
  13. /// removal of the objects.
  14. /// </summary>
  15. public class BatchRemovalCollection<T> : List<T>
  16. {
  17. /// <summary>
  18. /// The list of objects to be removed on the next ApplyPendingRemovals().
  19. /// </summary>
  20. private List<T> pendingRemovals;
  21. /// <summary>
  22. /// Constructs a new collection.
  23. /// </summary>
  24. public BatchRemovalCollection()
  25. {
  26. pendingRemovals = new List<T>();
  27. }
  28. /// <summary>
  29. /// Queue an item for removal.
  30. /// </summary>
  31. /// <param name="item"></param>
  32. public void QueuePendingRemoval(T item)
  33. {
  34. pendingRemovals.Add(item);
  35. }
  36. /// <summary>
  37. /// Remove all of the "garbage" objects from this collection.
  38. /// </summary>
  39. public void ApplyPendingRemovals()
  40. {
  41. for (int i = 0; i < pendingRemovals.Count; i++)
  42. {
  43. Remove(pendingRemovals[i]);
  44. }
  45. pendingRemovals.Clear();
  46. }
  47. }
  48. }