You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

DisposableObject.cs 3.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Diagnostics.CodeAnalysis;
  16. using System.Runtime.CompilerServices;
  17. using System.Text;
  18. namespace Tensorflow
  19. {
  20. /// <summary>
  21. /// Abstract class for disposable object allocated in unmanaged runtime.
  22. /// </summary>
  23. public abstract class DisposableObject : IDisposable
  24. {
  25. protected IntPtr _handle;
  26. protected bool _disposed;
  27. [SuppressMessage("ReSharper", "UnusedMember.Global")]
  28. protected DisposableObject()
  29. { }
  30. protected DisposableObject(IntPtr handle)
  31. => _handle = handle;
  32. [SuppressMessage("ReSharper", "InvertIf")]
  33. private void internal_dispose(bool disposing)
  34. {
  35. if (_disposed)
  36. return;
  37. _disposed = true;
  38. //first handle managed, they might use the unmanaged resources.
  39. if (disposing)
  40. // dispose managed state (managed objects).
  41. DisposeManagedResources();
  42. //free unmanaged memory
  43. if (_handle != IntPtr.Zero)
  44. {
  45. DisposeUnmanagedResources(_handle);
  46. _handle = IntPtr.Zero;
  47. }
  48. }
  49. /// <summary>
  50. /// Dispose any managed resources.
  51. /// </summary>
  52. /// <remarks>Equivalent to what you would perform inside <see cref="Dispose()"/></remarks>
  53. protected virtual void DisposeManagedResources()
  54. { }
  55. /// <summary>
  56. /// Dispose any unmanaged resources related to given <paramref name="handle"/>.
  57. /// </summary>
  58. protected abstract void DisposeUnmanagedResources(IntPtr handle);
  59. ~DisposableObject()
  60. {
  61. internal_dispose(false);
  62. }
  63. public void Dispose()
  64. {
  65. internal_dispose(true);
  66. GC.SuppressFinalize(this);
  67. }
  68. /// <summary>
  69. /// If <see cref="_handle"/> is <see cref="IntPtr.Zero"/> then throws <see cref="ObjectDisposedException"/>
  70. /// </summary>
  71. /// <exception cref="ObjectDisposedException">When <see cref="_handle"/> is <see cref="IntPtr.Zero"/></exception>
  72. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  73. protected void EnsureNotDisposed()
  74. {
  75. if (_disposed)
  76. throw new ObjectDisposedException($"Unable to access disposed object, Type: {GetType().Name}");
  77. }
  78. }
  79. }