22// The .NET Foundation licenses this file to you under the MIT license.
33
44#pragma warning disable CS1589 , CS0419 , CA1416 , CS0618
5+ using System . Runtime . CompilerServices ;
6+ using System . Runtime . InteropServices ;
7+
58namespace Silk . NET . Core ;
69
710/// <summary>
@@ -18,4 +21,131 @@ public partial struct HResult
1821 /// Has the function succeeded
1922 /// </summary>
2023 public bool Succeeded => Value >= 0 ;
24+
25+ /// <summary>
26+ /// Does the given HResult value indicates success
27+ /// </summary>
28+ /// <param name="hr">HResult value</param>
29+ /// <returns>Whether the value indicates success</returns>
30+ [ MethodImpl ( ( MethodImplOptions ) 768 ) ]
31+ public static bool IndicatesSuccess ( int hr ) => hr >= 0 ;
32+
33+ /// <summary>
34+ /// Does the given HResult value indicates failure
35+ /// </summary>
36+ /// <param name="hr">HResult value</param>
37+ /// <returns>Whether the value indicates failure</returns>
38+ [ MethodImpl ( ( MethodImplOptions ) 768 ) ]
39+ public static bool IndicatesFailure ( int hr ) => hr < 0 ;
40+
41+ /// <summary>
42+ /// Does the given HResult status indicates error
43+ /// </summary>
44+ /// <param name="status">HResult status value</param>
45+ /// <returns>Whether the value indicates error</returns>
46+ [ MethodImpl ( ( MethodImplOptions ) 768 ) ]
47+ public static bool IndicatesError ( int status ) => ( ( uint ) status >> 31 ) == 1 ;
48+
49+ /// <summary>
50+ /// Gets the code from the given HResult value
51+ /// </summary>
52+ /// <param name="hr">HResult value</param>
53+ /// <returns>code of the HResult</returns>
54+ [ MethodImpl ( ( MethodImplOptions ) 768 ) ]
55+ public static int GetCode ( int hr ) => hr & 0xFFFF ;
56+
57+ /// <summary>
58+ /// Gets the facility from the given HResult value
59+ /// </summary>
60+ /// <param name="hr">HResult value</param>
61+ /// <returns>facility of the HResult</returns>
62+ [ MethodImpl ( ( MethodImplOptions ) 768 ) ]
63+ public static int GetFacility ( int hr ) => ( hr >> 16 ) & 0x1FFF ;
64+
65+ /// <summary>
66+ /// Gets the severity from the given HResult value
67+ /// </summary>
68+ /// <param name="hr">HResult value</param>
69+ /// <returns>severity of the HResult</returns>
70+ [ MethodImpl ( ( MethodImplOptions ) 768 ) ]
71+ public static int GetSeverity ( int hr ) => ( hr >> 31 ) & 1 ;
72+
73+ /// <summary>
74+ /// Creates a HResult from the given severity, facility, and code values
75+ /// </summary>
76+ /// <param name="severity">severity value</param>
77+ /// <param name="facility">facility value</param>
78+ /// <param name="code">code value</param>
79+ /// <returns>HResult with the given severity, facility, and code</returns>
80+ [ MethodImpl ( ( MethodImplOptions ) 768 ) ]
81+ public static int Create ( int severity , int facility , int code ) =>
82+ ( int ) ( ( ( uint ) severity << 31 ) | ( ( uint ) facility << 16 ) | ( uint ) code ) ;
83+
84+ /// <summary>
85+ /// Constructs a HResult from the given severity, facility, and code values
86+ /// </summary>
87+ /// <param name="severity">severity value</param>
88+ /// <param name="facility">facility value</param>
89+ /// <param name="code">code value</param>
90+ public HResult ( int severity , int facility , int code ) =>
91+ Value = Create ( severity , facility , code ) ;
92+
93+ /// <summary>
94+ /// Does this HResult represent a success
95+ /// </summary>
96+ public bool IsSuccess
97+ {
98+ [ MethodImpl ( ( MethodImplOptions ) 768 ) ]
99+ get => IndicatesSuccess ( Value ) ;
100+ }
101+
102+ /// <summary>
103+ /// Does this HResult represent a failure
104+ /// </summary>
105+ public bool IsFailure
106+ {
107+ [ MethodImpl ( ( MethodImplOptions ) 768 ) ]
108+ get => IndicatesFailure ( Value ) ;
109+ }
110+
111+ /// <summary>
112+ /// Does this HResult represent a error
113+ /// </summary>
114+ public bool IsError
115+ {
116+ [ MethodImpl ( ( MethodImplOptions ) 768 ) ]
117+ get => IndicatesError ( Value ) ;
118+ }
119+
120+ /// <summary>
121+ /// Gets the code for this HResult
122+ /// </summary>
123+ public int Code
124+ {
125+ [ MethodImpl ( ( MethodImplOptions ) 768 ) ]
126+ get => GetCode ( Value ) ;
127+ }
128+
129+ /// <summary>
130+ /// Gets the facility for this HResult
131+ /// </summary>
132+ public int Facility
133+ {
134+ [ MethodImpl ( ( MethodImplOptions ) 768 ) ]
135+ get => GetFacility ( Value ) ;
136+ }
137+
138+ /// <summary>
139+ /// Gets the severity for this HResult
140+ /// </summary>
141+ public int Severity
142+ {
143+ [ MethodImpl ( ( MethodImplOptions ) 768 ) ]
144+ get => GetSeverity ( Value ) ;
145+ }
146+
147+ /// <summary>
148+ /// Throws this HResult as an exception
149+ /// </summary>
150+ public void Throw ( ) => Marshal . ThrowExceptionForHR ( Value ) ;
21151}
0 commit comments