using System;
namespace OpenMacroBoard.SDK
{
///
/// A conditional disposable wrapper.
///
///
/// This class is used in situations where the wrapped element is either borrowed (in which case
/// it shouldn't be disposed) or owned (in which case it should be disposed) and abstracts that
/// away from the consumer. The consumer has to make sure to call dispose once they are finished
/// and this wrapped decides whether the wrapped element is in fact disposed or not.
///
/// Disposable type.
public sealed class ConditionalDisposable : IDisposable
where T : IDisposable
{
///
/// Initializes a new instance of the class.
///
/// The wrapped item.
/// A flag that determines if the item will be disposed.
public ConditionalDisposable(T item, bool disposeItem)
{
Item = item;
DisposeItem = disposeItem;
}
///
/// Gets the underlying wrapped item.
///
public T Item { get; }
///
/// Get a value that determines whether the item will be disposed or not.
///
public bool DisposeItem { get; }
///
public void Dispose()
{
if (!DisposeItem)
{
return;
}
Item?.Dispose();
}
}
}