1
+ // Copyright (c) Six Labors and contributors.
2
+ // Licensed under the Apache License, Version 2.0.
3
+
4
+ using System ;
5
+ using System . Diagnostics ;
6
+
7
+ namespace SixLabors
8
+ {
9
+ /// <summary>
10
+ /// Provides methods to protect against invalid parameters for a DEBUG build.
11
+ /// </summary>
12
+ [ DebuggerStepThrough ]
13
+ internal static class DebugGuard
14
+ {
15
+ /// <summary>
16
+ /// Verifies, that the method parameter with specified object value is not null
17
+ /// and throws an exception if it is found to be so.
18
+ /// </summary>
19
+ /// <param name="target">The target object, which cannot be null.</param>
20
+ /// <param name="parameterName">The name of the parameter that is to be checked.</param>
21
+ /// <exception cref="ArgumentNullException"><paramref name="target"/> is null</exception>
22
+ [ Conditional ( "DEBUG" ) ]
23
+ public static void NotNull ( object target , string parameterName )
24
+ {
25
+ if ( target == null )
26
+ {
27
+ throw new ArgumentNullException ( parameterName ) ;
28
+ }
29
+ }
30
+
31
+ /// <summary>
32
+ /// Verifies that the specified value is less than a maximum value
33
+ /// and throws an exception if it is not.
34
+ /// </summary>
35
+ /// <param name="value">The target value, which should be validated.</param>
36
+ /// <param name="max">The maximum value.</param>
37
+ /// <param name="parameterName">The name of the parameter that is to be checked.</param>
38
+ /// <typeparam name="TValue">The type of the value.</typeparam>
39
+ /// <exception cref="ArgumentException">
40
+ /// <paramref name="value"/> is greater than the maximum value.
41
+ /// </exception>
42
+ [ Conditional ( "DEBUG" ) ]
43
+ public static void MustBeLessThan < TValue > ( TValue value , TValue max , string parameterName )
44
+ where TValue : IComparable < TValue >
45
+ {
46
+ if ( value . CompareTo ( max ) >= 0 )
47
+ {
48
+ throw new ArgumentOutOfRangeException ( parameterName , $ "Value must be less than { max } .") ;
49
+ }
50
+ }
51
+
52
+ /// <summary>
53
+ /// Verifies that the specified value is less than or equal to a maximum value
54
+ /// and throws an exception if it is not.
55
+ /// </summary>
56
+ /// <param name="value">The target value, which should be validated.</param>
57
+ /// <param name="max">The maximum value.</param>
58
+ /// <param name="parameterName">The name of the parameter that is to be checked.</param>
59
+ /// <typeparam name="TValue">The type of the value.</typeparam>
60
+ /// <exception cref="ArgumentException">
61
+ /// <paramref name="value"/> is greater than the maximum value.
62
+ /// </exception>
63
+ [ Conditional ( "DEBUG" ) ]
64
+ public static void MustBeLessThanOrEqualTo < TValue > ( TValue value , TValue max , string parameterName )
65
+ where TValue : IComparable < TValue >
66
+ {
67
+ if ( value . CompareTo ( max ) > 0 )
68
+ {
69
+ throw new ArgumentOutOfRangeException ( parameterName , $ "Value must be less than or equal to { max } .") ;
70
+ }
71
+ }
72
+
73
+ /// <summary>
74
+ /// Verifies that the specified value is greater than a minimum value
75
+ /// and throws an exception if it is not.
76
+ /// </summary>
77
+ /// <param name="value">The target value, which should be validated.</param>
78
+ /// <param name="min">The minimum value.</param>
79
+ /// <param name="parameterName">The name of the parameter that is to be checked.</param>
80
+ /// <typeparam name="TValue">The type of the value.</typeparam>
81
+ /// <exception cref="ArgumentException">
82
+ /// <paramref name="value"/> is less than the minimum value.
83
+ /// </exception>
84
+ [ Conditional ( "DEBUG" ) ]
85
+ public static void MustBeGreaterThan < TValue > ( TValue value , TValue min , string parameterName )
86
+ where TValue : IComparable < TValue >
87
+ {
88
+ if ( value . CompareTo ( min ) <= 0 )
89
+ {
90
+ throw new ArgumentOutOfRangeException (
91
+ parameterName ,
92
+ $ "Value must be greater than { min } .") ;
93
+ }
94
+ }
95
+
96
+ /// <summary>
97
+ /// Verifies that the specified value is greater than or equal to a minimum value
98
+ /// and throws an exception if it is not.
99
+ /// </summary>
100
+ /// <param name="value">The target value, which should be validated.</param>
101
+ /// <param name="min">The minimum value.</param>
102
+ /// <param name="parameterName">The name of the parameter that is to be checked.</param>
103
+ /// <typeparam name="TValue">The type of the value.</typeparam>
104
+ /// <exception cref="ArgumentException">
105
+ /// <paramref name="value"/> is less than the minimum value.
106
+ /// </exception>
107
+ [ Conditional ( "DEBUG" ) ]
108
+ public static void MustBeGreaterThanOrEqualTo < TValue > ( TValue value , TValue min , string parameterName )
109
+ where TValue : IComparable < TValue >
110
+ {
111
+ if ( value . CompareTo ( min ) < 0 )
112
+ {
113
+ throw new ArgumentOutOfRangeException ( parameterName , $ "Value must be greater than or equal to { min } .") ;
114
+ }
115
+ }
116
+
117
+ /// <summary>
118
+ /// Verifies, that the method parameter with specified target value is true
119
+ /// and throws an exception if it is found to be so.
120
+ /// </summary>
121
+ /// <param name="target">
122
+ /// The target value, which cannot be false.
123
+ /// </param>
124
+ /// <param name="parameterName">
125
+ /// The name of the parameter that is to be checked.
126
+ /// </param>
127
+ /// <param name="message">
128
+ /// The error message, if any to add to the exception.
129
+ /// </param>
130
+ /// <exception cref="ArgumentException">
131
+ /// <paramref name="target"/> is false
132
+ /// </exception>
133
+ [ Conditional ( "DEBUG" ) ]
134
+ public static void IsTrue ( bool target , string parameterName , string message )
135
+ {
136
+ if ( ! target )
137
+ {
138
+ throw new ArgumentException ( message , parameterName ) ;
139
+ }
140
+ }
141
+
142
+ /// <summary>
143
+ /// Verifies, that the method parameter with specified target value is false
144
+ /// and throws an exception if it is found to be so.
145
+ /// </summary>
146
+ /// <param name="target">The target value, which cannot be true.</param>
147
+ /// <param name="parameterName">The name of the parameter that is to be checked.</param>
148
+ /// <param name="message">The error message, if any to add to the exception.</param>
149
+ /// <exception cref="ArgumentException">
150
+ /// <paramref name="target"/> is true
151
+ /// </exception>
152
+ [ Conditional ( "DEBUG" ) ]
153
+ public static void IsFalse ( bool target , string parameterName , string message )
154
+ {
155
+ if ( target )
156
+ {
157
+ throw new ArgumentException ( message , parameterName ) ;
158
+ }
159
+ }
160
+
161
+ /// <summary>
162
+ /// Verifies, that the target span is of same size than the 'other' span.
163
+ /// </summary>
164
+ /// <typeparam name="T">The element type of the spans</typeparam>
165
+ /// <param name="target">The target span.</param>
166
+ /// <param name="other">The 'other' span to compare 'target' to.</param>
167
+ /// <param name="parameterName">The name of the parameter that is to be checked.</param>
168
+ /// <exception cref="ArgumentException">
169
+ /// <paramref name="target"/> is true
170
+ /// </exception>
171
+ [ Conditional ( "DEBUG" ) ]
172
+ public static void MustBeSameSized < T > ( ReadOnlySpan < T > target , ReadOnlySpan < T > other , string parameterName )
173
+ where T : struct
174
+ {
175
+ if ( target . Length != other . Length )
176
+ {
177
+ throw new ArgumentException ( "Span-s must be the same size!" , parameterName ) ;
178
+ }
179
+ }
180
+
181
+ /// <summary>
182
+ /// Verifies, that the `target` span has the length of 'minSpan', or longer.
183
+ /// </summary>
184
+ /// <typeparam name="T">The element type of the spans</typeparam>
185
+ /// <param name="target">The target span.</param>
186
+ /// <param name="minSpan">The 'minSpan' span to compare 'target' to.</param>
187
+ /// <param name="parameterName">The name of the parameter that is to be checked.</param>
188
+ /// <exception cref="ArgumentException">
189
+ /// <paramref name="target"/> is true
190
+ /// </exception>
191
+ [ Conditional ( "DEBUG" ) ]
192
+ public static void MustBeSizedAtLeast < T > ( ReadOnlySpan < T > target , ReadOnlySpan < T > minSpan , string parameterName )
193
+ where T : struct
194
+ {
195
+ if ( target . Length < minSpan . Length )
196
+ {
197
+ throw new ArgumentException ( $ "Span-s must be at least of length { minSpan . Length } !", parameterName ) ;
198
+ }
199
+ }
200
+ }
201
+ }
0 commit comments