Browse Source

Moved dispose control logic to base Image class

internal call EnsureNotDisposed is no longer virtual -> micro speedup gain in pixel index accessor Image<TPixel>[x, y].
pull/1629/head
Dmitry Pentin 5 years ago
parent
commit
acf9d85e8c
  1. 22
      src/ImageSharp/Image.cs
  2. 18
      src/ImageSharp/Image{TPixel}.cs

22
src/ImageSharp/Image.cs

@ -19,6 +19,8 @@ namespace SixLabors.ImageSharp
/// </summary>
public abstract partial class Image : IImage, IConfigurationProvider
{
private bool isDisposed;
private Size size;
private readonly Configuration configuration;
@ -78,7 +80,17 @@ namespace SixLabors.ImageSharp
Configuration IConfigurationProvider.Configuration => this.configuration;
/// <inheritdoc />
public void Dispose() => this.Dispose(true);
public void Dispose()
{
if (this.isDisposed)
{
return;
}
this.Dispose(true);
this.isDisposed = true;
}
/// <summary>
/// Saves the image to the given stream using the given image encoder.
@ -144,7 +156,13 @@ namespace SixLabors.ImageSharp
/// <summary>
/// Throws <see cref="ObjectDisposedException"/> if the image is disposed.
/// </summary>
internal abstract void EnsureNotDisposed();
internal void EnsureNotDisposed()
{
if (this.isDisposed)
{
throw new ObjectDisposedException("Trying to execute an operation on a disposed image.");
}
}
/// <summary>
/// Accepts a <see cref="IImageVisitor"/>.

18
src/ImageSharp/Image{TPixel}.cs

@ -23,8 +23,6 @@ namespace SixLabors.ImageSharp
public sealed class Image<TPixel> : Image
where TPixel : unmanaged, IPixel<TPixel>
{
private bool isDisposed;
private ImageFrameCollection<TPixel> frames;
/// <summary>
@ -272,26 +270,10 @@ namespace SixLabors.ImageSharp
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (this.isDisposed)
{
return;
}
if (disposing)
{
this.frames.Dispose();
}
this.isDisposed = true;
}
/// <inheritdoc/>
internal override void EnsureNotDisposed()
{
if (this.isDisposed)
{
throw new ObjectDisposedException("Trying to execute an operation on a disposed image.");
}
}
/// <inheritdoc/>

Loading…
Cancel
Save