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.

29 lines
1.0KB

  1. using System.Runtime.InteropServices;
  2. namespace Svelto.ECS.Hybrid
  3. {
  4. /// <summary>
  5. /// ValueReference is the only way to store a reference inside an Implementor. To stop any abuse
  6. /// the reference must be an implementor and converted back to an implementor.
  7. /// The OOP abstraction layer that knows about the implementor than can cast it to the real type
  8. /// </summary>
  9. /// <typeparam name="T"></typeparam>
  10. public struct ValueReference<T> : IValueReferenceInternal where T:class
  11. {
  12. public ValueReference(T obj) { _pointer = GCHandle.Alloc(obj, GCHandleType.Normal); }
  13. public T ConvertAndDispose<W>(W implementer) where W:IImplementor
  14. {
  15. var pointerTarget = _pointer.Target;
  16. _pointer.Free();
  17. return (T)pointerTarget;
  18. }
  19. public bool isDefault => _pointer.IsAllocated == false;
  20. GCHandle _pointer;
  21. }
  22. // Used to validate the use of this struct on the component builder check fields.
  23. internal interface IValueReferenceInternal {}
  24. }