Skip to content

Commit

Permalink
Added early exits when the exception is null and inlined the check if…
Browse files Browse the repository at this point in the history
… the exception is an error.
  • Loading branch information
dlemstra committed Oct 15, 2018
1 parent dc004cf commit 4178b19
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 70 deletions.
6 changes: 4 additions & 2 deletions Source/Magick.NET/Native/Drawables/DrawingWand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -815,8 +815,10 @@ public IntPtr FontTypeMetrics(string text, bool ignoreNewLines)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.DrawingWand_FontTypeMetrics(Instance, textNative.Instance, ignoreNewLines, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
ImageMagick.TypeMetric.Dispose(result);
Expand Down
6 changes: 4 additions & 2 deletions Source/Magick.NET/Native/MagickFormatInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,10 @@ public IntPtr CreateList(out UIntPtr length)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickFormatInfo_CreateList(out length, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
DisposeList(result, (int)length);
Expand Down
30 changes: 20 additions & 10 deletions Source/Magick.NET/Native/MagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4012,8 +4012,10 @@ public IntPtr FontTypeMetrics(DrawingSettings settings, bool ignoreNewlines)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImage_FontTypeMetrics(Instance, settingsNative.Instance, ignoreNewlines, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
ImageMagick.TypeMetric.Dispose(result);
Expand Down Expand Up @@ -4403,8 +4405,10 @@ public IntPtr Histogram(out UIntPtr length)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImage_Histogram(Instance, out length, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
ImageMagick.MagickColorCollection.DisposeList(result);
Expand Down Expand Up @@ -4676,8 +4680,10 @@ public IntPtr Moments()
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImage_Moments(Instance, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
ImageMagick.Moments.DisposeList(result);
Expand Down Expand Up @@ -4876,8 +4882,10 @@ public IntPtr PerceptualHash()
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImage_PerceptualHash(Instance, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
ImageMagick.PerceptualHash.DisposeList(result);
Expand Down Expand Up @@ -5827,8 +5835,10 @@ public IntPtr Statistics()
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImage_Statistics(Instance, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
ImageMagick.Statistics.DisposeList(result);
Expand Down
102 changes: 68 additions & 34 deletions Source/Magick.NET/Native/MagickImageCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,10 @@ public IntPtr Append(IMagickImage image, bool stack)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImageCollection_Append(image.GetInstance(), stack, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
Dispose(result);
Expand All @@ -193,8 +195,10 @@ public IntPtr Coalesce(IMagickImage image)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImageCollection_Coalesce(image.GetInstance(), out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
Dispose(result);
Expand All @@ -219,8 +223,10 @@ public IntPtr Combine(IMagickImage image, ColorSpace colorSpace)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImageCollection_Combine(image.GetInstance(), (UIntPtr)colorSpace, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
Dispose(result);
Expand All @@ -245,8 +251,10 @@ public IntPtr Deconstruct(IMagickImage image)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImageCollection_Deconstruct(image.GetInstance(), out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
Dispose(result);
Expand Down Expand Up @@ -286,8 +294,10 @@ public IntPtr Evaluate(IMagickImage image, EvaluateOperator evaluateOperator)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImageCollection_Evaluate(image.GetInstance(), (UIntPtr)evaluateOperator, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
Dispose(result);
Expand All @@ -312,8 +322,10 @@ public IntPtr Flatten(IMagickImage image)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImageCollection_Flatten(image.GetInstance(), out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
Dispose(result);
Expand Down Expand Up @@ -358,8 +370,10 @@ public IntPtr Merge(IMagickImage image, LayerMethod method)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImageCollection_Merge(image.GetInstance(), (UIntPtr)method, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
Dispose(result);
Expand All @@ -386,8 +400,10 @@ public IntPtr Montage(IMagickImage image, MontageSettings settings)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImageCollection_Montage(image.GetInstance(), settingsNative.Instance, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
Dispose(result);
Expand All @@ -413,8 +429,10 @@ public IntPtr Morph(IMagickImage image, int frames)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImageCollection_Morph(image.GetInstance(), (UIntPtr)frames, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
Dispose(result);
Expand All @@ -439,8 +457,10 @@ public IntPtr Optimize(IMagickImage image)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImageCollection_Optimize(image.GetInstance(), out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
Dispose(result);
Expand All @@ -465,8 +485,10 @@ public IntPtr OptimizePlus(IMagickImage image)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImageCollection_OptimizePlus(image.GetInstance(), out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
Dispose(result);
Expand Down Expand Up @@ -508,8 +530,10 @@ public IntPtr Polynomial(IMagickImage image, double[] terms, int length)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImageCollection_Polynomial(image.GetInstance(), terms, (UIntPtr)length, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
Dispose(result);
Expand Down Expand Up @@ -556,8 +580,10 @@ public IntPtr ReadBlob(MagickSettings settings, byte[] data, int length)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImageCollection_ReadBlob(settingsNative.Instance, data, (UIntPtr)length, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
Dispose(result);
Expand Down Expand Up @@ -585,8 +611,10 @@ public IntPtr ReadFile(MagickSettings settings)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImageCollection_ReadFile(settingsNative.Instance, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
Dispose(result);
Expand Down Expand Up @@ -614,8 +642,10 @@ public IntPtr ReadStream(MagickSettings settings, ReadWriteStreamDelegate reader
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImageCollection_ReadStream(settingsNative.Instance, reader, seeker, teller, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
Dispose(result);
Expand All @@ -641,8 +671,10 @@ public IntPtr Smush(IMagickImage image, int offset, bool stack)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImageCollection_Smush(image.GetInstance(), (IntPtr)offset, stack, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
Dispose(result);
Expand Down Expand Up @@ -689,8 +721,10 @@ public IntPtr WriteStream(IMagickImage image, MagickSettings settings, ReadWrite
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickImageCollection_WriteStream(image.GetInstance(), settingsNative.Instance, writer, seeker, teller, reader, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
Dispose(result);
Expand Down
6 changes: 4 additions & 2 deletions Source/Magick.NET/Native/MagickNET.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@ public static IntPtr GetFontFamilies(out UIntPtr length)
#if PLATFORM_x86 || PLATFORM_AnyCPU
result = NativeMethods.X86.MagickNET_GetFontFamilies(out length, out exception);
#endif
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return result;
if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
DisposeFontFamilies(result);
Expand Down
7 changes: 5 additions & 2 deletions Source/Magick.NET/Native/NativeInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ public void Dispose()

protected void CheckException(IntPtr exception, IntPtr result)
{
MagickException magickException = MagickExceptionHelper.Create(exception);
if (MagickExceptionHelper.IsError(magickException))
var magickException = MagickExceptionHelper.Create(exception);
if (magickException == null)
return;

if (magickException is MagickErrorException)
{
if (result != IntPtr.Zero)
Dispose(result);
Expand Down
Loading

0 comments on commit 4178b19

Please sign in to comment.