LuaBase.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace LuaInterface
  5. {
  6. /// <summary>
  7. /// Base class to provide consistent disposal flow across lua objects. Uses code provided by Yves Duhoux and suggestions by Hans Schmeidenbacher and Qingrui Li
  8. /// </summary>
  9. public abstract class LuaBase : IDisposable
  10. {
  11. private bool _Disposed;
  12. protected int _Reference;
  13. protected LuaState _Interpreter;
  14. ~LuaBase()
  15. {
  16. Dispose(false);
  17. }
  18. public void Dispose()
  19. {
  20. Dispose(true);
  21. GC.SuppressFinalize(this);
  22. }
  23. public virtual void Dispose(bool disposeManagedResources)
  24. {
  25. if (!_Disposed)
  26. {
  27. if (disposeManagedResources)
  28. {
  29. if (_Reference != 0)
  30. _Interpreter.dispose(_Reference);
  31. }
  32. _Interpreter = null;
  33. _Disposed = true;
  34. }
  35. }
  36. public override bool Equals(object o)
  37. {
  38. if (o is LuaBase)
  39. {
  40. LuaBase l = (LuaBase)o;
  41. return _Interpreter.compareRef(l._Reference, _Reference);
  42. }
  43. else return false;
  44. }
  45. public override int GetHashCode()
  46. {
  47. return _Reference;
  48. }
  49. }
  50. }