Mirror of Svelto.ECS because we're a fan of it
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.

91 lines
2.2KB

  1. using System;
  2. using Svelto.Common;
  3. using Svelto.DataStructures;
  4. namespace Svelto.ECS
  5. {
  6. public struct Consumer<T> : IDisposable where T : unmanaged, IEntityComponent
  7. {
  8. internal Consumer(string name, uint capacity) : this()
  9. {
  10. unsafe
  11. {
  12. #if DEBUG && !PROFILE_SVELTO
  13. _name = name;
  14. #endif
  15. _ringBuffer = new RingBuffer<ValueTuple<T, EGID>>((int) capacity,
  16. #if DEBUG && !PROFILE_SVELTO
  17. _name
  18. #else
  19. string.Empty
  20. #endif
  21. );
  22. mustBeDisposed = MemoryUtilities.Alloc(sizeof(bool), Allocator.Persistent);
  23. *(bool*) mustBeDisposed = false;
  24. }
  25. }
  26. internal Consumer(ExclusiveGroupStruct group, string name, uint capacity) : this(name, capacity)
  27. {
  28. this.group = group;
  29. hasGroup = true;
  30. }
  31. internal void Enqueue(in T entity, in EGID egid)
  32. {
  33. _ringBuffer.Enqueue((entity, egid));
  34. }
  35. public bool TryDequeue(out T entity)
  36. {
  37. var tryDequeue = _ringBuffer.TryDequeue(out var values);
  38. entity = values.Item1;
  39. return tryDequeue;
  40. }
  41. public bool TryDequeue(out T entity, out EGID id)
  42. {
  43. var tryDequeue = _ringBuffer.TryDequeue(out var values);
  44. entity = values.Item1;
  45. id = values.Item2;
  46. return tryDequeue;
  47. }
  48. public void Flush()
  49. {
  50. _ringBuffer.Reset();
  51. }
  52. public void Dispose()
  53. {
  54. unsafe
  55. {
  56. *(bool*) mustBeDisposed = true;
  57. }
  58. }
  59. public uint Count()
  60. {
  61. return (uint) _ringBuffer.Count;
  62. }
  63. public void Free()
  64. {
  65. MemoryUtilities.Free(mustBeDisposed, Allocator.Persistent);
  66. }
  67. readonly RingBuffer<ValueTuple<T, EGID>> _ringBuffer;
  68. internal readonly ExclusiveGroupStruct group;
  69. internal readonly bool hasGroup;
  70. internal IntPtr mustBeDisposed;
  71. #if DEBUG && !PROFILE_SVELTO
  72. readonly string _name;
  73. #endif
  74. }
  75. }