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.

45 lines
1.4KB

  1. using System.Runtime.CompilerServices;
  2. using System.Threading;
  3. using Svelto.Common;
  4. using Svelto.ECS.Internal;
  5. namespace Svelto.ECS
  6. {
  7. static class BurstCompatibleCounter
  8. {
  9. public static int counter;
  10. }
  11. public class ComponentTypeID<T> where T : struct, _IInternalEntityComponent
  12. {
  13. static readonly SharedStaticWrapper<ComponentID, ComponentTypeID<T>> _id;
  14. public static ComponentID id
  15. {
  16. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  17. get => _id.Data;
  18. }
  19. /// <summary>
  20. /// c# Static constructors are guaranteed to be thread safe
  21. /// The runtime guarantees that a static constructor is only called once. So even if a type is called by multiple threads at the same time,
  22. /// the static constructor is always executed one time. To get a better understanding how this works, it helps to know what purpose it serves.
  23. /// </summary>
  24. static ComponentTypeID()
  25. {
  26. Init();
  27. }
  28. #if UNITY_BURST
  29. [Unity.Burst.BurstDiscard]
  30. //SharedStatic values must be initialized from not burstified code
  31. #endif
  32. internal static void Init()
  33. {
  34. if (_id.Data != 0)
  35. return;
  36. _id.Data = Interlocked.Increment(ref BurstCompatibleCounter.counter);
  37. ComponentTypeMap.Add(typeof(T), id);
  38. }
  39. }
  40. }