Local.cs 1.9 KB

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