/*
* 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;
namespace Eina
{
/// Error codes from native Eina methods.
/// Since EFL 1.23.
///
public struct Error : IComparable
{
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
static public implicit operator Error(int val)
{
return new Error(val);
}
///
/// Int conversion from Error.
/// Since EFL 1.23.
///
/// Error identifier to be converted to int
static public implicit operator int(Error error)
{
return error.code;
}
///
/// Compare two Errors.
/// Since EFL 1.23.
///
/// Error to be compared with
/// True with the Errors is equal, False otherwise.
public int CompareTo(Error err)
{
return code.CompareTo(err.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.
///
public static void RaiseIfUnhandledException()
{
Error e = Get();
if (e == UNHANDLED_EXCEPTION)
{
Clear();
Raise(e);
}
}
///
/// Raises an exception.
/// Since EFL 1.23.
///
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);
}
}
}