Browse Source

Made the Bootstrapper fully static.

pull/52/head
Dirk Lemstra 10 years ago
parent
commit
c3941bfb8c
  1. 57
      src/ImageSharp/Bootstrapper.cs

57
src/ImageSharp/Bootstrapper.cs

@ -16,63 +16,48 @@ namespace ImageSharp
/// <summary> /// <summary>
/// Provides initialization code which allows extending the library. /// Provides initialization code which allows extending the library.
/// </summary> /// </summary>
public class Bootstrapper public static class Bootstrapper
{ {
/// <summary>
/// A singleton of the <see cref="Bootstrapper"/> class.
/// </summary>
private static readonly Bootstrapper Instance = new Bootstrapper();
/// <summary> /// <summary>
/// The list of supported <see cref="IImageFormat"/>. /// The list of supported <see cref="IImageFormat"/>.
/// </summary> /// </summary>
private readonly List<IImageFormat> imageFormats; private static readonly List<IImageFormat> ImageFormatsList;
/// <summary>
/// The parallel options for processing tasks in parallel.
/// </summary>
private readonly ParallelOptions parallelOptions;
/// <summary> /// <summary>
/// An object that can be used to synchronize access to the <see cref="Bootstrapper"/>. /// An object that can be used to synchronize access to the <see cref="Bootstrapper"/>.
/// </summary> /// </summary>
private readonly object syncRoot = new object(); private static readonly object SyncRoot = new object();
/// <summary>
/// The maximum header size of all formats.
/// </summary>
private int maxHeaderSize;
/// <summary> /// <summary>
/// Prevents a default instance of the <see cref="Bootstrapper"/> class from being created. /// Initializes static members of the <see cref="Bootstrapper"/> class.
/// </summary> /// </summary>
private Bootstrapper() static Bootstrapper()
{ {
this.imageFormats = new List<IImageFormat> ImageFormatsList = new List<IImageFormat>
{ {
new BmpFormat(), new BmpFormat(),
new JpegFormat(), new JpegFormat(),
new PngFormat(), new PngFormat(),
new GifFormat() new GifFormat()
}; };
this.SetMaxHeaderSize(); SetMaxHeaderSize();
this.parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }; ParallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount };
} }
/// <summary> /// <summary>
/// Gets the collection of supported <see cref="IImageFormat"/> /// Gets the collection of supported <see cref="IImageFormat"/>
/// </summary> /// </summary>
public static IReadOnlyCollection<IImageFormat> ImageFormats => new ReadOnlyCollection<IImageFormat>(Instance.imageFormats); public static IReadOnlyCollection<IImageFormat> ImageFormats => new ReadOnlyCollection<IImageFormat>(ImageFormatsList);
/// <summary> /// <summary>
/// Gets the global parallel options for processing tasks in parallel. /// Gets the global parallel options for processing tasks in parallel.
/// </summary> /// </summary>
public static ParallelOptions ParallelOptions => Instance.parallelOptions; public static ParallelOptions ParallelOptions { get; }
/// <summary> /// <summary>
/// Gets the maximum header size of all formats. /// Gets the maximum header size of all formats.
/// </summary> /// </summary>
internal static int MaxHeaderSize => Instance.maxHeaderSize; internal static int MaxHeaderSize { get; private set; }
/// <summary> /// <summary>
/// Adds a new <see cref="IImageFormat"/> to the collection of supported image formats. /// Adds a new <see cref="IImageFormat"/> to the collection of supported image formats.
@ -87,22 +72,22 @@ namespace ImageSharp
Guard.NotNullOrEmpty(format.Extension, nameof(format), "The extension should not be null or empty."); Guard.NotNullOrEmpty(format.Extension, nameof(format), "The extension should not be null or empty.");
Guard.NotNullOrEmpty(format.SupportedExtensions, nameof(format), "The supported extensions not be null or empty."); Guard.NotNullOrEmpty(format.SupportedExtensions, nameof(format), "The supported extensions not be null or empty.");
Instance.AddImageFormatLocked(format); AddImageFormatLocked(format);
} }
private void AddImageFormatLocked(IImageFormat format) private static void AddImageFormatLocked(IImageFormat format)
{ {
lock (this.syncRoot) lock (SyncRoot)
{ {
this.GuardDuplicate(format); GuardDuplicate(format);
this.imageFormats.Add(format); ImageFormatsList.Add(format);
this.SetMaxHeaderSize(); SetMaxHeaderSize();
} }
} }
private void GuardDuplicate(IImageFormat format) private static void GuardDuplicate(IImageFormat format)
{ {
if (!format.SupportedExtensions.Contains(format.Extension, StringComparer.OrdinalIgnoreCase)) if (!format.SupportedExtensions.Contains(format.Extension, StringComparer.OrdinalIgnoreCase))
{ {
@ -114,7 +99,7 @@ namespace ImageSharp
throw new ArgumentException("The supported extensions should not contain empty values.", nameof(format)); throw new ArgumentException("The supported extensions should not contain empty values.", nameof(format));
} }
foreach (var imageFormat in this.imageFormats) foreach (var imageFormat in ImageFormatsList)
{ {
if (imageFormat.Extension.Equals(format.Extension, StringComparison.OrdinalIgnoreCase)) if (imageFormat.Extension.Equals(format.Extension, StringComparison.OrdinalIgnoreCase))
{ {
@ -128,9 +113,9 @@ namespace ImageSharp
} }
} }
private void SetMaxHeaderSize() private static void SetMaxHeaderSize()
{ {
this.maxHeaderSize = imageFormats.Max(x => x.HeaderSize); MaxHeaderSize = ImageFormatsList.Max(x => x.HeaderSize);
} }
} }
} }

Loading…
Cancel
Save