// Copyright (c) 2010-2014 SharpDX - 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.Runtime.InteropServices;
namespace SharpDX
{
///
/// Result structure for COM methods.
///
[StructLayout(LayoutKind.Sequential)]
public struct Result : IEquatable
{
private int _code;
///
/// 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 the HRESULT error code.
///
/// The HRESULT error code.
public int Code
{
get { return _code; }
}
///
/// Gets a value indicating whether this is success.
///
/// true if success; otherwise, false.
public bool Success
{
get { return Code >= 0; }
}
///
/// Gets a value indicating whether this is failure.
///
/// true if failure; otherwise, false.
public bool Failure
{
get { return Code < 0; }
}
///
/// Performs an implicit conversion from to .
///
/// The result.
/// The result of the conversion.
public static explicit operator int(Result result)
{
return result.Code;
}
///
/// Performs an implicit conversion from to .
///
/// The result.
/// The result of the conversion.
public static explicit operator uint(Result result)
{
return unchecked((uint)result.Code);
}
///
/// Performs an implicit conversion from to .
///
/// The result.
/// The result of the conversion.
public static implicit operator Result(int result)
{
return new Result(result);
}
///
/// Performs an implicit conversion from to .
///
/// The result.
/// The result of the conversion.
public static implicit operator Result(uint result)
{
return new Result(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)
{
return this.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)
{
if (!(obj is Result))
return false;
return Equals((Result) obj);
}
///
/// 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()
{
return Code;
}
///
/// Implements the operator ==.
///
/// The left.
/// The right.
/// The result of the operator.
public static bool operator ==(Result left, Result right)
{
return left.Code == right.Code;
}
///
/// Implements the operator !=.
///
/// The left.
/// The right.
/// The result of the operator.
public static bool operator !=(Result left, Result right)
{
return left.Code != right.Code;
}
///
/// Returns a that represents this instance.
///
///
/// A that represents this instance.
///
public override string ToString()
{
return string.Format(System.Globalization.CultureInfo.InvariantCulture, "HRESULT = 0x{0:X}", _code);
}
///
/// Checks the error.
///
public void CheckError()
{
if (_code < 0)
{
throw new SharpDXException(this);
}
}
///
/// Gets a from an .
///
/// The exception
/// The associated result code
public static Result GetResultFromException(Exception ex)
{
return new Result(Marshal.GetHRForException(ex));
}
///
/// Gets the result from win32 error.
///
/// The win32Error.
/// A HRESULT.
public static Result GetResultFromWin32Error(int win32Error)
{
const int FACILITY_WIN32 = 7;
return win32Error <= 0 ? win32Error : (int)((win32Error & 0x0000FFFF) | (FACILITY_WIN32 << 16) | 0x80000000);
}
///
/// Result code Ok
///
/// S_OK
public readonly static Result Ok = new Result(unchecked((int)0x00000000));
///
/// Result code False
///
/// S_FALSE
public readonly static Result False = new Result(unchecked((int)0x00000001));
///
/// Result code Abort
///
/// E_ABORT
public static readonly SharpDX.ResultDescriptor Abort = new SharpDX.ResultDescriptor(unchecked((int)0x80004004), "General", "E_ABORT", "Operation aborted");
///
/// Result code AccessDenied
///
/// E_ACCESSDENIED
public static readonly SharpDX.ResultDescriptor AccessDenied = new SharpDX.ResultDescriptor(unchecked((int)0x80070005), "General", "E_ACCESSDENIED", "General access denied error");
///
/// Result code Fail
///
/// E_FAIL
public static readonly SharpDX.ResultDescriptor Fail = new SharpDX.ResultDescriptor(unchecked((int)0x80004005), "General", "E_FAIL", "Unspecified error");
///
/// Result code Handle
///
/// E_HANDLE
public static readonly SharpDX.ResultDescriptor Handle = new SharpDX.ResultDescriptor(unchecked((int)0x80070006), "General", "E_HANDLE", "Invalid handle");
///
/// Result code invalid argument
///
/// E_INVALIDARG
public static readonly SharpDX.ResultDescriptor InvalidArg = new SharpDX.ResultDescriptor(unchecked((int)0x80070057), "General", "E_INVALIDARG", "Invalid Arguments");
///
/// Result code no interface
///
/// E_NOINTERFACE
public static readonly SharpDX.ResultDescriptor NoInterface = new SharpDX.ResultDescriptor(unchecked((int)0x80004002), "General", "E_NOINTERFACE", "No such interface supported");
///
/// Result code not implemented
///
/// E_NOTIMPL
public static readonly SharpDX.ResultDescriptor NotImplemented = new SharpDX.ResultDescriptor(unchecked((int)0x80004001), "General", "E_NOTIMPL", "Not implemented");
///
/// Result code out of memory
///
/// E_OUTOFMEMORY
public static readonly SharpDX.ResultDescriptor OutOfMemory = new SharpDX.ResultDescriptor(unchecked((int)0x8007000E), "General", "E_OUTOFMEMORY", "Out of memory");
///
/// Result code Invalid pointer
///
/// E_POINTER
public static readonly SharpDX.ResultDescriptor InvalidPointer = new SharpDX.ResultDescriptor(unchecked((int)0x80004003), "General", "E_POINTER", "Invalid pointer");
///
/// Unexpected failure
///
/// E_UNEXPECTED
public static readonly SharpDX.ResultDescriptor UnexpectedFailure = new SharpDX.ResultDescriptor(unchecked((int)0x8000FFFF), "General", "E_UNEXPECTED", "Catastrophic failure");
///
/// Result of a wait abandonned.
///
/// WAIT_ABANDONED
public static readonly SharpDX.ResultDescriptor WaitAbandoned = new SharpDX.ResultDescriptor(unchecked((int)0x00000080L), "General", "WAIT_ABANDONED", "WaitAbandoned");
///
/// Result of a wait timeout.
///
/// WAIT_TIMEOUT
public static readonly SharpDX.ResultDescriptor WaitTimeout = new SharpDX.ResultDescriptor(unchecked((int)0x00000102L), "General", "WAIT_TIMEOUT", "WaitTimeout");
///
/// The data necessary to complete this operation is not yet available.
///
/// WAIT_TIMEOUT
public static readonly SharpDX.ResultDescriptor Pending = new SharpDX.ResultDescriptor(unchecked((int)0x8000000AL), "General", "E_PENDING", "Pending");
}
}