[Bluetooth][Non-ACR] Fix no data exception issue (#787)
[platform/core/csapi/tizenfx.git] / internals / src / EflSharp / EflSharp / eina_error.cs
1 #pragma warning disable 1591
2
3 using System;
4 using System.Runtime.InteropServices;
5
6 namespace Eina {
7
8 public struct Error : IComparable<Error>
9 {
10     int code;
11
12     public string Message
13     {
14         get { return MsgGet(this); }
15     }
16
17     public static Error UNHANDLED_EXCEPTION;
18
19     public static Error NO_ERROR = new Error(0);
20     public static Error EPERM = new Error(1);
21     public static Error ENOENT = new Error(2);
22     public static Error ECANCELED = new Error(125);
23
24     public Error(int value) { code = value; }
25     static public implicit operator Error(int val)
26     {
27         return new Error(val);
28     }
29     static public implicit operator int(Error error)
30     {
31         return error.code;
32     }
33     public int CompareTo(Error err)
34     {
35         return code.CompareTo(err.code);
36     }
37     public override string ToString()
38     {
39         return "Eina.Error(" + code + ")";
40     }
41
42     static Error()
43     {
44         UNHANDLED_EXCEPTION = eina_error_msg_register("Unhandled C# exception occurred.");
45     }
46
47     [DllImport(efl.Libs.Eina)] static extern Error eina_error_msg_register(string msg);
48     [DllImport(efl.Libs.Eina)] static extern Error eina_error_get();
49     [DllImport(efl.Libs.Eina)] static extern void eina_error_set(Error error);
50     [DllImport(efl.Libs.Eina)] static extern IntPtr eina_error_msg_get(Error error);
51
52     public static void Set(Error error)
53     {
54         eina_error_set(error);
55     }
56
57     public static Error Get()
58     {
59         return eina_error_get();
60     }
61
62     public static String MsgGet(Error error)
63     {
64         IntPtr cstr = eina_error_msg_get(error);
65         return Eina.StringConversion.NativeUtf8ToManagedString(cstr);
66     }
67
68     /// <summary>Raises an exception if an unhandled exception occurred before switching
69     /// back to the native code. For example, in an event handler.</summary>
70     public static void RaiseIfUnhandledException()
71     {
72         Error e = Get();
73         if (e == UNHANDLED_EXCEPTION)
74         {
75             Clear();
76             Raise(e);
77         }
78     }
79
80     public static void Raise(Error e)
81     {
82         if (e != 0)
83             throw (new Efl.EflException(MsgGet(e)));
84     }
85
86     public static void Clear()
87     {
88         Set(0);
89     }
90
91     public static Error Register(string msg)
92     {
93         return eina_error_msg_register(msg);
94     }
95 }
96 }