/* * Copyright 2019 by its authors. See AUTHORS. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma warning disable 1591 using System; using System.Runtime.InteropServices; using System.Diagnostics.CodeAnalysis; namespace Eina { /// Error codes from native Eina methods. /// Since EFL 1.23. /// public struct Error : IComparable, IEquatable { int code; /// /// The error's message. /// Since EFL 1.23. /// public string Message { get { return MsgGet(this); } } /// /// Unhandled Exception error identifier. /// Since EFL 1.23. /// public static readonly Error UNHANDLED_EXCEPTION = eina_error_msg_register("Unhandled C# exception occurred."); /// /// No error identifier. /// Since EFL 1.23. /// public static readonly Error NO_ERROR = new Error(0); /// /// Permission error identifier. /// Since EFL 1.23. /// public static readonly Error EPERM = new Error(1); /// /// No entity error identifier. /// Since EFL 1.23. /// public static readonly Error ENOENT = new Error(2); /// /// Cancelled error identifier. /// Since EFL 1.23. /// public static readonly Error ECANCELED = new Error(125); /// /// Constructor. /// Since EFL 1.23. /// /// The value of the error. public Error(int value) { code = value; } /// /// Error identifier conversion from int. /// Since EFL 1.23. /// /// Value to be converted to Error public static implicit operator Error(int val) => FromInt32(val); /// /// Converts a to a . /// Since EFL 1.23. /// /// The to be converted. public static Error FromInt32(int val) => new Error(val); /// /// Int conversion from Error. /// Since EFL 1.23. /// /// Error identifier to be converted to int public static implicit operator int(Error error) => ToInt32(error); /// /// Converts a to a . /// Since EFL 1.23. /// /// The to be converted. public static int ToInt32(Error error) => error.code; /// /// Transform the object to a string representing the object. /// Since EFL 1.23. /// /// The string representing the value of this. public override string ToString() { return "Eina.Error(" + code + ")"; } [DllImport(efl.Libs.Eina)] static extern Error eina_error_msg_register(string msg); [DllImport(efl.Libs.Eina)] static extern Error eina_error_get(); [DllImport(efl.Libs.Eina)] static extern void eina_error_set(Error error); [DllImport(efl.Libs.Eina)] static extern IntPtr eina_error_msg_get(Error error); /// /// Sets the last error. /// Since EFL 1.23. /// /// The error identifier. public static void Set(Error error) { eina_error_set(error); } /// /// Returns the last set error. /// Since EFL 1.23. /// /// The last error or NO_ERROR identifier. public static Error Get() { return eina_error_get(); } /// /// Returns the description of the given error identifier. /// Since EFL 1.23. /// /// Error identifier. /// The description of the error. public static String MsgGet(Error error) { IntPtr cstr = eina_error_msg_get(error); return Eina.StringConversion.NativeUtf8ToManagedString(cstr); } /// Raises an exception if an unhandled exception occurred before switching /// back to the native code. For example, in an event handler. /// Since EFL 1.23. /// [SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate", Justification = "It's not an event.")] public static void RaiseIfUnhandledException() { Error e = Get(); if (e == UNHANDLED_EXCEPTION) { Clear(); Raise(e); } } /// /// Raises an exception. /// Since EFL 1.23. /// [SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate", Justification = "It's not an event.")] public static void Raise(Error e) { if (e != 0) { throw (new Efl.EflException(MsgGet(e))); } } /// /// Set identifier to a NO_ERROR. /// Since EFL 1.23. /// public static void Clear() { Set(0); } /// /// Registers a new error type. /// Since EFL 1.23. /// /// The description of the error. /// The unique number identifier for this error. public static Error Register(string msg) { return eina_error_msg_register(msg); } /// /// Gets a hash for . /// Since EFL 1.23. /// /// A hash code. public override int GetHashCode() => code.GetHashCode() + Message.GetHashCode(StringComparison.Ordinal); /// /// Compare to a given error. /// Since EFL 1.23. /// /// Error to be compared with. /// -1, 0 or 1 if -1 if Error is less, equal or greater than err. public int CompareTo(Error err) => code.CompareTo(err.code); /// /// Check if is equal to obj. /// Since EFL 1.23. /// /// The object to be checked. /// false if obj is null or not equals, true otherwise. public override bool Equals(object obj) { if (object.ReferenceEquals(obj, null)) return false; return this.Equals((Error)obj); } /// /// Check if is equal to err. /// Since EFL 1.23. /// /// The object to be checked. /// false if obj is null or not equals, true otherwise. public bool Equals(Error err) => this.CompareTo(err) == 0; /// /// Check if lhs is equals to rhs. /// Since EFL 1.23. /// /// The left hand side of the operator. /// The right hand side of the operator. /// true if lhs is equals to rhs, false otherwise. public static bool operator==(Error lhs, Error rhs) => lhs.Equals(rhs); /// /// Check if lhs is not equals to rhs. /// Since EFL 1.23. /// /// The left hand side of the operator. /// The right hand side of the operator. /// true if lhs is not equals to rhs, false otherwise. public static bool operator!=(Error lhs, Error rhs) => !(lhs == rhs); /// /// Check if lhs is less than rhs. /// Since EFL 1.23. /// /// The left hand side of the operator. /// The right hand side of the operator. /// true if lhs is less than rhs, false otherwise. public static bool operator<(Error lhs, Error rhs) => (lhs.CompareTo(rhs) < 0); /// /// Check if lhs is greater to rhs. /// Since EFL 1.23. /// /// The left hand side of the operator. /// The right hand side of the operator. /// true if lhs is greater than rhs, false otherwise. public static bool operator>(Error lhs, Error rhs) => rhs < lhs; /// /// Check if lhs is equals and less than rhs. /// Since EFL 1.23. /// /// The left hand side of the operator. /// The right hand side of the operator. /// true if lhs is equals and less than rhs, false otherwise. public static bool operator<=(Error lhs, Error rhs) => !(lhs > rhs); /// /// Check if lhs is equals and greater than rhs. /// Since EFL 1.23. /// /// The left hand side of the operator. /// The right hand side of the operator. /// true if lhs is equals and greater than rhs, false otherwise. public static bool operator>=(Error lhs, Error rhs) => !(lhs < rhs); } }