// Copyright (c) 2010-2014 SharpGen.Runtime - Alexandre Mutel // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; namespace SharpGen.Runtime { /// /// Result structure. /// [StructLayout(LayoutKind.Sequential)] public readonly partial struct Result : IEquatable { /// /// Gets the HRESULT error code. /// /// The HRESULT error code. public int Code { get; } /// /// Initializes a new instance of the struct. /// /// The HRESULT error code. public Result(int code) => Code = code; /// /// Initializes a new instance of the struct. /// /// The HRESULT error code. public Result(uint code) => Code = unchecked((int)code); /// /// Gets a value indicating whether this is success. /// /// true if success; otherwise, false. public bool Success => Code >= 0; /// /// Gets a value indicating whether this is failure. /// /// true if failure; otherwise, false. public bool Failure => Code < 0; /// /// Performs an implicit conversion from to . /// /// The result. /// The result of the conversion. public static explicit operator int(Result result) => result.Code; /// /// Performs an implicit conversion from to . /// /// The result. /// The result of the conversion. public static explicit operator uint(Result result) => unchecked((uint)result.Code); /// /// Performs an implicit conversion from to . /// /// The result. /// The result of the conversion. public static implicit operator Result(int result) => new(result); /// /// Performs an implicit conversion from to . /// /// The result. /// The result of the conversion. public static implicit operator Result(uint result) => new(result); /// /// Indicates whether the current object is equal to another object of the same type. /// /// An object to compare with this object. /// /// true if the current object is equal to the parameter; otherwise, false. /// public bool Equals(Result other) => Code == other.Code; /// /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. /// /// true if the specified is equal to this instance; otherwise, false. /// public override bool Equals(object obj) => obj is Result res && Equals(res); /// /// Returns a hash code for this instance. /// /// /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// public override int GetHashCode() => Code; /// /// Implements the operator ==. /// /// The left. /// The right. /// The result of the operator. public static bool operator ==(Result left, Result right) => left.Code == right.Code; /// /// Implements the operator !=. /// /// The left. /// The right. /// The result of the operator. public static bool operator !=(Result left, Result right) => left.Code != right.Code; /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override string ToString() => $"Result: {Code}"; private void ThrowFailureException() => throw new SharpGenException(this); /// /// Checks the error. /// public void CheckError() { if (Failure) ThrowFailureException(); } /// /// Checks the error. /// public void CheckError(Result allowedFail) { var code = Code; if (code < 0 && code != allowedFail.Code) ThrowFailureException(); } /// /// Checks the error. /// public void CheckError(Result allowedFail1, Result allowedFail2) { var code = Code; if (code >= 0) return; if (code != allowedFail1.Code && code != allowedFail2.Code) ThrowFailureException(); } /// /// Checks the error. /// public void CheckError(params Result[] allowedFails) { var code = Code; if (code < 0 && !allowedFails.Contains(code)) ThrowFailureException(); } /// /// Checks the error. /// public void CheckError(IEnumerable allowedFails) { var code = Code; if (code < 0 && !allowedFails.Contains(code)) ThrowFailureException(); } /// /// Gets a from an . /// /// The exception /// The associated result code public static Result GetResultFromException(Exception ex) => new(ex.HResult); } }