// License: Apache 2.0. See LICENSE file in root directory. // Copyright(c) 2017 Intel Corporation. All Rights Reserved. namespace Intel.RealSense.Base { using System; using System.Diagnostics; /// /// Base class for disposable objects with native resources /// public abstract class Object : IDisposable { // TODO: rename, kept for backwards compatiblity [DebuggerBrowsable(DebuggerBrowsableState.Never)] internal readonly DeleterHandle m_instance; /// /// Initializes a new instance of the class. /// /// native pointer /// optional deleter /// Thrown when is null protected Object(IntPtr ptr, Deleter deleter) { if (ptr == IntPtr.Zero) { throw new ArgumentNullException(nameof(ptr)); } m_instance = new DeleterHandle(ptr, deleter); } /// /// Gets the native handle /// /// Thrown when public IntPtr Handle { get { if (m_instance.IsInvalid) { throw new ObjectDisposedException(GetType().Name); } return m_instance.Handle; } } /// public void Dispose() { this.Dispose(true); } protected virtual void Dispose(bool disposing) { m_instance.Dispose(); } internal void Reset(IntPtr ptr, Deleter deleter) { m_instance.Reset(ptr, deleter); } } }