using System; namespace SharpDX { // From SharpDX.Toolkit /// /// A disposable component base class. /// public abstract class Component : ComponentBase, IDisposable { /// /// Gets or sets the disposables. /// /// The disposables. protected DisposeCollector DisposeCollector { get; set; } /// /// Initializes a new instance of the class. /// protected internal Component() { } /// /// Initializes a new instance of the class with an immutable name. /// /// The name. protected Component(string name) : base(name) { } /// /// Gets or sets a value indicating whether this instance is attached to a collector. /// /// /// true if this instance is attached to a collector; otherwise, false. /// internal bool IsAttached { get; set; } /// /// Gets a value indicating whether this instance is disposed. /// /// /// true if this instance is disposed; otherwise, false. /// protected internal bool IsDisposed { get; private set; } protected internal bool IsDisposing { get; private set; } /// /// Occurs when when Dispose is called. /// public event EventHandler Disposing; /// /// Releases unmanaged and - optionally - managed resources /// public void Dispose() { if (!IsDisposed) { IsDisposing = true; // Call the disposing event. var handler = Disposing; if (handler != null) { handler(this, EventArgs.Empty); } Dispose(true); IsDisposed = true; } } /// /// Disposes of object resources. /// /// If true, managed resources should be /// disposed of in addition to unmanaged resources. protected virtual void Dispose(bool disposeManagedResources) { if (disposeManagedResources) { // Dispose all ComObjects if (DisposeCollector != null) DisposeCollector.Dispose(); DisposeCollector = null; } } /// /// Adds a disposable object to the list of the objects to dispose. /// /// To dispose. protected internal T ToDispose(T toDisposeArg) { if (!ReferenceEquals(toDisposeArg, null)) { if (DisposeCollector == null) DisposeCollector = new DisposeCollector(); return DisposeCollector.Collect(toDisposeArg); } return default(T); } /// /// Dispose a disposable object and set the reference to null. Removes this object from the ToDispose list. /// /// Object to dispose. protected internal void RemoveAndDispose(ref T objectToDispose) { if (!ReferenceEquals(objectToDispose, null) && DisposeCollector != null) DisposeCollector.RemoveAndDispose(ref objectToDispose); } /// /// Removes a disposable object to the list of the objects to dispose. /// /// /// To dispose. protected internal void RemoveToDispose(T toDisposeArg) { if (!ReferenceEquals(toDisposeArg, null) && DisposeCollector != null) DisposeCollector.Remove(toDisposeArg); } } }