// ReSharper disable ConvertToAutoPropertyWhenPossible
using System;
namespace UnityEngine.XR.ARSubsystems
{
///
/// Represents the status of a completed operation as a cross-platform and a
/// platform-specific .
///
public readonly struct XRResultStatus : IEquatable, IComparable
{
///
/// Indicates whether the operation succeeded or failed as well as whether additional status information is
/// available in .
///
///
/// The integer value of this enum has the following meanings:
///
///
/// Value
/// Meaning
///
/// -
/// Less than zero
/// The operation failed with an error.
///
/// -
/// Zero
/// The operation was an unqualified success.
///
/// -
/// Greater than zero
/// The operation succeeded.
///
///
///
public enum StatusCode
{
///
/// Indicates that the operation was successful, and additional information is available in
/// .
///
PlatformQualifiedSuccess = 1,
///
/// Indicates that the operation was successful, and no additional information is available.
///
UnqualifiedSuccess = 0,
///
/// Indicates that the operation failed with an error, and additional information is available in
/// .
///
PlatformError = -1,
///
/// Indicates that the operation failed with an unknown error, and no additional information is available.
///
UnknownError = -2,
///
/// Indicates that the operation failed because the provider was uninitialized.
/// This may be because XR Plug-in Management has not yet initialized an XR loader, or because a
/// platform-specific resource is not yet available.
///
ProviderUninitialized = -3,
///
/// Indicates that the operation failed because the provider was not started.
/// This may be because you haven't enabled a necessary manager component, or because platform-specific
/// requirements to start the provider were not met.
///
ProviderNotStarted = -4,
///
/// Indicates that the input parameters failed to meet Unity's validation requirements,
/// and no additional information is available.
///
ValidationFailure = -5,
}
///
/// The cross-platform status code.
///
/// The status code.
public StatusCode statusCode => m_StatusCode;
readonly StatusCode m_StatusCode;
///
/// The platform-specific status code.
///
/// If is or
/// , this property contains platform-specific status information
/// that you can use to help debug issues on that platform. Refer to the provider plug-in documentation for your
/// target platform(s) to further understand how to interpret this integer value.
///
public int nativeStatusCode => m_NativeStatusCode;
readonly int m_NativeStatusCode;
///
/// Construct an instance with a given status code. This constructor assumes that there is no platform-specific
/// status information.
///
/// The platform-agnostic status code.
public XRResultStatus(StatusCode statusCode)
{
m_StatusCode = statusCode;
m_NativeStatusCode = default;
}
///
/// Construct an instance with a given native status code.
///
/// This constructor assumes that the integer value of the status code follows OpenXR conventions:
///
///
/// Value
/// Meaning
///
/// -
/// Less than zero
///
/// The operation failed with an error, and additional status information is available.
/// will be assigned the value .
///
///
/// -
/// Zero
///
/// The operation was an unqualified success.
/// will be assigned the value .
///
///
/// -
/// Greater than zero
///
/// The operation succeeded, and additional status information is available.
/// will be assigned the value .
///
///
///
///
/// If your platform's native status code does not follow these conventions, use the
/// constructor instead.
///
/// The integer value.
public XRResultStatus(int nativeStatusCode)
{
m_NativeStatusCode = nativeStatusCode;
m_StatusCode = nativeStatusCode switch
{
> 0 => StatusCode.PlatformQualifiedSuccess,
0 => StatusCode.UnqualifiedSuccess,
< 0 => StatusCode.PlatformError
};
}
///
/// Construct an instance with a given status code and native status code.
///
/// The status code.
/// The native status code.
public XRResultStatus(StatusCode statusCode, int nativeStatusCode)
{
m_StatusCode = statusCode;
m_NativeStatusCode = nativeStatusCode;
}
///
/// Construct an instance from a boolean value.
///
///
/// If , assigns a of .
/// Otherwise, assigns
///
public XRResultStatus(bool wasSuccessful)
{
m_StatusCode = wasSuccessful ? StatusCode.UnqualifiedSuccess : StatusCode.UnknownError;
m_NativeStatusCode = default;
}
///
/// Indicates whether the operation was an unqualified success. In other words, returns
/// if the operation succeeded and no additional status information is available.
///
/// if the operation was an unqualified success. Otherwise, .
public bool IsUnqualifiedSuccess() => m_StatusCode == StatusCode.UnqualifiedSuccess;
///
/// Indicates whether the operation was successful, inclusive of all success codes and
/// .
///
///
/// Equivalent to both `!IsError()` and implicitly converting this instance to .
///
/// if the operation was successful. Otherwise, .
public bool IsSuccess() => m_StatusCode >= 0;
///
/// Indicates whether the operation failed with an error.
///
///
/// Equivalent to `!IsSuccess()`.
///
/// if the operation failed with error. Otherwise, .
public bool IsError() => m_StatusCode < 0;
///
/// Convert from to `XRResultStatus` using the constructor.
///
/// Whether the operation was successful.
/// The status.
public static implicit operator XRResultStatus(bool wasSuccessful)
{
return new XRResultStatus(wasSuccessful);
}
///
/// Convert from `XRResultStatus` to using .
///
/// The status.
/// if the operation was successful. Otherwise, .
public static implicit operator bool(XRResultStatus status)
{
return status.IsSuccess();
}
///
/// Indicates whether the current object is equal to another object of the same type. Equality is compared using
/// both and .
///
/// The other object.
/// if the objects are equal. Otherwise, .
public bool Equals(XRResultStatus other)
{
return m_StatusCode == other.statusCode && m_NativeStatusCode == other.nativeStatusCode;
}
///
/// Compares the current instance with another object of the same type and returns an integer that indicates
/// whether the current instance precedes, follows, or occurs in the same position in the sort order as the
/// other object.
///
/// `XRResultStatus` objects are sorted first by their , then by their
/// .
///
/// An object to compare with this instance.
///
/// A value that indicates the relative order of the objects being compared. The return value has these meanings:
///
///
/// Value
/// Meaning
///
/// -
/// Less than zero
/// The instance precedes in the sort order.
///
/// -
/// Zero
/// The instance occurs in the same position in the sort order as .
///
/// -
/// Greater than zero
/// This instance follows in the sort order.
///
///
///
public int CompareTo(XRResultStatus other)
{
var nativeComparison = nativeStatusCode.CompareTo(other.nativeStatusCode);
return nativeComparison == 0 ? statusCode.CompareTo(other.statusCode) : nativeComparison;
}
}
}