BatchRemovalCollection.cs 1.8 KB

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