Skip to content

Fix code issues by ReSharper #1728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions OpenCvSharp.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:Boolean x:Key="/Default/GrammarAndSpelling/GrammarChecking/Exceptions/=multi_002Dchannel/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/GrammarAndSpelling/GrammarChecking/Exceptions/=multi_002Ddimensional/@EntryIndexedValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=15b5b1f1_002D457c_002D4ca6_002Db278_002D5615aedc07d3/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static readonly fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="READONLY_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=236f7aa5_002D7b06_002D43ca_002Dbf2a_002D9b31bfcff09a/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Any" AccessRightKinds="Private" Description="Constant fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="CONSTANT_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=4a98fdf6_002D7d98_002D4f5a_002Dafeb_002Dea44ad98c70c/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Instance" AccessRightKinds="Private" Description="Instance fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="FIELD" /&gt;&lt;Kind Name="READONLY_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String>
Expand Down Expand Up @@ -110,6 +112,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=ONNX/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=opencv/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Otsu/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=prms/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=PSNR/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Ptrs/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=RANSAC/@EntryIndexedValue">True</s:Boolean>
Expand Down Expand Up @@ -148,6 +151,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Vecs/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=videoio/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Voronoi/@EntryIndexedValue">True</s:Boolean>

<s:Boolean x:Key="/Default/UserDictionary/Words/=Writeable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=ximgproc/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=xphoto/@EntryIndexedValue">True</s:Boolean>
Expand Down
8 changes: 3 additions & 5 deletions src/OpenCvSharp/Cv2/Cv2_imgcodecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public static bool ImWrite(string fileName, Mat img, int[]? prms = null)
throw new ArgumentNullException(nameof(fileName));
if (img is null)
throw new ArgumentNullException(nameof(img));
if (prms is null)
prms = Array.Empty<int>();
prms ??= [];

NativeMethods.HandleException(
NativeMethods.imgcodecs_imwrite(fileName, img.CvPtr, prms, prms.Length, out var ret));
Expand Down Expand Up @@ -100,7 +99,7 @@ public static bool ImWrite(string fileName, IEnumerable<Mat> img, int[]? prms =
throw new ArgumentNullException(nameof(fileName));
if (img is null)
throw new ArgumentNullException(nameof(img));
prms ??= Array.Empty<int>();
prms ??= [];

using var imgVec = new VectorOfMat(img);
NativeMethods.HandleException(
Expand Down Expand Up @@ -218,8 +217,7 @@ public static bool ImEncode(string ext, InputArray img, out byte[] buf, int[]? p
throw new ArgumentNullException(nameof(ext));
if (img is null)
throw new ArgumentNullException(nameof(img));
if (prms is null)
prms = Array.Empty<int>();
prms ??= [];
img.ThrowIfDisposed();

using var bufVec = new VectorOfByte();
Expand Down
5 changes: 1 addition & 4 deletions src/OpenCvSharp/Fundamentals/CvObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,5 @@ protected CvObject(IntPtr ptr)
/// <summary>
/// Native pointer of OpenCV structure
/// </summary>
public IntPtr CvPtr
{
get { return ptr; }
}
public IntPtr CvPtr => ptr;
}
134 changes: 66 additions & 68 deletions src/OpenCvSharp/Internal/PInvoke/ExceptionHandler.cs
Original file line number Diff line number Diff line change
@@ -1,81 +1,79 @@
#if DOTNETCORE
using System;
using System.Threading;

namespace OpenCvSharp.Internal
namespace OpenCvSharp.Internal;

/// <summary>
/// This static class defines one instance which than can be used by multiple threads to gather exception information from OpenCV
/// Implemented as a singleton
/// </summary>
public static class ExceptionHandler
{
// ThreadLocal variables to save the exception for the current thread
private static readonly ThreadLocal<bool> exceptionHappened = new(false);
private static readonly ThreadLocal<ErrorCode> localStatus = new();
private static readonly ThreadLocal<string> localFuncName = new();
private static readonly ThreadLocal<string> localErrMsg = new();
private static readonly ThreadLocal<string> localFileName = new();
private static readonly ThreadLocal<int> localLine = new();

/// <summary>
/// This static class defines one instance which than can be used by multiple threads to gather exception information from OpenCV
/// Implemented as a singleton
/// Callback function invoked by OpenCV when exception occurs
/// Stores the information locally for every thread
/// </summary>
public static class ExceptionHandler
{
// ThreadLocal variables to save the exception for the current thread
private static readonly ThreadLocal<bool> exceptionHappened = new ThreadLocal<bool>(false);
private static readonly ThreadLocal<ErrorCode> localStatus = new ThreadLocal<ErrorCode>();
private static readonly ThreadLocal<string> localFuncName = new ThreadLocal<string>();
private static readonly ThreadLocal<string> localErrMsg = new ThreadLocal<string>();
private static readonly ThreadLocal<string> localFileName = new ThreadLocal<string>();
private static readonly ThreadLocal<int> localLine = new ThreadLocal<int>();

/// <summary>
/// Callback function invoked by OpenCV when exception occurs
/// Stores the information locally for every thread
/// </summary>
public static readonly CvErrorCallback ErrorHandlerCallback =
delegate (ErrorCode status, string funcName, string errMsg, string fileName, int line, IntPtr userData)
{
try
{
return 0;
}
finally
{
exceptionHappened.Value = true;
localStatus.Value = status;
localErrMsg.Value = errMsg;
localFileName.Value = fileName;
localLine.Value = line;
localFuncName.Value = funcName;
}
};

/// <summary>
/// Registers the callback function to OpenCV, so exception caught before the p/invoke boundary
/// </summary>
public static void RegisterExceptionCallback()
public static readonly CvErrorCallback ErrorHandlerCallback =
delegate (ErrorCode status, string funcName, string errMsg, string fileName, int line, IntPtr userData)
{
IntPtr zero = IntPtr.Zero;
IntPtr ret = NativeMethods.redirectError(ErrorHandlerCallback, zero, ref zero);
}

/// <summary>
/// Throws appropriate exception if one happened
/// </summary>
public static void ThrowPossibleException()
{
if (CheckForException())
try
{
throw new OpenCVException(
localStatus.Value,
localFuncName.Value ?? "",
localErrMsg.Value ?? "",
localFileName.Value ?? "",
localLine.Value);
return 0;
}
}
finally
{
exceptionHappened.Value = true;
localStatus.Value = status;
localErrMsg.Value = errMsg;
localFileName.Value = fileName;
localLine.Value = line;
localFuncName.Value = funcName;
}
};

/// <summary>
/// Returns a boolean which indicates if an exception occured for the current thread
/// Reading this value changes its state, so an exception is handled only once
/// </summary>
private static bool CheckForException()
/// <summary>
/// Registers the callback function to OpenCV, so exception caught before the p/invoke boundary
/// </summary>
public static void RegisterExceptionCallback()
{
IntPtr zero = IntPtr.Zero;
IntPtr ret = NativeMethods.redirectError(ErrorHandlerCallback, zero, ref zero);
}

/// <summary>
/// Throws appropriate exception if one happened
/// </summary>
public static void ThrowPossibleException()
{
if (CheckForException())
{
var value = exceptionHappened.Value;
// reset exception value
exceptionHappened.Value = false;
return value;
throw new OpenCVException(
localStatus.Value,
localFuncName.Value ?? "",
localErrMsg.Value ?? "",
localFileName.Value ?? "",
localLine.Value);
}
}

/// <summary>
/// Returns a boolean which indicates if an exception occured for the current thread
/// Reading this value changes its state, so an exception is handled only once
/// </summary>
private static bool CheckForException()
{
var value = exceptionHappened.Value;
// reset exception value
exceptionHappened.Value = false;
return value;
}
}
#endif

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static void LoadLibraries(IEnumerable<string>? additionalPaths = null)
return;
}

var ap = (additionalPaths is null) ? Array.Empty<string>() : additionalPaths.ToArray();
var ap = (additionalPaths is null) ? [] : additionalPaths.ToArray();

/*
if (Environment.Is64BitProcess)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static partial class NativeMethods
public static extern ExceptionStatus core_getBuildInformation(IntPtr buf);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true)]
public static unsafe extern ExceptionStatus core_getVersionString(byte* buf, int maxLength);
public static extern unsafe ExceptionStatus core_getVersionString(byte* buf, int maxLength);
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus core_getVersionMajor(out int returnValue);
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ namespace OpenCvSharp.Internal;

static partial class NativeMethods
{
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void imgproc_LineSegmentDetector_detect_OutputArray(IntPtr obj, IntPtr image, IntPtr lines,
IntPtr width, IntPtr prec, IntPtr nfa);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void imgproc_LineSegmentDetector_detect_vector(IntPtr obj, IntPtr image, IntPtr lines,
IntPtr width, IntPtr prec, IntPtr nfa);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void imgproc_LineSegmentDetector_drawSegments(IntPtr obj, IntPtr image, IntPtr lines);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
Expand All @@ -30,7 +30,7 @@ public static extern IntPtr imgproc_createLineSegmentDetector(
int refine, double scale, double sigma_scale, double quant, double ang_th,
double log_eps, double density_th, int n_bins);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void imgproc_Ptr_LineSegmentDetector_delete(IntPtr obj);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
Expand Down
14 changes: 7 additions & 7 deletions src/OpenCvSharp/Internal/PInvoke/WindowsLibraryLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public sealed class WindowsLibraryLoader
private const string DllFileExtension = ".dll";
private const string DllDirectory = "dll";

private readonly List<string> loadedAssemblies = new();
private readonly List<string> loadedAssemblies = [];

/// <summary>
/// Map processor
Expand Down Expand Up @@ -63,7 +63,7 @@ public sealed class WindowsLibraryLoader
/// </summary>
private WindowsLibraryLoader()
{
AdditionalPaths = new List<string>();
AdditionalPaths = [];
}

/// <summary>
Expand Down Expand Up @@ -114,7 +114,7 @@ public void LoadLibrary(string dllName, IEnumerable<string>? additionalPaths = n
if (!IsCurrentPlatformSupported())
return;

var additionalPathsArray = additionalPaths?.ToArray() ?? Array.Empty<string>();
var additionalPathsArray = additionalPaths?.ToArray() ?? [];

// In .NET Core, process only when additional paths are specified.
if (IsDotNetCore() && additionalPathsArray.Length == 0)
Expand Down Expand Up @@ -187,7 +187,7 @@ public void LoadLibrary(string dllName, IEnumerable<string>? additionalPaths = n
if (processArch.HasWarnings)
{
// include process detection warnings
errorMessage.AppendLine().Append($"Warnings: ").AppendLine().Append("{processArch.WarningText()}");
errorMessage.AppendLine().Append("Warnings: ").AppendLine().Append("{processArch.WarningText()}");
}

throw new OpenCvSharpException(errorMessage.ToString());
Expand All @@ -202,7 +202,7 @@ public void LoadLibrary(string dllName, IEnumerable<string>? additionalPaths = n
}

/// <summary>
/// Get's the current process architecture while keeping track of any assumptions or possible errors.
/// Gets the current process architecture while keeping track of any assumptions or possible errors.
/// </summary>
/// <returns></returns>
private ProcessArchitectureInfo GetProcessArchitecture()
Expand All @@ -214,7 +214,7 @@ private ProcessArchitectureInfo GetProcessArchitecture()
if (!string.IsNullOrEmpty(processArchitecture))
{
// Sanity check
processInfo.Architecture = processArchitecture!;
processInfo.Architecture = processArchitecture;
}
else
{
Expand Down Expand Up @@ -360,7 +360,7 @@ private class ProcessArchitectureInfo
public ProcessArchitectureInfo()
{
Architecture = "";
Warnings = new List<string>();
Warnings = [];
}

public bool HasWarnings => Warnings.Count > 0;
Expand Down
2 changes: 1 addition & 1 deletion src/OpenCvSharp/Internal/Vectors/VectorOfByte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public byte[] ToArray()
var size = Size;
if (size == 0)
{
return Array.Empty<byte>();
return [];
}
var dst = new byte[size];
Marshal.Copy(ElemPtr, dst, 0, dst.Length);
Expand Down
2 changes: 1 addition & 1 deletion src/OpenCvSharp/Internal/Vectors/VectorOfDMatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public DMatch[] ToArray()
var size = Size;
if (size == 0)
{
return Array.Empty<DMatch>();
return [];
}
var dst = new DMatch[size];
using (var dstPtr = new ArrayAddress1<DMatch>(dst))
Expand Down
2 changes: 1 addition & 1 deletion src/OpenCvSharp/Internal/Vectors/VectorOfDTreesNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public DTrees.Node[] ToArray()
var size = Size;
if (size == 0)
{
return Array.Empty<DTrees.Node>();
return [];
}
var dst = new DTrees.Node[size];
using (var dstPtr = new ArrayAddress1<DTrees.Node>(dst))
Expand Down
2 changes: 1 addition & 1 deletion src/OpenCvSharp/Internal/Vectors/VectorOfDTreesSplit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public DTrees.Split[] ToArray()
var size = Size;
if (size == 0)
{
return Array.Empty<DTrees.Split>();
return [];
}
var dst = new DTrees.Split[size];
using (var dstPtr = new ArrayAddress1<DTrees.Split>(dst))
Expand Down
2 changes: 1 addition & 1 deletion src/OpenCvSharp/Internal/Vectors/VectorOfDouble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public double[] ToArray()
var size = Size;
if (size == 0)
{
return Array.Empty<double>();
return [];
}
var dst = new double[size];
Marshal.Copy(ElemPtr, dst, 0, dst.Length);
Expand Down
2 changes: 1 addition & 1 deletion src/OpenCvSharp/Internal/Vectors/VectorOfFloat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public float[] ToArray()
var size = Size;
if (size == 0)
{
return Array.Empty<float>();
return [];
}
var dst = new float[size];
Marshal.Copy(ElemPtr, dst, 0, dst.Length);
Expand Down
4 changes: 2 additions & 2 deletions src/OpenCvSharp/Internal/Vectors/VectorOfImageFeatures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public ImageFeatures[] ToArray()
{
var size = Size;
if (size == 0)
return Array.Empty<ImageFeatures>();
return [];

VectorOfKeyPoint[]? keypointsVecs = null;
Mat[]? descriptors = null;
Expand All @@ -53,7 +53,7 @@ public ImageFeatures[] ToArray()
var nativeResult = new WImageFeatures[size];
keypointsVecs = new VectorOfKeyPoint[size];
descriptors = new Mat[size];
for (int i = 0; i < size; i++)
for (var i = 0; i < size; i++)
{
keypointsVecs[i] = new VectorOfKeyPoint();
descriptors[i] = new Mat();
Expand Down
Loading
Loading