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

18
src/ImageSharp/Image{TPixel}.cs

@ -23,8 +23,6 @@ namespace SixLabors.ImageSharp
public sealed class Image<TPixel> : Image public sealed class Image<TPixel> : Image
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
private bool isDisposed;
private ImageFrameCollection<TPixel> frames; private ImageFrameCollection<TPixel> frames;
/// <summary> /// <summary>
@ -272,26 +270,10 @@ namespace SixLabors.ImageSharp
/// <inheritdoc/> /// <inheritdoc/>
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
{ {
if (this.isDisposed)
{
return;
}
if (disposing) if (disposing)
{ {
this.frames.Dispose(); 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/> /// <inheritdoc/>

Loading…
Cancel
Save