1
1
#if DOTNETCORE
2
- using System ;
3
- using System . Threading ;
4
2
5
- namespace OpenCvSharp . Internal
3
+ namespace OpenCvSharp . Internal ;
4
+
5
+ /// <summary>
6
+ /// This static class defines one instance which than can be used by multiple threads to gather exception information from OpenCV
7
+ /// Implemented as a singleton
8
+ /// </summary>
9
+ public static class ExceptionHandler
6
10
{
11
+ // ThreadLocal variables to save the exception for the current thread
12
+ private static readonly ThreadLocal < bool > exceptionHappened = new ( false ) ;
13
+ private static readonly ThreadLocal < ErrorCode > localStatus = new ( ) ;
14
+ private static readonly ThreadLocal < string > localFuncName = new ( ) ;
15
+ private static readonly ThreadLocal < string > localErrMsg = new ( ) ;
16
+ private static readonly ThreadLocal < string > localFileName = new ( ) ;
17
+ private static readonly ThreadLocal < int > localLine = new ( ) ;
18
+
7
19
/// <summary>
8
- /// This static class defines one instance which than can be used by multiple threads to gather exception information from OpenCV
9
- /// Implemented as a singleton
20
+ /// Callback function invoked by OpenCV when exception occurs
21
+ /// Stores the information locally for every thread
10
22
/// </summary>
11
- public static class ExceptionHandler
12
- {
13
- // ThreadLocal variables to save the exception for the current thread
14
- private static readonly ThreadLocal < bool > exceptionHappened = new ThreadLocal < bool > ( false ) ;
15
- private static readonly ThreadLocal < ErrorCode > localStatus = new ThreadLocal < ErrorCode > ( ) ;
16
- private static readonly ThreadLocal < string > localFuncName = new ThreadLocal < string > ( ) ;
17
- private static readonly ThreadLocal < string > localErrMsg = new ThreadLocal < string > ( ) ;
18
- private static readonly ThreadLocal < string > localFileName = new ThreadLocal < string > ( ) ;
19
- private static readonly ThreadLocal < int > localLine = new ThreadLocal < int > ( ) ;
20
-
21
- /// <summary>
22
- /// Callback function invoked by OpenCV when exception occurs
23
- /// Stores the information locally for every thread
24
- /// </summary>
25
- public static readonly CvErrorCallback ErrorHandlerCallback =
26
- delegate ( ErrorCode status , string funcName , string errMsg , string fileName , int line , IntPtr userData )
27
- {
28
- try
29
- {
30
- return 0 ;
31
- }
32
- finally
33
- {
34
- exceptionHappened . Value = true ;
35
- localStatus . Value = status ;
36
- localErrMsg . Value = errMsg ;
37
- localFileName . Value = fileName ;
38
- localLine . Value = line ;
39
- localFuncName . Value = funcName ;
40
- }
41
- } ;
42
-
43
- /// <summary>
44
- /// Registers the callback function to OpenCV, so exception caught before the p/invoke boundary
45
- /// </summary>
46
- public static void RegisterExceptionCallback ( )
23
+ public static readonly CvErrorCallback ErrorHandlerCallback =
24
+ delegate ( ErrorCode status , string funcName , string errMsg , string fileName , int line , IntPtr userData )
47
25
{
48
- IntPtr zero = IntPtr . Zero ;
49
- IntPtr ret = NativeMethods . redirectError ( ErrorHandlerCallback , zero , ref zero ) ;
50
- }
51
-
52
- /// <summary>
53
- /// Throws appropriate exception if one happened
54
- /// </summary>
55
- public static void ThrowPossibleException ( )
56
- {
57
- if ( CheckForException ( ) )
26
+ try
58
27
{
59
- throw new OpenCVException (
60
- localStatus . Value ,
61
- localFuncName . Value ?? "" ,
62
- localErrMsg . Value ?? "" ,
63
- localFileName . Value ?? "" ,
64
- localLine . Value ) ;
28
+ return 0 ;
65
29
}
66
- }
30
+ finally
31
+ {
32
+ exceptionHappened . Value = true ;
33
+ localStatus . Value = status ;
34
+ localErrMsg . Value = errMsg ;
35
+ localFileName . Value = fileName ;
36
+ localLine . Value = line ;
37
+ localFuncName . Value = funcName ;
38
+ }
39
+ } ;
67
40
68
- /// <summary>
69
- /// Returns a boolean which indicates if an exception occured for the current thread
70
- /// Reading this value changes its state, so an exception is handled only once
71
- /// </summary>
72
- private static bool CheckForException ( )
41
+ /// <summary>
42
+ /// Registers the callback function to OpenCV, so exception caught before the p/invoke boundary
43
+ /// </summary>
44
+ public static void RegisterExceptionCallback ( )
45
+ {
46
+ IntPtr zero = IntPtr . Zero ;
47
+ IntPtr ret = NativeMethods . redirectError ( ErrorHandlerCallback , zero , ref zero ) ;
48
+ }
49
+
50
+ /// <summary>
51
+ /// Throws appropriate exception if one happened
52
+ /// </summary>
53
+ public static void ThrowPossibleException ( )
54
+ {
55
+ if ( CheckForException ( ) )
73
56
{
74
- var value = exceptionHappened . Value ;
75
- // reset exception value
76
- exceptionHappened . Value = false ;
77
- return value ;
57
+ throw new OpenCVException (
58
+ localStatus . Value ,
59
+ localFuncName . Value ?? "" ,
60
+ localErrMsg . Value ?? "" ,
61
+ localFileName . Value ?? "" ,
62
+ localLine . Value ) ;
78
63
}
79
64
}
65
+
66
+ /// <summary>
67
+ /// Returns a boolean which indicates if an exception occured for the current thread
68
+ /// Reading this value changes its state, so an exception is handled only once
69
+ /// </summary>
70
+ private static bool CheckForException ( )
71
+ {
72
+ var value = exceptionHappened . Value ;
73
+ // reset exception value
74
+ exceptionHappened . Value = false ;
75
+ return value ;
76
+ }
80
77
}
81
- #endif
78
+
79
+ #endif
0 commit comments