Local.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #if FEAT_COMPILER
  2. using System;
  3. using System.Reflection.Emit;
  4. namespace ProtoBuf.Compiler
  5. {
  6. internal sealed class Local : IDisposable
  7. {
  8. // public static readonly Local InputValue = new Local(null, null);
  9. private LocalBuilder value;
  10. private readonly Type type;
  11. private CompilerContext ctx;
  12. private Local(LocalBuilder value, Type type)
  13. {
  14. this.value = value;
  15. this.type = type;
  16. }
  17. internal Local(CompilerContext ctx, Type type)
  18. {
  19. this.ctx = ctx;
  20. if (ctx != null) { value = ctx.GetFromPool(type); }
  21. this.type = type;
  22. }
  23. internal LocalBuilder Value => value ?? throw new ObjectDisposedException(GetType().Name);
  24. public Type Type => type;
  25. public Local AsCopy()
  26. {
  27. if (ctx == null) return this; // can re-use if context-free
  28. return new Local(value, this.type);
  29. }
  30. public void Dispose()
  31. {
  32. if (ctx != null)
  33. {
  34. // only *actually* dispose if this is context-bound; note that non-bound
  35. // objects are cheekily re-used, and *must* be left intact agter a "using" etc
  36. ctx.ReleaseToPool(value);
  37. value = null;
  38. ctx = null;
  39. }
  40. }
  41. internal bool IsSame(Local other)
  42. {
  43. if((object)this == (object)other) return true;
  44. object ourVal = value; // use prop to ensure obj-disposed etc
  45. return other != null && ourVal == (object)(other.value);
  46. }
  47. }
  48. }
  49. #endif