Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CallbackArray.cs 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. namespace UnityEngine.InputSystem.Utilities
  2. {
  3. // Keeps a copy of the callback list while executing so that the callback list can safely
  4. // be mutated from within callbacks.
  5. internal struct CallbackArray<TDelegate>
  6. where TDelegate : System.Delegate
  7. {
  8. private bool m_CannotMutateCallbacksArray;
  9. private InlinedArray<TDelegate> m_Callbacks;
  10. private InlinedArray<TDelegate> m_CallbacksToAdd;
  11. private InlinedArray<TDelegate> m_CallbacksToRemove;
  12. public int length => m_Callbacks.length;
  13. public TDelegate this[int index] => m_Callbacks[index];
  14. public void Clear()
  15. {
  16. m_Callbacks.Clear();
  17. m_CallbacksToAdd.Clear();
  18. m_CallbacksToRemove.Clear();
  19. }
  20. public void AddCallback(TDelegate dlg)
  21. {
  22. if (m_CannotMutateCallbacksArray)
  23. {
  24. if (m_CallbacksToAdd.Contains(dlg))
  25. return;
  26. var removeIndex = m_CallbacksToRemove.IndexOf(dlg);
  27. if (removeIndex != -1)
  28. m_CallbacksToRemove.RemoveAtByMovingTailWithCapacity(removeIndex);
  29. m_CallbacksToAdd.AppendWithCapacity(dlg);
  30. return;
  31. }
  32. if (!m_Callbacks.Contains(dlg))
  33. m_Callbacks.AppendWithCapacity(dlg, capacityIncrement: 4);
  34. }
  35. public void RemoveCallback(TDelegate dlg)
  36. {
  37. if (m_CannotMutateCallbacksArray)
  38. {
  39. if (m_CallbacksToRemove.Contains(dlg))
  40. return;
  41. var addIndex = m_CallbacksToAdd.IndexOf(dlg);
  42. if (addIndex != -1)
  43. m_CallbacksToAdd.RemoveAtByMovingTailWithCapacity(addIndex);
  44. m_CallbacksToRemove.AppendWithCapacity(dlg);
  45. return;
  46. }
  47. var index = m_Callbacks.IndexOf(dlg);
  48. if (index >= 0)
  49. m_Callbacks.RemoveAtWithCapacity(index);
  50. }
  51. public void LockForChanges()
  52. {
  53. m_CannotMutateCallbacksArray = true;
  54. }
  55. public void UnlockForChanges()
  56. {
  57. m_CannotMutateCallbacksArray = false;
  58. // Process mutations that have happened while we were executing callbacks.
  59. for (var i = 0; i < m_CallbacksToRemove.length; ++i)
  60. RemoveCallback(m_CallbacksToRemove[i]);
  61. for (var i = 0; i < m_CallbacksToAdd.length; ++i)
  62. AddCallback(m_CallbacksToAdd[i]);
  63. m_CallbacksToAdd.Clear();
  64. m_CallbacksToRemove.Clear();
  65. }
  66. }
  67. }